运算符重载
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
int a;
Student(int a)
{
this->a = a;
}
int operator+(const Student&stu1)
{
return this->a + stu1.a;
}
};
int main()
{
Student stu1(10);
Student stu2(20);
Student stu3(30);
string str1 = "aaaa";
string str2 = "bbbb";
cout << str1 + str2 << endl;
//int sum = stu1 + stu2 + stu3; //不可以
int sum = stu1 + stu2; //隐式调用 前面那个是调用者stu1调用operator这个 后面那个是传参stu2传给Student
//int sum = stu1.operator+(stu2);//显式调用
cout << sum << endl;
system("pause");
return 0;
}