题目内容
//转换构造函数: 只能带一个非本类的非const引用参数//作用:将一个标准类型数据或自定义的类型数据转换为其他类对象#include "iostream"using namespace std;class Stu{public:string m_no;string m_name;float m_grade;Stu(string no="1001", string name="zhangsan", int grade=80):m_no(no),m_name(name),m_grade(grade){ }};class Teacher{public:string m_no;string m_name;string m_title;//定义转换构造函数:将Stu类对象转换为Teacher类对象Teacher(Stu& s){m_no = s.m_no;m_name = s.m_name;m_title = "助教";}//定义转换构造函数:将string基本类型数据转换为Teacher类对象Teacher(string& name){m_name = name;m_title = "讲师";}};int main(){Stu s;Teacher t1(s);cout << t1.m_name << " "<< t1.m_title << endl;string name = "wanglan";Teacher t2(name);cout << t2.m_name << " " << t2.m_title << endl;return 0;}程序运行结果是:
查看答案
搜索结果不匹配?点我反馈
更多问题