Example:6
Add two objects using the overloaded operator “+=”. with the friend function.
// to overload += operator with friend function
// add two objects
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
int a;
public:
sample();
sample(int n);
void get();
void disp();
friend sample operator+=(sample s1,sample s2);
};
sample::sample()
{
a=0;
}
sample::sample(int n)
{
a=n;
}
void sample::get()
{
cout<<"Enter the value of a ";
cin>>a;
}
void sample::disp()
{
cout<<"a= "<<a<<endl;
}
sample operator+=(sample s1,sample s2)
{
sample t;
t.a=(s1.a+=s2.a);
return(t);
}
int main()
{
sample s1,s2,s3;
s1.get();s2.get();
s1.disp();s2.disp();
s3=(s1+=s2); //s3=opertor+=(s1,s2);
s3.disp();
getch();
return(0);
}
Output:
Enter the value of a 10
Enter the value of a 25
a= 10
a= 25
a= 35
Example:7
Multiply two objects using overloaded operator “*=”. with friend function.
code
Output:
Example:8
Using overloaded operator “+=” add a value to the objects data element
// overload += operator
// to add a value to the data element of an object
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
int a;
public:
sample();
sample(int n);
void get();
void disp();
void operator+=(int n);
};
sample::sample()
{
a=0;
}
sample::sample(int n)
{
a=n;
}
void sample::get()
{
cout<<"Enter the value of a ";
cin>>a;
}
void sample::disp()
{
cout<<"a= "<<a<<endl;
}
void sample::operator+=(int n)
{
a+=n;
}
int main()
{
sample s1;
int value;
s1.get();s1.disp();
cout<<"Enter the value to add ";
cin>>value;
s1+=value; //s1.operator+=(value);
s1.disp();
getch();
return(0);
}
Output:
Enter the value of a 10
a= 10
Enter the value to add 25
a= 35
Example:9
Using overloaded operator
“+=” : add a value to the objects data element
“*=”: calculate the product of two objects
“-=” : calculate the difference between two objects
// to overload += operator
// to add a value to an object value
// subtract a value
// multiple by a value
#include<stdio.h>
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
int a;
public:
sample();
sample(int n);
~sample();
void get();
void disp();
void operator+=(int n);
void operator-=(int n);
void operator*=(int n);
};
sample::sample()
{
a=0;
}
sample::sample(int n)
{
a=n;
}
sample::~sample()
{
}
void sample::get()
{
cout<<"Enter the value of a ";
cin>>a;
}
void sample::disp()
{
cout<<"a= "<<a<<endl;
}
void sample::operator+=(int n)
{
a+=n;
}
void sample::operator-=(int n)
{
a-=n;
}
void sample::operator*=(int n)
{
a*=n;
}
int main()
{
sample s1;
int value;
// to add
s1.get();
cout<<"s1 is "<<endl;
s1.disp();
cout<<"Enter the value to add ";
cin>>value;
s1+=value;
cout<<"s1 is "<<endl;
s1.disp();
// to sub
s1.get();
cout<<"s1 is "<<endl;
s1.disp();
cout<<"Enter the value to sub ";
cin>>value;
s1-=value;
cout<<"s1 is "<<endl;
s1.disp();
// to mul
s1.get();
cout<<"s1 is "<<endl;
s1.disp();
cout<<"Enter the value to mul with ";
cin>>value;
s1*=value;
cout<<"s1 is "<<endl;
s1.disp();
getch();
return(0);
}
Output:
Enter the value of a 10
s1 is
a= 10
Enter the value to add 500
s1 is
a= 510
Enter the value of a 10
s1 is
a= 10
Enter the value to sub 5
s1 is
a= 5
Enter the value of a 50
s1 is
a= 50
Enter the value to mul with 5
s1 is
a= 250




