运算符重载定义:赋予运算符具有操作自定义类型数据功能
实质:函数的调用
写法:函数返回值 函数名(函数参数)
函数返回值由运算完成后的值决定 函数名:operator加上重载运算符 参数:看运算符的操作数
友元函数&类成员函数重载运算符
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() = default;//指定使用默认的无参构造函数
Complex(int a, int b) :a(a), b(b) {}
void print()
{
cout << a << endl << b;
}
friend Complex operator+(Complex A, Complex B)//友元函数运算符重载函数写法,操作符是+
{
return Complex(A.a + B.a, A.b + B.b);//返回值类型为Complex
}
//类成员函数重载,参数个数等于操作数减一
bool operator>(Complex A)
{
if (this->a > A.a)
{
return true;
}
if (this->a == A.a && this->b > A.b)
{
return true;
}
else
{
return false;
}
}
protected:
int a;
int b;
};
int main()
{
Complex A(2, 3);
Complex B(7, 8);
Complex C;
C = A + B; //重载函数的隐式调用
Complex D;
D = operator+(C, A);//重载函数的显示调用
if (A > B)//A>B时是bool值
{
cout << "A" << endl;
}
//对象可以表示一个数据,所以参数应该少一个
if (B.operator>(C))
{
cout << "B" << endl;
}
return 0;
}
特殊运算符重载
流运算符重载(流重载必须采用友元的方式)
cin类型:istream类的对象
cout类型:ostream类的对象
流运算符: << >>
class student
{
public:
student() = default;
student(string name, int age) :name(name), age(age) {}
friend istream& operator>>(istream& in, student& student)
{//cin是istream类
cin >> student.name >> student.age;
}
friend ostream& operator<<(ostream& out, student& student)
{//cout是ostream类
cout << student.name << endl << student.age;
}
protected:
string name;
int age;
};
int main()
{
string str;
cin >> str;
cout << str << endl;
student A;
cin >> A;
cout << A << endl;
return 0;
}
++ --运算符重载
前置和后置的问题
增加无用参数int来确定++的前置还是后置
class student
{
public:
student() = default;
student(string name, int age) :name(name), age(age) {}
friend ostream& operator<<(ostream& out, student& A)
{
cout << A.name << endl << A.age;
return out;
}
const student& operator++(int)//int 为无用参数 充当标记
{
return student(name, age++);
}
student operator++()
{
return student(name, ++age);
}
student operator--(int)
{
return student(name, age--);
}
student operator--()
{
return student(name, --age);
}
protected:
string name;
int age;
};
int main()
{
student A("jfhf", 18);
cout << A << endl;
int num = 1;
int result = num++;
result = num--;
student B = A++;
cout << B;
B = ++A;
cout << B;
student C;
C = --A;
cout << C << endl;
C = A--;
cout << C;
return 0;
}
文本重载(新标准中的,有的编译工具不支持),以及类的对象的隐式转换就不写啦
其他运算符:= () -> [] 只能采用类的成员函数形式重载
. .* ?: :: 不能重载
案例小int:
#include <iostream>
using namespace std;
class INT
{
public:
INT() = default;
INT(int numa) :num(num) {}
int& data()
{
return num;
}
string tostr()
{
return to_string(num);
}
//友元函数重载操作数-1=重载函数的参数个数
friend INT operator-(const INT& A, const INT& B)
{
return(A.num - B.num);
}
friend ostream& operator<<(ostream& out, const INT& A)
{
out << A.num << endl;
return out;
}
friend istream& operator<<(istream& in, INT& A)
{
in >> A.num;
return in;
}
INT operator+(const INT& value)
{
return INT(this->num + value.num);
}
INT operator+=(const INT& A)
{
return INT(this->num + A.num);
}
INT operator+=(const int& A)
{
return INT(this->num + A);
}
INT operator-=(const INT& A)
{
return INT(this->num - A.num);
}
INT operator-=(const int& A)
{
return INT(this->num - A);
}
INT operator++(int)
{
return INT(this->num++);
}
INT operator++()
{
return INT(++this->num);
}
INT operator--(int)
{
return INT(this->num--);
}
INT operator--()
{
return INT(--this->num);
}
INT operator-()
{
return INT(-this->num);
}
INT operator&(const INT& A)
{
return INT(this->num & A.num);
}
INT operator^(const INT& A)
{
return this->num ^ A.num;
}
bool operator!()
{
return !this->num;
}
bool operator>(const INT& A)
{
return this->num>A.num;
}
bool operator<(const INT& A)
{
return this->num < A.num;
}
int* operator&()
{
return &this->num;
}
protected:
int num = 0;
};
void print(const INT& num)
{
cout << num << endl;
}
int main()
{
INT A(99);
INT B = 88;
print(A.operator-());
print(A.operator+(B));
print(B.operator+=(8));
return 0;
}
重载个人理解就是自己写一个运算方式把不能进行相关运算的数据进行运算
operator是标志