// overload4.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // // This program illustrates operator overloading. The program // shows how operators can be overloaded as member functions // of a class. // The following points must be considered when overloading // operators: // 1) The programmer may defines operators only for class // types, or enumeration types. // // 2) The predefined precedence of operators can not be // overridden. That is, the "+" operator, for example, // will always have a higher precedence than the "==" // operator. // // 3) Default arguments for an operator overloading function // is illegal. // // 4) The predefined meaning of an operator for the built-in // data types may not be overridden. For example, the // built-in integer addition can not be replaced with // an operation that performs anything else. // // 5) Overloaded operators can serve as either binary or // unary operators, as they are defined by the language. A // binary operator is an operator that have two operands. // Four predefined operators can be both binary or unary // operators, since this is how they are originally defined // by the language. These operators are: "+", "-", "*" // and "&". // // 6) The predefined parity of the operator must be preserved. // For example, the unary operator "!" can not be defined // as the binary inequality operator for two "String" class // objects. The following implementation would then be illegal: // operator !( String st1, String st2 ) // { return ( strcmp(st1, st2) != 0); } // // // Key Points: 1) Operator overloading. #include "overload4.h" Vehicle::Vehicle( int nd, double mxspd, double pr ) { numdoors = nd; maxspeed = mxspd; price = pr; } int Vehicle::GetNumDrs( void ) { return numdoors; } void Vehicle::SetNumDrs( int nd ) { numdoors = nd; } double Vehicle::GetmaxSpeed( void ) { return maxspeed; } void Vehicle::SetMaxSpeed( double mxspd) { maxspeed = mxspd; } double Vehicle::GetPrice( void ) { return price; } void Vehicle::SetPrice( double pr ) { price = pr; } void Vehicle::ShowInfo( void ) { cout << " The vehicle has " << numdoors << " doors, " << maxspeed << " KM maximum speed, and its price is " << price << "$." << endl << endl; } bool Vehicle::operator== ( const Vehicle& vec) // overload the "==" operator. Now, Vehicle objects can be compared in the very // usual way using the "==" operator. { if ((numdoors == vec.numdoors) && (maxspeed == vec.maxspeed) && (price == vec.price)) return true; else return false; } double Vehicle::operator+( const Vehicle& vec) // overload the "+" operator. With this operator, the total price // of two car objects can simply be found by performing an // addition of these objects. That is, an addition of two vehicle objects // indicates an addition of the prices of these objects. { return price + vec.price; } int main() { Vehicle v1(2, 180, 22000); Vehicle v2; cout << "\n Here is the information of v1: " << endl; cout << " ============================== " << endl; v1.ShowInfo(); cout << "\n Here is the information of v2: " << endl; cout << " ============================== " << endl; v2.ShowInfo(); if (v1 == v2) cout << " v1 and v2 are identical objects. " << endl; else cout << " v1 and v2 are not identical objects. " << endl; v2 = v1; // The "=" operator is overloaded already by most compilers. cout << "\n Here is the information of v2 after updating it: " << endl; cout << " ============================== " << endl << endl; v2.ShowInfo(); cout << "\n Comparing v1 and v2 again" << endl; cout << " =========================" << endl << endl; if (v1 == v2) cout << " v1 and v2 are identical objects. " << endl; else cout << " v1 and v2 are not identical objects. " << endl; Vehicle v3(2, 200, 3000), v4(4, 180, 6000); cout << "\n The total price of v3 and v4 is: " << v3 + v4 << endl; return 0; } // The result of running the program /* Here is the information of v1: ============================== The vehicle has 2 doors, 180 KM maximum speed, and its price is 22000$. Here is the information of v2: ============================== The vehicle has 4 doors, 200 KM maximum speed, and its price is 10000$. v1 and v2 are not identical objects. Here is the information of v2 after updating it: ============================== The vehicle has 2 doors, 180 KM maximum speed, and its price is 22000$. Comparing v1 and v2 again ========================= v1 and v2 are identical objects. The total price of v3 and v4 is: 9000 */