什么是运算符重载?
赋予运算符具有操作自定义型数据功能
运算符重载的实质:
函数的调用
运算符重载函数写法:
函数返回值类型 函数名 (函数参数)
函数返回值:运算符运算完成后的值决定的。
函数名:operate+重载运算符组成函数名 operate+
参数:看运算符的操作数,具体参数个数要看函数重载形式
函数体:写运算符具体想要实现的操作
友元函数重载运算符
类重载:以类的成员函数重载
#include<iostream>
using namespace std;
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);
friend bool operator>(Complex& one, Complex& two) {
if(one.a>two.a) {
return true;
}
else if ((one.a == two.a) && (one.b > two.b)) {
return true;
}
else {
return false;
}
}
//bool operator>(Complex object) {//类成员函数重载,参数个数=操作数-1
// 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重载函数隐式调用
//显示调用
Complex result;
result = operator+(one, two);
three.print();
cout << "------------------" << endl;
if (one > two) {//one>two是bool类型
cout << "one比two大" << endl;
}
else {
cout << "one比two小" << endl;
}
//对象可以表示一个数据
//if (one.operator>(two)) {//因为是通过对象调用成员函数,所以操作数少一个
// cout << "one比two大"<<endl;
//}
//else {
// cout << "one比two小" << endl;
//}
}
特殊运算符重载
注意:
= () -> [] 只能采用类的成员函数重载
>> <<只能采用友元重载
. .* ?: ::不能被重载
流运算符重载
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) {//&operator必须返回引用,如果不返回引用就是个局部变量,不能当作运算符右值
out << mm.name << "\t" << mm.age;
return out;
}
int main() {
string str("ILoveYou");
cin >> str;
//void operator>>(istream& in,MM& mm)(初步推断,因为会修改cin和mm的值,所以要传引用)
cout << str;
MM mm;
cin >> mm;
cout << mm;
int num;
//cin >> mm >> num;报错,因为没有返回值,但只有当其返回值是cin的时候才可以连续操作
return 0;
}
++ --运算符重载
解决问题:前置后置问题 增减一个int去表示当前运算符重载是后置的
文本重载
对象隐式转换:
#include<iostream>
#include<string>
#include<chrono>
#include<thread>
using namespace std;
class GG {
public:
GG(string name,int age):name(name),age(age){}
friend ostream& operator<<(ostream& out, GG& gg) {
out << gg.name << "\t" << gg.age << endl;
return out;
}
GG operator++(int) {//需要一个无用参数,充当标记,帮助区分前置和后置
return GG(name, age++);//构建的是匿名(临时)对象(右值)所以如要返回引用,要么加const修饰,要么用右值引用
}
GG operator++() {
return GG(name, ++age);
}
//类的对象隐式转换operator的一种使用方式
operator int() {//operator+隐式转换类型 无函数参数
return age;
}
protected:
string name;
int age;
};
//文本重载,为了和标准库中重载系列区分,以下划线开头
unsigned long long operator""_h(unsigned long long num) {
return 60 * 60 * num;
}
int main() {
GG gg("shuaige",18);
cout << gg << endl;;
int num = 1;
int result = num++;//result=1,num=2
result = ++num;//result=3,num=3
cout << "-------------------" << endl;
GG sum = gg++; //age=18,sum 19
cout << sum << endl << gg << endl;//age=20,sum=20
sum = ++gg;
//文本重载实例
this_thread::sleep_for(1s);
cout << sum << endl << gg << endl;
int a = 1_h;
cout << a << "s" << endl;
//类的对象隐式转换
GG boy("大男孩", 18);
int boyAge = boy;
cout << boyAge << endl;
return 0;
}