EECE 2560 Lecture Notes - Operator Overloading, Code Reuse

42 views3 pages

Document Summary

// sometimes it is easier to read object1 + object 2 than: readobject1(class1) readobject2(class2) class1. duration(class2) // class1 + class2 is just more intuitive, and we can build in this functionality into our program with operator overloading. Ba returntype operator op (type1 lhs, type2 rhs) _____ bool operator == (const time24 &1lhs, const time24 &rhs) return lhs. gethour() == rhs. gethour() && lhs. getminute() == rhs. getminute(); 3a. time24 operator + (int min1, const time24 &rhs) // because these operators are closely connected to the time24 class, we will allow them to access private data. // this means we don"t have to use the interface to access the data, i. e. time24object. gethour(), . getminute(), etc. // this is a rare exception of allowing a global function to access private data. // let the overloaded operator functions be a friend of the time24 class. // a friend is a global function that can access private members of a particular class.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers