Open In App

How to convert a class to another class type in C++?

Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Type Conversion in C++, Advanced C++ | Conversion Operators
Through class conversion, one can assign data that belongs to a particular class type to an object that belongs to another class type. 
Example: 
Let there be two classes ‘A’ and ‘B’. If we want to allocate the details that belong to class ‘A’ to an object of class ‘B’ then this can be achieved by – 
 

B(object of class B) = A(object of class A) 
where ‘=’ has been overloaded for objects of class type ‘B’.

Class conversion can be achieved by conversion function which is done by the use of operator overloading.
Example: 
 

CPP




#include <bits/stdc++.h>
using namespace std;
 
// Source class, i.e
// class that will be converted to another class
class Class_type_one {
    string a = "GeeksforGeeks";
 
public:
    // Member function which returns
    // string type data
    string get_string()
    {
        return (a);
    }
 
    // Member function to display
    void display()
    {
        cout << a << endl;
    }
};
 
// Destination class, i.e
// Class type to which source class will be converted
class Class_type_two {
    string b;
 
public:
    // Operator overloading which accepts data
    // of the Destination class and
    // assigns those data to the source class
    // Here it is for the conversion of
    // Class_type_two to Class_type_one
    void operator=(Class_type_one a)
    {
        b = a.get_string();
    }
 
    // Member function for displaying
    // the data assigned to b.
    void display()
    {
        cout << b << endl;
    }
};
 
int main()
{
    // Creating object of class Class_type_one
    Class_type_one a;
 
    // Creating object of class Class_type_two
    Class_type_two b;
 
    // CLass type conversion
    // using operator overloading
    b = a;
 
    // Displaying data of object
    // of class Class_type_one
    a.display();
 
    // Displaying data of object
    // of class Class_type_two
    b.display();
 
    return 0;
}


Output

GeeksforGeeks
GeeksforGeeks

Class to class conversion using constructor in destination class:

A source class can be converted to a destination class by using a constructor in destination class which takes reference to source class.

Destination_class( source_class &object)

{

//assignments of members of destination class

//with members of source class using source class object 

}

C++




#include <bits/stdc++.h>
using namespace std;
 
// Source class, i.e
// Class which will be converted to another
class Class_type_one {
    string a = "GeeksforGeeks";
 
public:
    // Member function which returns
    // string type data
    string get_string()
    {
        return (a);
    }
 
    // Member function to display
    void display()
    {
        cout << a << endl;
    }
};
 
// Destination class, i.e
// class to which source class will be converted
class Class_type_two {
    string b;
 
public:
     // a default constructor to
     //create object of this class
     Class_type_two()
     {
        
     }
    //here we are using constructor
    //which takes reference of object
    //of source class i.e. Class_type_one
    //as its argument
    Class_type_two (Class_type_one& a)
    {
        b = a.get_string();
    }
 
    // Member function for displaying
    // the data assigned to b.
    void display()
    {
        cout << b << endl;
    }
};
 
int main()
{
    // Creating object of class Class_type_one
    Class_type_one a;
 
    // Creating object of class Class_type_two
    Class_type_two b;
 
    // CLass type conversion
    // using operator overloading
    b = a;
 
    // Displaying data of object
    // of class Class_type_one
    a.display();
 
    // Displaying data of object
    // of class Class_type_two
    b.display();
 
    return 0;
}


Output

GeeksforGeeks
GeeksforGeeks

Class to class conversion using casting operator:

Class to class conversion is also possible using casting operator in source class. Operator type will be that of destination class.

A casting operator will be member function of source class and will return object of destination class.

operator destination_class()

{

destination_class obj;

//assignment of members of destination class with members of source class

//this function is member of source class hence it can access its other data members

return obj

}

C++




#include <bits/stdc++.h>
using namespace std;
//Forward declaration of
//destination class
class Class_type_two;
// Source class, i.e
// Class which will be converted to another
class Class_type_one {
    string a = "GeeksforGeeks";
 
public:
    // Member function which returns
    // string type data
    string get_string()
    {
        return (a);
    }
 
    // Member function to display
    void display()
    {
        cout << a << endl;
    }
  //declaration of casting
  //operator for class conversion
  //note that it will be of type destination class
  operator Class_type_two();
     
};
 
// Destination class, i.e
// class type to which source class will converted
class Class_type_two {
     
 
public:
     string b;
     
    // Member function for displaying
    // the data assigned to b.
    void display()
    {
        cout << b << endl;
    }
};
//here we define our casting operator
Class_type_one::operator Class_type_two()
{
  Class_type_two obj;
  obj.b=a;
  return obj;
   
}
 
int main()
{
    // Creating object of class Class_type_one
    Class_type_one a;
 
    // Creating object of class Class_type_two
    Class_type_two b;
 
    // CLass type conversion
    // using operator overloading
    b = a;
 
    // Displaying data of object
    // of class Class_type_one
    a.display();
 
    // Displaying data of object
    // of class Class_type_two
    b.display();
 
    return 0;
}


Output

GeeksforGeeks
GeeksforGeeks


Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads