什么是运算符重载
赋予运算符具有操作自定义类型数据功能
友元函数重载运算符
类成员函数重载运算符
#include <iostream>
using namespace std;
/*
什么是运算符重载?
赋予运算符具有操作自定义类型数据功能
运算符重载的实质是什么?
运算符重载的实质本身就是函数调用
运算符重载函数的写法
函数返回值 函数名(函数参数)
函数返回值 :运算完成后的值决定的 Complex
函数名 : operator 加上重载运算符组成函数名 operator+
参数 :看运算符的操作数,具体参数个数是要看你重载函数形式是什么
函数体 : 写运算符具体想要的操作
*/
class Complex
{
public:
Complex(int a=0, int b=0) :a(a), b(b) {}
void print()
{
cout << a << endl;
cout << b << endl;
}
friend Complex operator+ (Complex one, Complex two);
//类成员函数重载,参数个数等于操作减一
bool operator> (Complex object)
{
if (this->a > object.a)
{
return true;
}
else if (this->a == object.a && this->b > object.b)
{
return true;
}
else
{
return false;
}
}
protected:
int a;
int b;
};
//友元重载: 参数个数就是操作数据
Complex operator+ (Complex one, Complex two)
{
return Complex(one.a + two.a, one.b + two.b);
}
int main()
{
Complex one(1, 1);
Complex two(2, 0);
Complex three;
three = one + two; //Complex 重载函数的隐式调用
three.print();
//显式调用
Complex result;
result = operator+(one, two);
if (one > two) //one > two 是bool
{
cout << "one 比较大" << endl;
}
//对象可以表示一个数据,所以参数应该少一个
if (one.operator>(two))
{
cout << "one比较大" << endl;
}
return 0;
}
特殊运算符重载
流运算符重载
-
cin类型 : istream类的对象
-
cout类型:ostream类的对象
-
流运算符 >> <<
-
必须采用友元函数形式重载
-
#include <iostream> #include <string> using namespace std; class MM { public: MM(string name = "", int age = 18) :name(name), age(age) {} friend istream& operator>>(istream& in, MM& mm); friend ostream& operator<<(ostream& out, MM& mm); protected: string name; int age; }; //其他运算符 //= () -> [] 只能采用类的成员函数形式重载 //流重载采用友元方式 //. .* ?: :: 不能重载 istream& operator>>(istream& in, MM& mm) { in >> mm.name >> mm.age; return in; } ostream& operator<<(ostream& out, MM& mm) { out << mm.name << "\t" << mm.age << endl; return out; } int main() { string str; cin >> str; cout << str << endl; MM mm; cin >> mm; //void operator>>(istream& in,MM& mm) cout << mm; //void operator<<(ostream& out,MM& mm) cin >> str >> mm; cout << str << endl << mm; return 0; }
-
++ --运算符重载
解决问题:前置和后置的问题:增加无用参数 int去表示当前运算符重载是后置操作
-
文本重载 (新标准中的,稍微落后一点开发工具不适用)
-
其他运算符
-
= () -> [] 只能采用类的成员函数形式重载
-
流重载采用友元方式
-
. .* ?: :: 不能重载
-
-
对象隐式转换
-
#include <iostream> #include <string> #include <chrono> #include <thread> using namespace std; class MM { public: MM(string name ,int age):name(name),age(age){} friend ostream& operator<<(ostream& out, MM& object) { out << object.name << "\t" << object.age << endl; return out; } MM operator++(int) //需要一个无用参数 充当标记 { int num = age; age++; return MM(name, num); //上面三行等效下面一行 //return MM(name,age++); } MM operator++() { return MM(name, ++age); } //类的对象的隐式转换 operator operator int() { return age; } protected: string name; int age; }; //文本重载 unsigned long long operator"" _h(unsigned long long num) { return 60 * 60*num; } unsigned long long operator"" _min(unsigned long long num) { return 60 * num; } int main() { MM mm("小芳", 18); cout << mm << endl; int num = 1; int result = num++; //result=1 num=2 cout << result << "\t" << num << endl; result = ++num; //result=3 num=3 cout << result << "\t" << num << endl; MM object = mm++; cout << object<<mm; //age=18 mm: 19 object = ++mm; //age:20 mm: 20 cout << object<<mm; //this_thread::sleep_for(3s); cout << "3s结束" << endl; int second = 1_h; cout << second << "s" << endl; int sum = 1_h + 18_min + 30; cout << sum << "s" << endl; MM girl("girl", 18); int girlAge = girl; cout << girlAge << endl; return 0; }