1基本内容
(1)概念
赋予运算符具有操作自定义类型数据的功能。
本质为函数的调用。
函数写法
函数返回值类型 函数名 (函数参数)
返回值类型:运算后的值
函数名:operator加上重载运算符
参数:运算符的操作数
函数体:写运算符具体想要的操作
(2)注意
= () -> []只能采用类的成员函数重载
流重载只能友元重载(改变了另一个类的对象)
. 和 .*和 ?:以及::不能被重载
2基本重载类型
(1)友元重载
class Complex
{
public:
Complex(int a=0, int b=0) :a(a), b(b) {};
void print()
{
cout << a << endl;
cout << b << endl;
}
//实现加法所以用operator加上+ 类比实现减法用operator-
//友元重载:参数个数就是操作数
friend Complex operator+(Complex one, Complex two);
protected:
int a;
int b;
};
Complex operator+(Complex one, Complex two)
{
return Complex(one.a + two.a, one.b+two.b);
}
/*隐式调用 Complex three = one + two;
显式调用 Complex three = operator+(one,two) */
(2)类成员函数重载
/*
if (one > two)//bool类型 本质为one.operator>(two) 因此参数数减1
{
cout << "one比two大" << endl;
}
*/
class Complex
{
public:
Complex(int a=0, int b=0) :a(a), b(b) {};
void print()
{
cout << a << endl;
cout << b << endl;
}
//类成员函数重载,参数个数等于操作数减一,通过对象调用成员函数
//大小比较是bool类型
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 true;
}
protected:
int a;
int b;
};
3特殊运算符重载
(1)流运算符重载(只能采用友元的方式去重载)
cin类型:istream类的对象
cout类型:ostream类的对象
流运算符:<< >>
class MM;
int main()
{
/*string str;
cin >> str;
cout << str;//进行了运算符重载
*/
MM mm;
//要修改数据,操作两个对象推测void operator>>(istream& in,MM& mm)
/*倘若为void则
int num; cin >> mm >> num;
会出错无法实现连续输入 要求cin>>mm 再cin>>num
因此在cin>>mm后返会cin
最终istream& operator>>(istream& in,MM& mm)
若为istream operator>>(istream& in,MM& mm)报错因为是局部变量*/
int num;
cin >> mm>>num;
//可以不修改数据,操作两个对象推测void operator<<(ostream& out,MM& mm)
//可以不写 & 此后过程同上最终ostream& operator<<(ostream& out, MM& mm)
cout << mm<<endl<<num;
return 0;
}
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;
return out;
}
(2)++、--运算符重载
主要解决前置后置问题
使用无用参数作为标记,判断前置还是后置
class MM;
int main()
{
MM mm("abby",18);
cout << mm;
int num = 1;
int result = num++;//result=1 num=2
result = ++num;//result=3 num=3
//类成员函数重载 参数=操作数-1 操作数为1参数为0
//因此MM operator++()
// MM operator++(int)添加int 表示后置
MM rs=mm++;//age=18 mm 19 rs 18
cout << rs << mm;
rs = ++mm;//age=20 mm20 rs 20
cout << rs << mm;
return 0;
}
class MM
{
public:
MM(string name, int age) :name(name), age(age) {}
friend ostream& operator<<(ostream& out, MM& mm)
{
out << mm.name << "\t" << mm.age << endl;
return out;
}
//或者使用const MM& operator++(int) 或MM&& operator++(int)
MM operator++(int)//int为无用参数,充当标记
{
return MM(name, age++);//临时对象
}
MM operator++()
{
return MM(name, ++age);
}
protected:
int age;
string name;
};
(3)文本重载
#include<thread>
//文本重载 固定写法 _h 参数 返回值可以改其余固定
unsigned long long operator"" _h(unsigned long long num)
{
return 60 * 60*num;
}
int main()
{
this_thread::sleep_for(1s);//延时函数 对文本1s重载
int n = 1_h;
cout << n;
return 0;
}
(4)类的隐士隐士类型转换
注意函数写法
class MM
{
public:
MM(string name, int age) :name(name), age(age) {}
operator int()//根据需求替换int即可
{
return age;
}
protected:
string name;
int age;
};
int main()
{
MM mm("baby", 18);
int num = mm;
return 0;
}