Friday 31 May 2013

Type Conversion in C++


Meaning of Type Conversion

  • Consider the following segment of code:
void main()
{
   int x;
   float y=3.25;
   x=y;
   …
}

  • Here, x and y are of different types but during assignment of content of y into x, type of the content of y automatically converted into an int type value.
  • We can say compiler supports automatic type conversion for primitive data types.
  • The problem arises when at least one of the variables is of user defined data type.
  • We categorized the situation into three branches:
    • Basic type to class type
    • Class type to basic type
    • One class type to another class type

Basic type to Class type

#include<iostream.h>
class complex
{
     private:
            int a,b;
     public:
            complex() { } //default constructor
            complex(int x) {a=x; b=x; } //used in type conversion
            void showdata();        
            void getdata();
};
void complex:: getdata()
{
            cout<<”Enter two numbers”;
            cin>>a>>b;
}
void complex:: showdata()
{   cout<<”a=”<<a<<”b=”<<b;  }
void main()
{
  complex c1;
  c1.showdata();
  int k=5;
  c1=k;  //basic type to class type
  c1.showdata();
}

  • Here, notice the line c1=k; in function main(). The value of k which is an integer, assigned to an object c1 of type complex. This leads to an error due to type difference.
  • The problem can be solved with constructor. We studied earlier that a constructor is called automatically when an object is created. In addition to this fact, constructor also called when we try to assign a value other than object’s type.
  • So we need to define a constructor which takes one argument of type int.
  • Observe that we have created two constructors in complex class. One is default constructor (called during object creation) and another is parameterized constructor ( called during type conversion)
  • The value of k is passed to the constructor which is received by variable x, further this value is transferred to variables a and b.

Class type to basic type

#include<iostream.h>
class complex
{
     private:
            int a,b;
     public:
            void getdata();
            void showdata();        
            operator int()
            { return(a+b); }
};
void complex:: getdata()
{
            cout<<”Enter two numbers”;
            cin>>a>>b;
}
void complex:: showdata()
{   cout<<”a=”<<a<<”b=”<<b;  }
void main()
{
  complex c1;
  c1.getdata();
  c1.showdata();
  int k;
  k=c1;  //class type to basic type
  cout<<”k=”<<k;
}

  • Here, object c1 of class complex is assigned to an int type variable k.
  • Notice the line k=c1; c1 has to convert into an int type to store in variable k.
  • This can not be done with the help of constructor as object is on the right side of assignment operator.
  • This problem can be solved with casting operator.
  • Observe casting operator in class complex. Definition starts from keyword operator followed by data type (it should be according to the type of variable in the left side of assignment, in our example it is int as k=c1) and again followed by empty parenthesis. Body of casting operator is similar to body of any function.
  •  Since k can contain only one value of type int, there should be some mechanism or logic (according to the programming need) to manipulate content of object c1 into a single value. Here we add a and b, result is then returned, which is received by variable k in function main.

One class type to another class type

#include<iostream.h>
class item
{
    int m,n;
  public:
    void showdata()
    { cout<<”m=”<<m<<”n=”<<n; }
     void getm(int x) { m=x; }
     void getn (int y)  { n=y; }
  };
class complex
{
     private:
            int a,b;
     public:
            void showdata();        
{   cout<<”a=”<<a<<”b=”<<b;  }
            void complex:: getdata()
{
              cout<<”Enter two numbers”;
              cin>>a>>b;
}
operator item()
{
  item i;
  i.getm(a);
  i.getn(b);
  return(i);
};
void main()
{
  complex c1;
  item i1;
  c1.getdata();
  c1.showdata();
  i1=c1;   //One class type to another class type
  i1.showdata();
}

  • If we have to convert one class type data into another class type, then this can be done either with the help of constructors or operator overloading.
  • In the above example we are using casting operator.
  • Casting operator in class complex is called for object c1 (see line i1=c1 in main)
  • We have created an object  i of type item in casting operator, set values of variables of object i. Lastly we returned object i, which is then received by i1 in function main. 

No comments:

Post a Comment