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

14 Sep, 2021
Pre-requisite: Type Conversion in C++, Advanced C++ | Conversion OperatorsThrough 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 classclass 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 convertedclass 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;}
GeeksforGeeks
GeeksforGeeks
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 anotherclass 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 convertedclass 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;}
GeeksforGeeks
GeeksforGeeks
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 classclass Class_type_two;// Source class, i.e// Class which will be converted to anotherclass 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 convertedclass 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 operatorClass_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;}
GeeksforGeeks
GeeksforGeeks
husain0803
rs1686740
cpp-operator-overloading
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++