Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro."— Presentation transcript:

1 ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro

2 Lecture outline Announcements / reminders  Lab 9 (last assignment) Wednesday Session (April 10) Due April 12, Friday by 5:00 pm Monday Session(April 15) Rescheduled to April 17, Wednesday; Due April 19, Friday, by 5:00 pm  Exam 2 review next week  Focus on your term project (great team!) Today  Review copy constructors  Operator overloading  Introduce inheritance ECE 264: Lecture 17 2

3 Copy constructors Used to initialize a newly declared variable from an existing variable  Not called for assignments  Example: Point p1(2,3), p3; Point p2 = p1;// calls copy constructor p3 = p2;// uses assignment Default behavior: shallow copy  Directly copy data members  Doesn’t work for arrays and pointers  Perform deep copy—allocate enough space for data, then copy the data (not the pointer) ECE 264: Lecture 17 3

4 Motivating operator overloading Recall earlier example Point p1(2,3), p3; Point p2 = p1;// calls copy constructor p3 = p2;// uses assignment We handled first case with copy constructors What about second case?  Uses assignment operator  How is that operator defined? ECE 264: Lecture 17 4

5 Overloading Overloading: giving a function more than one meaning  We’ve seen examples of overloading before Constructors: default vs. parameterized vs. copy  All have same name, but different arguments  We’ve even seen overloaded operators String concatenation: + +=  Works with strings, char*, single characters, etc. Input/output operators: >  Work with any built-in type Can define own operators that allow use of our classes in same manner as built-in type ECE 264: Lecture 17 5

6 Overloading Operators Definitions for overloaded operators (usually) included in class  Look just like functions  May be binary or unary  Example double operator –(const Point& p2);  Syntax: Keyword operator is used, followed by the actual operator “Argument” is (usually) an object reference of the same type const declarations are optional but often preferred  Only predefined operators may be overloaded. Exceptions:. ::.* ?: sizeof ECE 264: Lecture 17 6

7 Example: Overloaded Point operator //Distance Formula double Point::operator –(const Point& rhs) { double t1, t2, d; t1 = rhs.xCoord – xCoord; //(x2-x1) t2 = rhs.yCoord – yCoord; //(y2-y1) d = std::sqrt( std::pow(t1,2) + std::pow(t2,2) ); return d; } ECE 264: Lecture 17 7 xCoord and yCoord are provided by the calling object. To access the corresponding data from the object on the right hand side of the operator, use rhs.xCoord and rhs.yCoord

8 Forms of overloaded operators Member functions  Left hand side of binary operator (x, if operation is x + y) invokes operator Operator requires one argument (y)  Object used with unary operator (e.g., -x) invokes operator  Type of x must match the class Friend functions  Typically used when left hand side of binary operator does not match class For example, cout << x  cout (type ostream ) is on LHS Want operator to have access to data members of class ECE 264: Lecture 17 8

9 Friend Functions binary operators  friend function requires two arguments unary operator  friend function requires one argument Disadvantage  A friend function is NOT a member function  Friend functions violate a strict interpretation of object oriented principals (implementation is hidden)  Recommended for operator overloading only Particularly useful with stream operators > ECE 264: Lecture 17 9

10 Example: In class definition (.h file): friend complex operator +(complex c1, complex c2); In class implementatio (.cpp file) n: complex operator +(complex c1, complex c2) { complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } In client program: complex cA, cB, cC; cC = cA+cB; cC = 54.3 + cB; //this is ok, when + is a friend function ECE 264: Lecture 17 10

11 Inheritance Animal, mammal, dog, poodle - Inheritance. A poodle is-a dog, a dog is-a mammal, a mammal is- an animal. (refer to image.google.com) Food, dessert, pie, banana-cream - Inheritance. Pie is food. Dessert is food. Notice this is a tree structure - you could have many types of dessert, and many types of pies. ECE 264: Lecture 17 11

12 Motivating Inheritance Say we have two classes as part of a company’s payroll system  Employee models: general company employee who is paid hourly Pay = (pay rate) * (hours worked)  Manager models: a specific type of employee that may be salaried or hourly Pay = (pay rate) ECE 264: Lecture 17 12

13 Inheritance example: class diagram Employee - string name - float payRate + Employee() + Employee(string theName, float thePayRate) + getName() : string + getPayRate() : float + pay(float hrsWorked) : float Manager - string name - float payRate - bool salaried + Manager() + Manager(string theName, float thePayRate, bool isSalaried) + getName() : string + getPayRate() : float + isSalaried() : bool + setSalaried(bool sal) + pay(float hrsWorked) : float ECE 264: Lecture 17 13 Functions/data in red completely redundant Functions in blue share some functionality Would like to reuse code wherever possible

14 Inheritance The creation of a new class from an existing class is called inheritance Promotes software reusability Terminology:  Base class is existing class  Derived class reuses data and functions from base class Can customize base class functions Can add functions to derived class Can have inheritance hierarchy ECE 264: Lecture 17 14

15 Inheritance and UML Inheritance models “is a” relationship  Example: a square is a rectangle with equal length sides Contrast with composition (“has a”)  Example: a rectangle has a point of origin ECE 264: Lecture 17 15 Rectangle Square Point

16 Inheritance example: base class class Employee { private: string name; float payRate; public: Employee(); Employee(string n, float pr); string getName(); float getPayRate(); float pay(float hrsWorked); }; ECE 264: Lecture 17 16

17 Inheritance example: derived class class Manager : public Employee { private: bool salaried; public: Manager(); Manager(string theName, float thePayRate, bool isSalaried); bool isSalaried(); void setSalaried(bool sal); float pay(float hrsWorked); }; The notation above indicates that Manager inherits from Employee Only declare data/functions that are not shared ECE 264: Lecture 17 17

18 ECE 264: Lecture 17 18 Constructors and Inheritance Default constructor for a base class is called automatically in the derived class constructor  Ex: Manager() calls Employee()  Will actually traverse inheritance hierarchy, starting at lowest class If a derived class needs the parameterized constructor of a base class, it must explicitly invoke it in an initialization list

19 Inheritance: Manager constructors How would you write the two constructors for the Manager class?  Manager(); Manager::Manager() { salaried = false;} Employee default constructor called automatically  Manager(string theName, float thePayRate, bool isSalaried); Manager::Manager(string theName, float thePayRate, bool isSalaried): Employee(theName, thePayRate) { salaried = isSalaried; } Explicitly call Employee parameterized constructor ECE 264: Lecture 17 19

20 Final notes Next time  Operator overloading examples  Inheritance  Exam review 2 Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. ECE 264: Lecture 17 20


Download ppt "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro."

Similar presentations


Ads by Google