The C++ operator overloading program example with class object

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: An addition, constant addition and multiplication operations for class object to demonstrate the C++ operator overloading in C++ programming

To show: How to overload the operators for the C++ class object operator overloading in C++ programming

 

// the C++ operator overloading with friend keyword of the class object

#include <iostream>

using namespace std;

 

// class declaration part

class wall

{

public:

int length;

int width;

public:

void set(int l, int w) {length = l; width = w;}

int get_area(void) {return length * width;}

// operator overloading, add two walls

friend wall operator + (wall aa, wall bb);

// add a constant to a wall

friend wall operator + (int aa, wall bb);

// multiply a wall by a constant

friend wall operator * (int aa, wall bb);

};

 

// class implementations

// add two walls widths together

wall operator + (wall aa, wall bb)

{

wall temp;

temp.length = aa.length;

temp.width = aa.width + bb.width;

return temp;

}

 

// add a constant to wall

wall operator + (int aa, wall bb)

{

wall temp;

temp.length = bb.length;

temp.width = aa + bb.width;

return temp;

}

 

// multiply wall by a constant

wall operator * (int aa, wall bb)

{

wall temp;

temp.length = aa * bb.length;

temp.width = aa * bb.width;

return temp;

}

 

void main(void)

{

wall small, medium, large;

wall temp;

small.set(2,4);

medium.set(5,6);

large.set(8,10);

 

cout<<"Normal values"<<endl;

cout<<"-------------"<<endl;

cout<<"Area of the small wall surface is "<<small.get_area()<<endl;

cout<<"Area of the medium wall surface is "<<medium.get_area()<<endl;

cout<<"Area of the large wall surface is "<<large.get_area()<<endl<<endl;

 

cout<<"Overload the operators!"<<endl;

cout<<"-----------------------"<<endl;

 

temp = small + medium;

 

cout<<"New value, 2 * (4 + 6)"<<endl;

cout<<"New area of the small wall surface is "<<temp.get_area()<<endl<<endl;

cout<<"New value, 2 * (10 + 4) "<<endl;

 

temp = 10 + small;

 

cout<<"New area of the medium wall surface is "<<temp.get_area()<<endl<<endl;

cout<<"New value, (4 * 8) * (4 * 10)"<<endl;

 

temp = 4 * large;

 

cout<<"New area of the large wall surface is "<<temp.get_area()<<endl<<endl;

 

return;

}

 

Output example:

 

Normal values

-------------

Area of the small wall surface is 8

Area of the medium wall surface is 30

Area of the large wall surface is 80

Overload the operators!

-----------------------

New value, 2 * (4 + 6)

New area of the small wall surface is 20

New value, 2 * (10 + 4)

New area of the medium wall surface is 28

New value, (4 * 8) * (4 * 10)

New area of the large wall surface is 1280

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index