C++ Data Conversion

Data conversion in C++ includes conversions between basic types and user-defined types, and conversions between different user-defined types.

The assignments between types, whether they are basic types or user-defined types, are handled by the compiler with no effort on our part, provided that the same data type is used on both sides of the equal sign.

The different possible data conversion situtaions are:

1. Conversion between basic and user defined

a) Conversion from basic to user defined data type
b) Conversion from user-defined data type to basic data type

2. Conversion between user defined data types

a) Conversion in destination object
b) Conversion in source object

Lets look at each of them.

 

1. Conversion between basic and user defined

(a) Conversion from basic to user defined data type

Conversion from basic to user defined type is done by using the constructor function with one argument of basic type as follows.

Syntax:

class class_name { 
   private: 
      //…. 
   public: 
      class_name ( data_type) { 
          // conversion code 
   } 
};

Here is program that illustrates this conversion. 

//example 
//conversion from basic type to object
#include<iostream>
using namespace std;
class celsius
{
private:
    float temper;
public:
    celsius()
    {
        temper=0;
    } celsius(float ftmp)
    {
        temper=(ftmp-32)* 5/9;
    }
    void showtemper()
    {
        cout<<"Temperature in Celsius: "<<temper;
    }
};
int main()
{
    celsius cel;                //cel is user defined
    float fer;                  //fer is basic type
    cout<<"\nEnter temperature in Fahrenheit measurement: ";
    cin>>fer;
    cel=fer;                    //convert from basic to user-defined; //eqvt to  cel = celsius(fer);
    cel.showtemper();
    return 0;
}

The output of the program is:

Enter temperature in Fahrenheit measurement: -40 
Temperature in Celsius: -40

 

(b) Conversion from user-defined type to basic data type

Conversion from user-defined type of basic data type is done by overloading the cast operator of basic type as a member function. 

Operator function is defined as an overloaded basic data type which takes no arguments.

Return type of operator is not specified because the cast operator function itself specifies the return type.

Syntax:

class class_name { 
       ... 
       public: 
             operator data_type() { 
                    //Conversion code 
             } 
};

Here is example program to illustrate conversion from user-defined type of basic data type.

//conversion from user to basic type
#include<iostream>
using namespace std;
class celsius
{
private:
    float temper;
public:
    celsius()
    {
        temper=0;
    }
    operator float()
    {
        float fer;
        fer=temper *9/5 + 32;
        return (fer);
    }
    void gettemper()
    {
        cout<<"\n Enter Temperature in Celsius:";
        cin>>temper;
    }
};
int main()
{
    celsius cel;            //cel is user defined
    float fer;              //fer is basic type
    cel.gettemper();
    fer=cel;                //convert from user-defined to basic;
                            //eqvt to  fer= float(cel);
    cout<<"\nTemperature in Fahrenheit measurement: "<<fer;
}

The output of the program is:

Enter Temperature in Celsius: -40 
Temperature in Fahrenheit measurement: -40

 

1. Conversion between basic and user defined

(a) Conversion in Destination Object

This conversion is exactly like conversion of basic to user defined data type i.e. one argument constructor is used.

Conversion routine is defined as a one argument constructor in destination class and takes the argument of the source class type.

Eg.

classA objA;
classB objB;
objA=objB;

Here, objB is the source of the class classB and objA is the destination object of class classA.

For this expression to work, the conversion routine should exist in classA (destination class type) as a one argument constructor as

//source object class
class classB   
{
    //body of classB
}

//destination object class
class classA
{
private:
    //….
public:
    classA (classB objB) //object of source class
    {
        //code for conversion
        //from classB
        //to classA
    }
};

 

(b) Conversion in source object

This conversion is exactly like conversion of user-defined type to basic type i.e. overloading the cast operator is used.

Eg.

classA objA; 
classB objB; 
objA=objB;

Here, objB is the source of the class classB and objA is the destination object of class classA. Conversion routine is specified as conversion (cast) operator overloading for the type of destination class.

Syntax:

//destination object class
class classA   
{
    //body of classA
}
//source object class
class classB
{
private: 
    //….
public:
    operator classA () //cast operator destination types
    {
        //code for conversion from classB
        //to classA
    }
};

Here is an example program.

//example
//conversion from user defined to user defined
//conversion routine in source class
#include<iostream>
#include<cmath>
using namespace std;
class Cartesian
{
private:
    float xco, yco;
public:
    Cartesian()
    {
        xco=0;
        yco=0;
    }
    Cartesian(float x, float y)
    {
        xco=x;
        yco=y;
    }
    void display()
    {
        cout<<"("<<xco<<","<<yco<<")";
    }
};
class Polar
{
private:
    float radius, angle;
public:
    Polar()
    {
        radius=0;
        angle =0;
    }
    Polar(float rad, float ang)
    {
        radius =rad;
        angle=ang;
    }

    operator Cartesian()
    {
        float x=static_cast<int>(radius * cos(angle));
        float y=static_cast<int>(radius * sin(angle));
        return Cartesian(x,y);
    }
    void display()
    {
        cout<<"("<<radius<<","<<angle<<")";
    }
};
int main()
{
    Polar pol(10.0, 0.78);
    Cartesian cart;
    cart=pol;
    cout<<"\nGiven Polar: ";
    pol.display();
    cout<<"\nEquivalent cartesian: ";
    cart.display();
    return 0;
}

The output of the program is:

Give Polar : (10.0, 0.78)
Equivalent cartesian: (7,7)
C++ Operator Overloading
C++ Inheritance