Tuesday, 9 June 2015

Program to illusrate data conversion b/w built-in data types and user defined data types (int & float)



Code for Program to illusrate data conversion b/w built-in data types and user defined data types (int & float) in C++ Programming



 



 



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


 /*************************************************************************///-----------------------------  distance  ------------------------------///*************************************************************************/class distance
    {
       private:
        int feet;
        float inches;

       public:
        distance()
           {
              feet=0;
              inches=0;
           }

        distance(float meter)
           {
              float f=3*meter;
              feet=f;
              inches=12*(f-feet);
           }

        distance(int f,float i)
           {
              feet=f;
              inches=i;
           }

        void show_distance()
           {
              cout<<"\t Value of feet = "<<feet<<endl;
              cout<<"\t Value of inches = "<<inches<<endl;
           }

        operatorfloat()
           {
              float m=inches/12;
              m+=(feet/3);
              return m;
           }
    };

 /*************************************************************************//*************************************************************************///-----------------------------  Main( )  -------------------------------///*************************************************************************//*************************************************************************/

 main()
    {
       clrscr();

       distance d_1(2.35);
       distance d_2;

       cout<<"\n ***** Basic to User ***** \n"<<endl;

       cout<<" Value of distance d_1 is : "<<endl;
       d_1.show_distance();

       d_2=1.00;

       cout<<"\n Value of distance d_2 is : "<<endl;
       d_2.show_distance();


       distance d_3(5,10.25);

       float m=float(d_3);

       cout<<"\n ***** User to Basic ***** \n"<<endl;

       cout<<" Value of distance d_3 is : "<<m<<endl;

       m=d_2;

       cout<<"\n Value of distance d_2 is : "<<m<<endl;

       getch();
       return 0;
    }

No comments:

Post a Comment