Thursday 16 March 2017

Class to Class Type Conversion -through Casting Operator Function (Overload Destination Class in Source Class)
#include<iostream>
#include<iomanip>
using namespace std;
class KG;
class POUND
{
private:
float lb;
public:
POUND(){}
void get();
void put();
float & get_lb(){ return lb; } // reference function for lb member
};
class KG
{
private:
float kg;
public:
KG(){}
void get();
void put();
operator POUND()
{
POUND P1;
P1.get_lb() =kg/2.20462;
return P1;
}
};
void KG::get()
{
cout<<"enter value of kg:";
cin>>kg;
}
void KG::put()
{
cout<<"kg = "<<kg<<endl;
}
void POUND::get()
{
cout<<"enter value of lb:";
cin>>lb;
}
void POUND::put()
{
cout<<"lb = "<<lb<<endl;
}
int main()
{
KG K;
K.get();
POUND P;
//P.get();
P=K;
P.put();
return 0;
}

No comments:

Post a Comment