इस ट्यूटोरियल में, हम यह समझने के लिए एक प्रोग्राम पर चर्चा करेंगे कि C/C++ में एक क्लास को दूसरे क्लास टाइप में कैसे बदला जाए।
ऑपरेटर ओवरलोडिंग की मदद से क्लास कन्वर्जन किया जा सकता है। यह एक वर्ग प्रकार के डेटा को दूसरे वर्ग प्रकार के ऑब्जेक्ट को असाइन करने की अनुमति देता है।
उदाहरण
#include <bits/stdc++.h>
using namespace std;
//type to which it will be converted
class Class_type_one {
string a = "TutorialsPoint";
public:
string get_string(){
return (a);
}
void display(){
cout << a << endl;
}
};
//class to be converted
class Class_type_two {
string b;
public:
void operator=(Class_type_one a){
b = a.get_string();
}
void display(){
cout << b << endl;
}
};
int main(){
//type one
Class_type_one a;
//type two
Class_type_two b;
//type conversion
b = a;
a.display();
b.display();
return 0;
} आउटपुट
TutorialsPoint TutorialsPoint