Problem Session Working in pairs of two, solve the following problem...

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
CMSC 2021 Stream I/O Operators and Friend Functions.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Matrices Introducing Inheritance. Consider A matrix is a grid in which numbers can be stored. Algorithms for problems in scientific computing frequently.
Vectors of Vectors Representing Objects with Two Dimensions.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 07.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Applying Recursion Routing in Computer Networks. Review We’ve seen that recursion provides an elegant, simple, and (sometimes) efficient way to solve.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
C String, Proper Type, and String Objects Andy Wang COP 4530: Data Structures, Algorithms, and Generic Programming.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Problem Session Working in pairs of two, solve the following problem...
Operator Overloading.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Classes.
Vectors of Vectors Representing Objects with Two Dimensions.
Object-Oriented Design (OOD) and C++
Operator Overloading Part 2
Introducing Inheritance
A First C++ Class – a Circle
Jim Fawcett Copyright ©
solve the following problem...
Chapter 14: More About Classes.
Overloading the << operator
Operator Overloading; String and Array Objects
Operator Overloading.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Operator Overloading; String and Array Objects
References, const and classes
Constant Member Functions
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Vectors of Vectors Representing Objects with Two Dimensions.
Function Overloading.
Operator Overloading; String and Array Objects
Overloading the << operator
Jim Fawcett Copyright ©
solve the following problem...
Presentation transcript:

Problem Session Working in pairs of two, solve the following problem...

Problem Design an Address class to represent street addresses e.g., 1313 Mockingbird Lane Universal City, CA Identify –the function members needed to operate on such objects; –the data members needed to represent addresses. Implement as much of your design as time permits.

Coding: Interface // Address.h declares a ‘bare bones’ address class // #include directives have been omitted... class Address { public: Address(); Address(int houseNumber, const string & street, const string & city, const string & state, int zipCode); int HouseNumber() const; string Street() const; string City() const; string State() const; int ZipCode() const; friend istream & operator>>(istream & in, Address & addr); friend ostream & operator<<(ostream & out, const Address & addr); //...

Coding: Private Section //... still in Date.h private: int myHouseNumber; string myStreet, myCity, myState; int myZipCode; }; inline Address::Address() { myHouseNumber = myZipCode = 0; myStreet = myCity = mystate = “”; }

Coding (Ct’d) //... still in Date.h inline int Address::HouseNumber() const { return myHouseNumber; } inline string Address::Street() const { return myStreet; } inline string Address::City() const { return myCity; }

Coding (Ct’d) //... still in Date.h inline string Address::State() const { return myState; } inline int Address::ZipCode() const { return myZipCode; }

Coding (Ct’d) // Date.cpp defines non-trivial Date function members. //... #include “Date.h” // class Date inline Address::Address(int houseNumber, const string & street, const string & city, const string & state, int zipCode) { assert(houseNumber >= 0 && zipCode > 0); myHouseNumber = houseNumber; myStreet = street; myCity = city; myState = state; myZipCode = zipCode; }

Coding (Ct’d) //... Date.cpp continued // display using three-line format ostream & operator<<(ostream & out, const Address & addr) { out << addr.myHouseNumber << ‘ ‘ // line 1 << addr.myStreet << ‘\n’ << addr.myCity << “, “ // line 2 << addr.myState << ‘\n’ << addr.myZipCode; // line 3 return out; }

Coding (Ct’d) //... Date.cpp continued // assume three-line format istream & operator>>(istream & in, Address & addr) { int number, zipCode; string street = “”, city = “”, state = “”; in >> number; // beginning of line 1 assert(in.good()); getline(in, street); // rest of line 1 is street char separator; string word; do { // line 2: in >> word; // city may consist of city += word; // multiple words in.get(separator); // terminated by } while (separator != ‘,’); // a comma //...

Coding (Ct’d) //... Date.cpp continued //... do { // line 2 (ctd) in >> word; // state may consist of state += word; // multiple words in.get(separator); // terminated by } while (separator != ‘\n’); // a newline in >> zipCode; // line 3: zipcode if (in.good()) // change addr if all is well addr = Address(number, street, city, state zipCode); return in; }