Wednesday, January 20, 2016

TYPE CONVERSION IN C++

TYPE CONVERSIONS

We have overloaded several kinds of operators but we haven’t considered the assignment operator (=). It is a very special operator having complex properties. We know that = operator assigns values form one variable to another or assigns the value of user defined object to another of the same type. For example,

int     x, y ;

x = 100;

y = x;

Here, first 100 is assigned to x and then x to y.

Consider another statement, t3 = t1 + t2;

This statement used in program 11.2 earlier, assigns the result of addition, which is of type time to object t3 also of type time.
So the assignments between basic types or user defined types are taken care by the compiler provided the data type on both sides of = are of same type.

But what to do in case the variables are of different types on both sides of the = operator? In this case we need to tell to the compiler for the solution.

Three types of situations might arise for data conversion between different types :

(i)            Conversion form basic type to class type.

(ii)           Conversion from class type to basic type.

(iii)          Conversion from one class type to another class type. Now let us discuss the above three cases :

(i) Basic Type to Class Type

This type of conversion is very easy. For example, the following code segment converts an int type to a class type.

class distance

{

int feet;

int inches;

public:

.....

.....

distance (int dist) //constructor

{

feet = dist/12;

inches = dist%12;

}

};

The following conversion statements can be coded in a function :

distance dist1; //object dist1 created

int length = 20;


dist1=length; //int to class type

After the execution of above statements, the feet member of dist1 will have a value of 1 and inches member a value of 8, meaning 1 feet and 8 inches.

A class object has been used as the left hand operand of = operator, so the type conversion can also be done by using an overloaded = operator in C++.

(ii) Class Type to Basic Type

For conversion from a basic type to class type, the constructors can be used. But for conversion from a class type to basic type constructors do not help at all. In C++, we have to define an overloaded casting operator that helps in converting a class type to a basic type.

The syntax of the conversion function is given below:

Operator typename()

{

.......

....... //statements

}

Here, the function converts a class type data to typename. For example, the operator float ( ) converts a class type to type float, the operator int ( ) converts a class type object to type int. For example,

matrix :: operator float ()

{

float sum = 0.0; for(int i=0;i<m;i++)

{

for (int j=0; j<n; j++) sum=sum+a[i][j]*a[i][j];

}

Return sqrt(sum); //norm of the matrix

}


Here, the function finds the norm of the matrix (Norm is the square root of the sum of the squares of the matrix elements). We can use the operator float ( ) as given below :

float norm = float (arr);

or

float norm = arr;

where arr is an object of type matrix. When a class type to a basic type conversion is required, the compiler will call the casting operator function for performing this task.

The following conditions should be satisfied by the casting operator function :

(a)  It must not have any argument

(b)  It must be a class member

(c)  It must not specify a return type.



(i)            One Class Type to Another Class Type

There may be some situations when we want to convert one class type data to another class type data. For example,

Obj2 = obj1; //different type of objects

Suppose obj1 is an object of class studdata and obj2 is that of class result. We are converting the class studdata data type to class result type data and the value is assigned to obj2. Here studdata is known as source class and result is known as the destination class.

The above conversion can be performed in two ways :

(a)  Using a constructor.

(b)  Using a conversion function.

When we need to convert a class, a casting operator function can be used i.e. source class. The source class performs the conversion and result is given to the object of destination class.

If we take a single-argument constructor function for converting the argument’s type to the class type (whose member it is). So the argument is of the source class and being passed to the destination class for the purpose of conversion. Therefore it is compulsory that the conversion constructor be kept in the destination class.

Example:
# include <iostream.h>
# include <conio.h>
class in1
{
    int code,items;
    float price;
    public:
        in1(int a,int b,int c)
        {
            code=a;
            items=b;
            price=c;
        }
        void putdata()
        {
            cout<<"CODE= "<<code<<endl;
            cout<<"ITEMS= "<<items<<endl;
            cout<<"VALUE= "<<price<<endl;
        }
        int getcode()
        {
            return code;
        }
        int getitems()
        {
            return items;
        }
        int getprice()
        {
            return price;
        }
        Operator float ()
        {
             return items*price;
        }
};

class in2
{
    int code;
    floatvalue;
    public:
    in2()
    {
        code=0;
        value=0;
    }
    in2(int x,float y)
    {
        code=x;
        value=y;
    }
    void putdata()
    {
        cout<<"CODE= "<<code<<endl;
        cout<<"VALUE= "<<value<<endl;
    }
    in2(in1 p)
    {
        code=p.getcode();
        value=p.getitems()*p.getprice();
    }
};

main()
{
clrscr();
    in1 s1(100,5,140.0);
    float tot_value;
    in2 d1;
    tot_value=s1;
    d1=in1(s1);
    cout<<"PRODUCT DETAILS INVENT-1 TYPES:->"<<endl;
    s1.putdata();
    cout<<"STOCK VALUE"<<endl;
    cout<<"VALUE= "<<tot_value<<endl;
    cout<<"PRODUCT DETAILS INVENT-2 TYPES:->"<<endl;
    d1.putdata();
}

OUTPUT:
PRODUCT DETAILS INVENT-1 TYPES:->
CODE= 100
ITEMS= 5
VALUE= 140
STOCK VALUE
VALUE= 700
PRODUCT DETAILS INVENT-2 TYPES:->
CODE= 100
VALUE= 700


No comments:

Post a Comment