


























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Operator Overloading is one of the most exciting features of object oriented programming. It can transform complex, obscure program listings into intuitively obvious ones. For example, Statements like d3.addobjects(d1, d2); or the similar but equally obscure d3 = d1.addobjects(d2); can be changed to the much more readable d3 = d1 + d2; The rather forbidding term operator overloading refers to giving the normal C++ operators, such as +, *, <=, and +=, additional meanings when they are applied
Typology: Slides
1 / 66
This page cannot be seen from the preview
Don't miss anything!



























































Overloading Unary (++) Operator (2/2) int main() { Counter c1, c2; // define and initialize cout << "\nc1=" << c1.return_count(); // display cout << "\nc2=" << c2.return_count(); ++c1; // increment c ++c2; // increment c ++c2; // increment c cout << "\nc1=" << c1.return_count(); //display again cout << "\nc2=" << c2.return_count() << endl; system("PAUSE"); return 0; }
Returning Values in Operator (1/2) //increment counter variable with ++ operator, return value #include
Returning Nameless Temporary Objects (1/2) // increment with ++ operator uses unnamed temporary object #include
Returning Nameless Temporary Objects (2/2) int main() { Counter c1, c2; // c1=0, c2= cout << "\nc1=" << c1.get_count(); // display cout << "\nc2=" << c2.get_count(); ++c1; // c1= c2 = ++c1; // c1=2, c2= cout << "\nc1=" << c1.get_count(); // display again cout << "\nc2=" << c2.get_count() << endl; system("PAUSE"); return 0; }
Using Postfix Notation (2/2) int main() { Counter c1, c2; // c1=0, c2= cout << "\nc1=" << c1.get_count(); // display cout << "\nc2=" << c2.get_count(); ++c1; // c1= c2 = ++c1; // c1=2, c2=2 (prefix) cout << "\nc1=" << c1.get_count(); // display cout << "\nc2=" << c2.get_count(); c2 = c1++; // c1=3, c2=2 (postfix) cout << "\nc1=" << c1.get_count(); // display again cout << "\nc2=" << c2.get_count() << endl; system("PAUSE"); return 0; }
Using Postfix Notation
In the program, We have two different declarations for overloading the ++ operator.
Declaration for prefix notation is Counter operator ++ ()
for postfix notation, is Counter operator ++ (int)
The only difference is the int in the parentheses.
This int isn’t really an argument, and it doesn’t mean integer.
It’s simply a signal to the compiler to create the postfix version of the operator.
Overloading Binary Operators (2/3) Distance Distance::operator + (Distance d2) const { // add this distance to d2 and return sum int f = feet + d2.feet; // add the feet float i = inches + d2.inches; // add the inches if(i>= 12.0){ // if total exceeds 12.0, i-= 12.0; // then decrease inches by 12.0 and f++; // increase feet by 1 } // return a temporary Distance return Distance(f,i); // initialized to sum } int main() { Distance dist1, dist3, dist4; // define distances dist1.getdist(); // get dist1 from user Distance dist2(11, 6.25); // define, initialize dist dist3 = dist1 + dist2; // single ‘+’ operator dist4 = dist1 + dist2 + dist3; // multiple ‘+’ operators
Overloading Binary Operators (3/3) // display all lengths cout << "dist1 = " ; dist1.showdist(); cout << endl; cout << "dist2 = " ; dist2.showdist(); cout << endl; cout << "dist3 = " ; dist3.showdist(); cout << endl; cout << "dist4 = " ; dist4.showdist(); cout << endl; system("PAUSE"); return 0; }
Concatenating String (1/3) // strplus.cpp overloaded ‘+’ operator concatenates strings #include