solve the following problem...

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 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.
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:
Problem Session Working in pairs of two, solve the following problem...
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.
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.
 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.
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.
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.
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...
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Classes.
Strings: C-strings vs. Strings as Objects
Chapter 14: More About Classes.
Vectors of Vectors Representing Objects with Two Dimensions.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Operator Overloading; String and Array Objects
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.
Strings: C-strings vs. Strings as Objects
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
Abstraction: Generic Programming, pt. 2
Classes and 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.
Operator Overloading; String and Array Objects
Overloading the << operator
Jim Fawcett Copyright ©
Presentation transcript:

solve the following problem... 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 12345 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; }