//运算符重载函数的显式和隐式调用方式#include "iostream"using namespace std;// 定义学生类class Stu{public:string m_no;string m_name;float m_grade;Stu(string no, string name, int grade){m_no = no;m_name = name;m_grade = grade;}// 1、定义成员函数,并用显式的函数调用来实现。float add(const Stu& s){return m_grade + s.m_grade;}//2、运算符重载,对现有的运算符重新进行定义,可用显式或隐式两种不同方式调用实现float operator+(const Stu& s){return m_grade + s.m_grade;}};// 定义主函数int main(){Stu s1("1001", "zhangsan", 90);Stu s2("1002", "wanglan", 80);// 1、定义成员函数,并用显式的函数调用来实现。cout << "s1.add(s2)=" << s1.add(s2) << endl;// 显式的函数调用//2、运算符重载,对现有的运算符重新进行定义,可用显式或隐式两种不同方式调用实现cout << "s1.operator+(s2)=" << s1.operator+(s2) << endl;// 显式的函数调用cout << "s1+s2=" << s1 + s2 << endl;// 隐式的函数调用return 0;}程序运行结果是:
查看答案
//输入输出运算符重载#include "iostream"using namespace std;// 定义学生类class Stu{public:string m_no;string m_name;float m_grade;//重载关系运算符: ">"bool operator>(const Stu& s){if (this->m_grade > s.m_grade) return 1;else return 0;}//声明输入输出运算符重载函数,为Stu类的友元函数friend ostream& operator<< (ostream& os, const Stu& s);friend istream& operator>> (istream& is, Stu& s);};//重载输入输出运算符:"<<"和">>" ,因声明为Stu类的友元函数,所以可以访问Stu类对象的成员ostream& operator<< (ostream& os, const Stu& s) { //ostream& os,不能定义为常量,因为向流中写入数据时会改变流的状态os << s.m_name << " " << s.m_grade << endl; //输出Stu类对象的成员数据return os;}istream& operator>> (istream& is, Stu& s) { // 参数s是用来存放输入数据,所以不能定义为常量cout << "please input Stu's No Name and Grade: " << endl;is >> s.m_no >> s.m_name >> s.m_grade; //输入Stu类对象的成员数据return is;}// 定义主函数int main(){Stu s1,s2; //调用系统提供的默认无参构造函数cin >> s1;cin >> s2;if(s1>s2) cout << s1;else cout << s2;return 0;}运行上面的程序,并输入以下信息:1001 zhangsan 701002 liuli 90则程序的运行结果是:
//类型转换运算符重载函数#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;}程序运行结果是: