Reminder • Exam 1 – next Thursday, Sept 29 th C++: Classes -- Methods, • Details to be given Thursday. Operators Logistics Plan for Today • Project • C++ Classes – Part 1 (clock and design) due Sunday, Sept 25 th – Overloading functions/methods – Start thinking about partners for Parts 2-3 – Overloading operators • Questions? Function overloading Function overloading class Foo { • The same function / method name can be public: used several times: char f(); – The argument set and return type must be char f (int x); different for each function definition char f (int x, int y); – Overloaded functions cannot differ by return double f (double x, double y); type alone. int f (int x); // not allowed } 1
Function overloading Operator overloading • Why bother? • All C++ operators can be overloaded on a void print_int (int i); class by class basis. void print_char (char c); • Overloaded operators call specially named void print_double (double d); class methods. Compared to – Keyword operator followed by operator to void print (int i); be overloaded. void print (char c); – E.g void print (double d); • operator+ Operator overloading Operator overloading class Complex • Once overloaded, operators can be used in the { same manner as for basic types. private: • E.g. double re, im; Complex c1, c2, c3; public: Complex (double r, double i); double d=5.0; Complex operator+( Complex &c ) const; Complex operator-() const; c2 = c1 + c3; bool operator== (Complex &c) const; c3 = -c1; Complex& operator+= (Complex &c) ; c3+=c2; Complex& operator+= (double d); }; c3+=d; if (c3 == c1) { … } Operator overloading Operator overloading • What would the definition of an overloaded • Using overloaded operators is just a operator function look like? shorthand for calling the specially named class methods. Complex Complex::operator+( Complex &c ) const { • E.g. return Complex (re + c.re, im + c.im); c2 = c1 + c3; } • Is the same as bool Complex::operator== (Complex &c) const c2 = c1.operator+ (c3); { return ((re == c.re) && (im == c.im)); } 2
Operator overloading Operator overloading • What would the definition of an overloaded • It would be nice for all operators to return operator function look like? references…but this is difficult Complex& Complex::operator+( Complex &c ) const Complex& Complex::operator+= (Complex &c) { { // Here memory associated with CC will go re += c.re; // away once function completes im += c.im; return (*this); Complex CC(re + c.re, im + c.im); } return (CC); // Returning ref to a var // that will no longer exist. } Operator overloading Operator overloading • It would be nice for all operators to return • So when can references be returned? references…but this is difficult – Operators that modify themselves and return references to themselves. Complex& Complex::operator+( Complex &c ) const – Const operators, which just use the values of an { object will generally create an pass back a new // We could try to allocate on the free store object. Complex *CC – Logical operators should return bool . (new Complex (re + c.re, im + c.im)); return (*CC); // but who’s going to clean this // up? } Operator overloading Operator overloading • Overloaded operators can also be defined • Friends globally as non-members (outside of the – By declaring a function as a friend , we allow class definition) it access to a class’s private data members (both data and methods) 3
Operator overloading Operator overloading • Global operator definitions • Why use friend? – Used for operators that have another class as the left friend Complex operator+( Complex &c1, Complex &c2 ); operand friend Complex operator-(Complex &c1); friend bool operator== (const Complex &c1, const • E.g. << (as we’ll see in next slide) Complex &c2); – permit operators to be commutative . friend Complex& operator+= (Complex &c1, const Complex Complex c1, c2; &c2); double d; friend Complex& operator+= (Complex &c, double d); c1 = c2 + d; c1 = d + c2; // Not allowed if member Operator overloading I/O overloaded operators • Global friend operators can be declared anywhere. • Overloading << and >> – public and private don't apply to them. friend ostream& operator<<(ostream& output, class Complex const Complex c) { { output << c.re << “ + “ << c.im << “ I” private: return output; double re, im; } friend Complex operator+( Complex &c1, Complex &c2 ); friend Complex operator-(Complex &c1); public: Complex c1 (1.0, 2.0); Complex (double r, double i); cout << “My complex number is: “ << c1; friend bool operator== (const Complex &c1, const Complex &c2); friend Complex& operator+= (Complex &c1, const Complex &c2); My complex number is: 1.0 + 2.0 i friend Complex& operator+= (Complex &c, double d); }; Overloading operators Assignment operator • Questions? • operator= – Called when an assignment is made – Copies all relevant data from object assigner to assignee. – Must be declared as a class member – Should check for self-assignment! 4
Assignment operator Operators that can be overloaded • Unary Arithmetic Operators • If no assignment operator is defined for a – - , += , -= , *= , /= , ++ , -- class, the default assignment operator is used. • Binary Arithmetic operators – + , - , / , * , % – Member by member copy of data from one object to another. • Relational operator (should return bool) – Can be troublesome if class have pointers as – < , > , <= , >= , == data members. • Assignment – = Operators that can be overloaded Summary • Subscript (For collection classes) • Function overloading – [] • Operator overloading • Pointer to member (for pointer classes) – -> • Call operator – () • Memory management – new , delete Next time • Templates and the Standard Template Library 5
Recommend
More recommend