Download presentation
Presentation is loading. Please wait.
Published byAntony Fox Modified over 7 years ago
1
Overloading C++ supports the concept of overloading Two main types
Function Operator The same function/operator is defined multiple times Which is used is based on context
2
Function overloading A function, or method, with the same name is defined multiple times The compiler decides which to use based on the argument list Considerations Number of arguments Types of arguments Inheritance Lower-level method will be chosen over a higher level
3
Function overloading (type based)
class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; void print(char* c) { cout << "Printing character: " << c << endl; }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print( ); // Call print to print character pd.print("Hello C++"); return 0; }
4
Function overloading (number based)
class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(int i, int j) { cout << "Printing 2 ints: " << i << “ “ << j << endl; }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print two integers pd.print(5,10); return 0; }
5
Operator overloading C++ operators can be overloaded to perform user-defined, object- based functions Type of operators Both unary and binary Assignment Relational Input/output Subscript … + - * / % ^ & | ~ ! , = < > <= >= ++ -- << >> == != && || += -= /= %= ^= &= |= *= <<= >>= [] () -> ->* new new [] delete delete[] C++ operators that can be overloaded
6
Operator Overloading Define a method/function using the keyword operator Combine the keyword with the operator symbol to be overloaded Ex: cname operator+(int i); This will “add” an integer to the object cname Note: overloading can be defined for different types! For unary operators (!, ++), the overload method should be a member of the class For binary operators (two arguments) the overload method may be a member of the class depending on the “left-hand rule”
7
“left-hand rule” Applies to binary operators The rule:
If the object itself is on the left-hand side of the expression to be evaluated, the operator can be made a member of the class In this case, only the right hand side of the expression need to be passed to the method, the left hand is implied (it is the class instance itself) If not, operator overloading can still be done, but it must be defined externally to the class In this case, both the left and right hand sides of the expression need to be passed to the overload function (i.e. two arguments)
8
Applying the “left-hand rule”
Left hand side is the object type Add a number of additional days to a Date object Date Date::operator+( int additionalDays ) { … } Single argument: the number of days Compare two Date objects bool operator>(const Date &d) { … } Single argument: the “other” Date object Left hand side is another object type Output a Date object ostream &operator<<( ostream &output, const Date &d ) { … } Two arguments: the stream and the date
9
this this is a special variable that Example use:
Is created automatically with every instance of a class Is a pointer to that instance i.e. its value is the memory location of the instance Example use: Enable a method to return a reference to the instance To enable operator cascading
10
Special Considerations (cascading)
Certain operators return a reference to the left-hand operand in an overload function Enables operator “cascading” Ex: // cascaded assignment A = B = C; // cascaded output cout << D1 << D2 << D3; ostream & operator<<(ostream &output, const Date &d) { . . . output << … // enables cascading return output; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.