如何在C++中将类转换为另一类类型?

如何在C++中将类转换为另一类类型?

通过类转换,可以将属于特定类类型的数据分配给属于另一个类类型的对象。

例子:

假设有两个类“A”和“B”。如果我们想将属于类“A”的详细信息分配给类“B”的对象,则可以通过以下方式实现 –

B(类B的对象)= A(类A的对象)

其中“=”已被重载为类类型“B”的对象。

可以通过使用运算符重载完成的转换函数来实现类转换。

例子:

#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;
}  

输出

GeeksforGeeks
GeeksforGeeks

在目标类中使用构造函数进行类到类转换:

可以通过在目标类中使用一个构造函数,该构造函数接受对源类的引用,将源类转换为目标类。

Destination_class( source_class &object) 

{ 

//assignments of members of destination class 

//with members of source class using source class object 

} 
#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;
}  

输出

GeeksforGeeks
GeeksforGeeks

使用类型强制转换实现类型转换:

可以使用源类的类型强制转换运算符来实现类到类的转换。运算符类型将是目标类的类型。

这个类型强制转换运算符将作为源类的成员函数,返回目标类的对象。

operator destination_class() 

{ 

destination_class obj; 

// 将目标类的成员赋值源类的成员 

// 由于此函数是源类的成员,因此可以访问其它成员数据 

return obj 

} 
#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类的对象
    Class_type_one a;
 
    // 创建Class_type_two类的对象
    Class_type_two b;
 
    //使用运算符重载进行类类型转换
    b = a;
 
    // 显示Class_type_one类对象的数据
    a.display();
 
    // 显示Class_type_two类对象的数据
    b.display();
 
    return 0;
}  

输出

GeeksforGeeks
GeeksforGeeks

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程