In: Computer Science
// Overloaded operator is a Rectangle member function #include <iostream > using namespace std ; /************ Declaration Section ******************/ class Rectangle{ private : int width , length ; public : Rectangle (); Rectangle ( int w, int l ); void printArea (); void displayInfo (); Rectangle operator +(Rectangle& r ); }; /************ Implementation Section ******************/ Rectangle :: Rectangle () { width = 1, length = 2; } Rectangle :: Rectangle ( int w, int l ) { width = w, length = l ; } void Rectangle :: printArea () { cout <<"Area of "<< this <<" = "<< width*length << endl ; } void Rectangle :: displayInfo () { cout << "I am a " << width <<"x"<< length <<" rectangle " << "with id " << this << endl ; } Rectangle Rectangle :: operator +( Rectangle& r ) { Rectangle temp ; // cout << "This " << this->width << " " << this->length << endl;; // cout << "argument r: "<< r.width << " " << r.length << endl; temp.width = this->width + r.width ; temp.length = this->length + r.length ; return temp; } /***************** Main Section *********************/ int main (){ Rectangle r1 , r2 (5 ,7) , r3 ; r1 . displayInfo (); r2 . displayInfo (); r3 . displayInfo (); // r1 = r1 + r2; // r1.displayInfo(); r3=r1+r2 ; r3 . displayInfo (); r1 . printArea (); r2 . printArea (); r3 . printArea (); return 0; }
/*C++ program that overloads the (*) operator as non-member function and (/) as a member function of the Rectangle class.*/
//Demonstrate the class in the main function
//Rectangle.cpp
#include <iostream >
using namespace std ;
//Rectangle class
class Rectangle
{
private :
int width , length ;
public :
Rectangle ();
Rectangle ( int w, int l );
void printArea ();
void displayInfo ();
Rectangle operator +(Rectangle& r );
//non-member friend function
friend Rectangle operator + (Rectangle &r1, const
Rectangle& r2);
//non-member friend function
friend Rectangle operator * (Rectangle &r1, const
Rectangle& r2);
//member friend function
Rectangle operator / (Rectangle
&r);
}; //end of the class ,Rectangle
//non-member friend function to overload addition (+)
operator
Rectangle operator + (Rectangle &r1, const Rectangle&
r2)
{
Rectangle add;
add.length=r1.length+r2.length;
add.width=r1.width+r2.width;
return add;
}
//non-member friend function to overload multiply (*)
operator
Rectangle operator * (Rectangle &r1, const Rectangle&
r2)
{
Rectangle multiply;
multiply.length=r1.length*r2.length;
multiply.width=r1.width*r2.width;
return multiply;
}
//member friend function to overload division (/)
operator
Rectangle Rectangle ::operator / (Rectangle &r)
{
Rectangle temp;
temp.length=length/r.length;
temp.width=width/r.width;
return temp;
}
/************ Implementation Section ******************/
Rectangle :: Rectangle ()
{
width = 1, length = 2;
}
Rectangle :: Rectangle ( int w, int l )
{
width = w, length = l ;
}
void Rectangle :: printArea ()
{
cout <<"Area of "<< this <<" =
"<< width*length << endl ;
}
void Rectangle :: displayInfo ()
{
cout << "I am a " << width
<<"x"<< length <<" rectangle "
<< "with id " << this
<< endl ;
}
Rectangle Rectangle :: operator +( Rectangle& r )
{
Rectangle temp ;
temp.width = this->width + r.width ;
temp.length = this->length + r.length ;
return temp;
}
/***************** Main Section
*********************/
int main ()
{
Rectangle r1(2,4) , r2 (2 ,4) , r3 ;
r1 . displayInfo ();
r2 . displayInfo ();
r3 . displayInfo ();
cout<<"Overloading mutltiply(*) non-
member operator "<<endl;
r3 = r1 * r2;
r3.displayInfo();
cout<<"Overloading division(/) member operator
"<<endl;
r3=r1/r2;
r3.displayInfo();
cin.get();
cin.ignore();
return 0;
}
Sample output screenshot: