Type Conversion | C++

Type Conversion in C++


There are type of situation arise in data conversion between uncompatable type.
  1. Type I : Conversion from built in type (BDT-basic data type) to built in type (BDT-basic data type)
  2. Type II : Conversion from built in type (BDT-basic data type) to class type (UDT-user define data type)
  3. Type III : Conversion from class type (UDT-user define data type) to built in type (BDT-basic data type)
  4. Type IV : Conversion from class type (UDT-user define data type) to class type (UDT-user define data type)

Type I : Conversion from built in type (BDT-basic data type) to built in type (BDT-basic data type)


This type of conversion is done automatically.

Type II : Conversion from built in type (BDT-basic data type) to class type (UDT-user define data type)


Here if we want to convert BDT to UDT than some additional overloaded conversion function takes place in destination class. such as:
 
operator int()  
    {              
       return(a); 
    }

Example

Copy Code
#include <iostream>

using namespace std;
class source
{
    int a;
    public:
    source()
    {
        a=0;
    }
    source(int x)
    {
        a=x;
    }
    
    operator int() // for type conversion obj s1= int r; in main here int is converted into source  
    {              // for BDT to UDT 
       return(a); 
    }
    
    ~source(){ }
};


int main()
{
   source s1; 
   int r=10;
   s1=r; 
   cout<<s1;
   return 0;
}

Output:

10


Type III : Conversion from class type (UDT-user define data type) to built in type (BDT-basic data type)


Here if we want to convert BDT to UDT than some additional overloaded conversion function takes place in source class. such as:

operator int()  
    {              
       return(a); 
    }

Example

Copy Code
#include <iostream>

using namespace std;
class source
{
    int a;
    public:
    source()
    {
        a=0;
    }
    source(int x)
    {
        a=x;
    }
    
    operator int() // for type conversion  int r= s1 (object) in main here source is converted into int
    {              // for UDT to BDT conversion
       return(a); 
    }
    int geta()
    {
        return(a);
    }
    
      ~source(){ }
};


int main()
{
  source s1;
  int r=s1;
  cout<<s1;
  return 0;
}

Output:

0

Type IV : Conversion from class type (UDT-user define data type) to class type (UDT-user define data type)


Case I: For d = s

If source is the source class and dest is the destination class than
We can write d = s in main() after adding some conversion function in source class and overload conversion function in destination class such as
In Source class function is
 
int geta()
    {
        return(a);
    }

In destination class overload function is
 
dest (source obj)
    {
        p=obj.geta();
    }
Note:This overloaded conversion function is used when we want to access the private data member of source class indirectly.
Indirectly means we can not access private data member of source class. So we access conversion function of source class which is define in source class.
It must be noted that those class which define first is called source class like source and dest class is define after source class so it is a destination class.

Example

Copy Code
#include <iostream>
using namespace std;

class source
{
    int a;
    public:
    source()
    {
        a=0;
    }
    source(int x)
    {
        a=x;
    }
        
    int geta()
    {
        return(a);
    }
     
    ~source(){ }
};

class dest
{
    int p;
    public:
    dest()
    {
        p=0;
    }
    dest(int x)
    {
        p=x;
    }
    operator int()  
    {              
       return(p); 
    }
    
    dest (source obj)
    {
        p=obj.geta();
    }
    
    ~dest(){ }
};


int main()
{
  source s1;
  dest d1;
  d1=s1; // convert from s1 (source type) to d1(dest type)
  cout<<d1;
  return 0;
}

Output:

0

Case II: For s = d

If the source class is dest and destination class is source than conversion function in source class and overload conversion function in destination class will be
In Source class function is
 
int getp()
    {
        return(p);
    }

In destination class overload function is
 
dest (source obj)
    {
        a=obj.getp();
    }

Output:

0

Example

Copy Code
#include <iostream>
using namespace std;


class dest
{
    int p;
    public:
    dest()
    {
        p=0;
    }
    dest(int x)
    {
        p=x;
    }
    
    int getp()
    {
        return(p);
    } 
    
    ~dest(){ }
};

class source
{
    int a;
    public:
    source()
    {
        a=0;
    }
    source(int x)
    {
        a=x;
    }
    operator int()
    {
        return(a);
    }
    
    source (dest obj)
    {
        a=obj.getp();
    }
    ~source(){ }
};

int main()
{
  source s1;
  dest d1;
  s1=d1; // convert from d1 (source type) to s1(dest type)
  cout<<s1;
  return 0;
}

Output:

0

No comments:

Post a Comment