题目内容

//类型转换运算符重载函数#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程序运行结果是:

查看答案
更多问题

//转换构造函数: 只能带一个非本类的非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;}程序运行结果是:

//仿函数(函数对象): 在类中重载()运算符, 使得类对象可以像函数一样使用。在STL的算法中使用较广泛#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 AddGrade{public://重载"()"运算符float operator()(Stu& s){s.m_grade += 10;return s.m_grade;}int operator()(float s){return int(s);}};int main(){Stu s;AddGrade a;cout << a(s) << endl;//输出函数对象cout << s.m_grade << endl;cout << a(123.45) << endl;//输出函数对象return 0;}程序运行结果是:

从产品角度划分,Nike与Addidas之间的竞争属于?

A. 类别竞争
B. 品牌竞争
C. 形式竞争
D. 愿望竞争

从产品角度划分,共享电动车与共享自行车之间的竞争属于?

A. 品牌竞争
B. 类别竞争
C. 形式竞争
D. 愿望竞争

答案查题题库