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

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
OOP Languages: Java vs 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:
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
More C++ Features True object initialisation
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Object Oriented Programming
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
1 CSC241: Object Oriented Programming Lecture No 05.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
1 CSC241: Object Oriented Programming Lecture No 08.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Operator Overloading.
ECE 264 Object-Oriented Software Development
Chapter 14: More About Classes.
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Presentation transcript:

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

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

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

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

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

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

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

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

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

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 = cB; //this is ok, when + is a friend function ECE 264: Lecture 17 10

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

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

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 Functions/data in red completely redundant Functions in blue share some functionality Would like to reuse code wherever possible

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

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 Rectangle Square Point

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

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

ECE 264: Lecture 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

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

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