google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Write a C++ program to create class currency using operator overloading

Thursday, February 26, 2015

Write a C++ program to create class currency using operator overloading

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

रामायण

रामायण दशरथ की तीन पत्नियाँ – कौशल्या, सुमित्रा , कैकेयी दशरथ के चार पुत्र – राम,लक्ष्मण,भरत,शत्रुघ्न दशरथ: राम के पिता और कौशल के राजा कौशल...