이 튜토리얼에서는 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