Binary operator program using object oriented programing

c++

Program:

Define + binary operator for the counter class that should allow the statements like: c3=c1+c2  where c1,c2 and c3 are instances of class counter. c3 will have its own serial number but value as sum of count values for objects c1 and c2.

#include<iostream>

using namespace std;

class counter

{

int count;

public:

counter()

{

count=0;

}

 

int get_count()

{

return count;

}

counter operator ++(int)

{

counter temp;

temp.count=count++;

return temp;

}

counter operator ++()

{

counter temp;

temp.count=++count;

return temp;

}

 

counter operator +(counter c2_copy)

{

counter temp;

temp.count=count+c2_copy.count;

return temp;

}

};

 

int main()

{

counter c1,c2,c3;

c2=++c1;

c2=c1++;

cout<<“\nAfter increment c1 value is:\n”<<c1.get_count()<<“\n\n”;

cout<<“\nAfter increment c2 value is:\n”<<c2.get_count()<<“\n\n”;

 

c3=c1+c2;

cout<<“\nAdition is:\n”<<c3.get_count()<<“\n\n”;

 

}

Your comment are valueable