C++ program for typecast from one class type to another class type (routine in source object)

//C++ program to convert Indian Currency into Nepalese Currency
//two classes Indian_Currency and Nepalese_Currency have data members Rs and Paisa.
//Here, conversion routine is in source object

#include <iostream>
using namespace std;
class Nepalese_Currency
{
    int Rs;
    float Paisa;
    public:
        Nepalese_Currency()
        {
            Rs=0;
            Paisa=0;
        }
        Nepalese_Currency(int r,float p)
        {
            Rs=r;
            Paisa=p;
        }
        void show()
        {
            cout<<Rs<<" Rs "<<Paisa<<" Paisa "<<"(Nepali)"<<endl;
        }
};
class Indian_Currency
{
    int Rs,Paisa;
    public:
        Indian_Currency()
        {
            Rs=0;
            Paisa=0;
        }
        Indian_Currency(int r,int p)
        {
            Rs=r;
            Paisa=p;
        }
        void show()
        {
            cout<<Rs<<" Rs "<<Paisa<<" Paisa "<<"(Indian)"<<endl;
        }
        operator Nepalese_Currency()
//conversion routine is in source object
        {
            int NRs;
            float NPaisa,total_NRs;
            total_NRs=(Rs+Paisa/100)*1.6;
            NRs=(int)total_NRs;
            NPaisa=(total_NRs-NRs)*100;
            return Nepalese_Currency(NRs,NPaisa);
        }
};
int main()
{
    Indian_Currency i(12,50);
    i.show();
    Nepalese_Currency n;
    n=i;
    n.show();
    return 0;
}

No comments:

Post a Comment