C++ program for typecast from basic data type to class type

//type cast W=F where F is float value in kg and W is an object of Weight
//Weight has kg and g as data members

#include <iostream>
using namespace std;
class Weight
{
    int kg;
    float g;
    public:
        Weight()
        {
            kg=g=0;
        }
        Weight(float f)
        {
            kg=int(f);
            g=(f-kg)*1000;
        }
        display()
        {
            cout<<kg<<" kg "<<g<<" gm";
        }
};
int main()
{
    Weight W;
    float F;
    cout<<"Enter weight: ";
    cin>>F;
    W=F;
//same as W(F).
    cout<<"The kg equivalent object is: "<<endl;
    W.display();
    return 0;
}

No comments:

Post a Comment