How to convert a class to another class type in C++?
Last Updated :
14 Sep, 2021
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;
class Class_type_one {
string a = "GeeksforGeeks" ;
public :
string get_string()
{
return (a);
}
void display()
{
cout << a << endl;
}
};
class Class_type_two {
string b;
public :
void operator=(Class_type_one a)
{
b = a.get_string();
}
void display()
{
cout << b << endl;
}
};
int main()
{
Class_type_one a;
Class_type_two b;
b = a;
a.display();
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;
class Class_type_one {
string a = "GeeksforGeeks" ;
public :
string get_string()
{
return (a);
}
void display()
{
cout << a << endl;
}
};
class Class_type_two {
string b;
public :
Class_type_two()
{
}
Class_type_two (Class_type_one& a)
{
b = a.get_string();
}
void display()
{
cout << b << endl;
}
};
int main()
{
Class_type_one a;
Class_type_two b;
b = a;
a.display();
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;
class Class_type_two;
class Class_type_one {
string a = "GeeksforGeeks" ;
public :
string get_string()
{
return (a);
}
void display()
{
cout << a << endl;
}
operator Class_type_two();
};
class Class_type_two {
public :
string b;
void display()
{
cout << b << endl;
}
};
Class_type_one::operator Class_type_two()
{
Class_type_two obj;
obj.b=a;
return obj;
}
int main()
{
Class_type_one a;
Class_type_two b;
b = a;
a.display();
b.display();
return 0;
}
|
Output
GeeksforGeeks
GeeksforGeeks
Share your thoughts in the comments
Please Login to comment...