Create a class currency containing rupees and paise as data members. Write necessary member functions using operator overloading for the following: 1. currency (long int rup=0,int paise=0) 2. currency & operator += (currency &) (to add one currency to another) 3. currency & operator - = (currency &) (to subtract one currency from another) Accept Rupee & paise from user and display it.


#include<iostream.h>
#include<conio.h>

class currency
{
  long int rup;
  int paise;

  public:
  void getcurrency()
  {
    cout<<endl<<"Enter currency in rupees and paise: ";
    cin>>rup>>paise;
  }

  currency operator+(currency &c)
  {
    currency temp;
    temp.rup=rup+c.rup;
    temp.paise=paise+c.paise;
    return temp;
  }

  currency operator-(currency &c)
  {
    currency temp;
    temp.rup=rup-c.rup;
    temp.paise=paise-c.paise;
    return temp;
  }

  void display()
  {
    cout<<rup<<" rupees and "<<paise<<" paise";
  }
};//end of class

int main()
{
clrscr();
currency c1;
c1.getcurrency();
currency c2;
c2.getcurrency();
cout<<endl<<"Currency1 is: ";
c1.display();
cout<<endl<<"Currency2 is: ";
c2.display();
currency c3=c1+c2;
cout<<endl<<"Currency1 + Currency2 is: ";
c3.display();
currency c4=c1-c2;
cout<<endl<<"Currency1 - Currency2 is: ";
c4.display();
getch();
return 0;
}

No comments:

Post a Comment