c++中怎样完成类之间的转换?
从student类转化为teacher类要怎样转换,用类型转换函数可以么?这样写为啥老是报错#include<iostream>#include<string>using...
从student类转化为teacher类要怎样转换,用类型转换函数可以么?
这样写为啥老是报错
#include <iostream>
#include <string>
using namespace std;
class teacher
{private:
string num;
string name;
string sex;
string subject;
public:
void getvalue();
void display();
};
void teacher::getvalue()
{cin>>num>>name>>sex>>subject;}
void teacher::display()
{
cout<<num<<endl;
cout<<name<<endl;
cout<<sex<<endl;
cout<<subject<<endl;
}
class student
{private:
string num;
string name;
string sex;
int score;
public:
void getvalue();
void display();
operator teacher();
};
void student::getvalue()
{cin>>num>>name>>sex>>score;}
void student::display()
{
cout<<num<<endl;
cout<<name<<endl;
cout<<sex<<endl;
cout<<score<<endl;
}
student::operator teacher()
{return teacher ;}//在这里报错
void main()
{
student a;
teacher b;
a.getvalue();
b.getvalue();
teacher(a);//这里也报错
a.display();
b.display();
}
这是我写的代码。
帮我看看啊,谢谢啊。 展开
这样写为啥老是报错
#include <iostream>
#include <string>
using namespace std;
class teacher
{private:
string num;
string name;
string sex;
string subject;
public:
void getvalue();
void display();
};
void teacher::getvalue()
{cin>>num>>name>>sex>>subject;}
void teacher::display()
{
cout<<num<<endl;
cout<<name<<endl;
cout<<sex<<endl;
cout<<subject<<endl;
}
class student
{private:
string num;
string name;
string sex;
int score;
public:
void getvalue();
void display();
operator teacher();
};
void student::getvalue()
{cin>>num>>name>>sex>>score;}
void student::display()
{
cout<<num<<endl;
cout<<name<<endl;
cout<<sex<<endl;
cout<<score<<endl;
}
student::operator teacher()
{return teacher ;}//在这里报错
void main()
{
student a;
teacher b;
a.getvalue();
b.getvalue();
teacher(a);//这里也报错
a.display();
b.display();
}
这是我写的代码。
帮我看看啊,谢谢啊。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询