Write a C++ program to create class currency using operator overloading:
Create a class currency containing rupees and paisa as data members.
Write a necessary member function using operator overloading for the following.
1.currency(long int rup=0,int paisa=0)
2.currency &operator +=(currency &)to add one currency to another
3.currency & operator -=(currency &) to subtracts one currency from another accept rupees & paisa from user and display it.
#include
#include
class currency
{
int r,p;
public:currency(long int rs=0,int paisa=0)
{
r=rs;
p=paisa;
}
currency & operator +=(currency &);
currency & operator -=(currency &);
};
currency & currency :: operator +=(currency &c2)
{
int a,b;
a=r+c2.r;
b=p+c2.p;
if(b>=100)
{
b=b-100;
a=a+1;
}
cout<<"\n Addition of two currency is:-";
cout<<"\n Rupees:-"<<a;
cout<<"\n Paisa:-"<<b;
return *this;
}
currency & currency:: operator -=(currency &c2)
{
int a,b;
a=r-c2.r;
b=p-c2.p;
cout<<"\n Subtraction of two currency is:-";
cout<<"\n Rupees:-"<<a;
cout<<"\n Paisa:-"<<b;
return *this;
}
int main()
{
int rs,paisa;
clrscr();
cout<<"\n\t\t Accept two currency \n\n";
cout<<"\n Enter rupees:-";
cin>>rs;
cout<<"Enter paisa";
cin>>paisa;
currency c1(rs,paisa);
cout<<"\n Enter rupees:-";
cin>>rs;
cout<<"Enter paisa";
cin>>paisa;
currency c2(rs,paisa);
c1+=c2;
c1-=c2;
getch();
return 0;
}
===================
OUTPUT:-
Accept two currency
Enter rupees:-12
Enter paisa34
Enter rupees:-45
Enter paisa34
Addition of two currency is:-
Rupees:-57
Paisa:-68
Subtractions of two currency is:-
rupees:--33
paisa:-0
Create a class currency containing rupees and paisa as data members.
Write a necessary member function using operator overloading for the following.
1.currency(long int rup=0,int paisa=0)
2.currency &operator +=(currency &)to add one currency to another
3.currency & operator -=(currency &) to subtracts one currency from another accept rupees & paisa from user and display it.
#include
#include
class currency
{
int r,p;
public:currency(long int rs=0,int paisa=0)
{
r=rs;
p=paisa;
}
currency & operator +=(currency &);
currency & operator -=(currency &);
};
currency & currency :: operator +=(currency &c2)
{
int a,b;
a=r+c2.r;
b=p+c2.p;
if(b>=100)
{
b=b-100;
a=a+1;
}
cout<<"\n Addition of two currency is:-";
cout<<"\n Rupees:-"<<a;
cout<<"\n Paisa:-"<<b;
return *this;
}
currency & currency:: operator -=(currency &c2)
{
int a,b;
a=r-c2.r;
b=p-c2.p;
cout<<"\n Subtraction of two currency is:-";
cout<<"\n Rupees:-"<<a;
cout<<"\n Paisa:-"<<b;
return *this;
}
int main()
{
int rs,paisa;
clrscr();
cout<<"\n\t\t Accept two currency \n\n";
cout<<"\n Enter rupees:-";
cin>>rs;
cout<<"Enter paisa";
cin>>paisa;
currency c1(rs,paisa);
cout<<"\n Enter rupees:-";
cin>>rs;
cout<<"Enter paisa";
cin>>paisa;
currency c2(rs,paisa);
c1+=c2;
c1-=c2;
getch();
return 0;
}
===================
OUTPUT:-
Accept two currency
Enter rupees:-12
Enter paisa34
Enter rupees:-45
Enter paisa34
Addition of two currency is:-
Rupees:-57
Paisa:-68
Subtractions of two currency is:-
rupees:--33
paisa:-0