题目内容
//类型转换运算符重载函数#include "iostream"using namespace std;class Stu{public:string m_no;string m_name;float m_grade;//定义类型转换运算符重载函数:不能指定返回类型,不能带参,只能重载为类成员函数operator int(){return static_cast(m_grade);}operator string(){return m_name;}//声明输入输出运算符重载函数,为Stu类的友元函数friend ostream& operator<< (ostream& os, const Stu& s);friend istream& operator>> (istream& is, Stu& s);};ostream& operator<< (ostream& os, const Stu& s){os << s.m_name << "\t" << s.m_grade << endl;return os;}istream& operator>> (istream& is, Stu& s){is >> s.m_no >> s.m_name >> s.m_grade;return is;}int main(){Stu s1,s2;cin >> s1;cin >> s2;cout << int(s1)+int(s2) << endl;cout << string(s1)+string(s2) << endl;return 0;}运行程序,并输入以下信息:1001 zhang 50.561002 liu 50.78程序运行结果是:
查看答案
搜索结果不匹配?点我反馈
更多问题