算术运算符重载
一般来说对于内置的数据类型(比如整形)进行加减乘除运算,编译器可以识别。但对于自定的数据类型(比如类、结构体)进行运算的话,编译器就不知道了
但我们可以自己手写一个成员函数来实现两个自定义数据类型的相加。
注意成员函数和全局函数的写法有一点区别,而且成员函数只需要写一个参数,另外一个由this指针指向。
重载+号
注意:
注意1:对于内置的数据类型的表达式的的 运算符是不可能改变的。也就是不能自己写个重载函数,去改变内置数据类型的运算方式。
注意2:不要滥用运算符重载。函数名可以写的是重载+号,但里面真正实现可以是各种运算,这会造成很大的歧义。
#include<iostream>
using namespace std;
#include<string>
// 加号运算符重载
class Person {
public:
int m_A;
int m_B;
// 1.通过成员函数重载+号
// temp是个局部变量在函数调用完后被回收
// 因为返回的是局部对象,所以return语句会执行拷贝构造,构造出跟temp一模一样的新对象出来
/*Person operator+(Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
*/
};
// 2.全局函数重载+号
Person operator+(Person& p1, Person& p2) {
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
// 运算符重载,也可以发生函数重载
Person operator+(Person& p, int num) {
Person temp;
temp.m_A = p.m_A - num;
temp.m_B = p.m_B - num;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 15;
cout << "p1.m_a=" << p1.m_A << endl << "p1.m_B=" << p1.m_B << endl << endl;
Person p2;
p2.m_A = 20;
p2.m_B = 25;
cout << "p2.m_A=" << p2.m_A << endl << "p2.m_B=" << p2.m_B << endl << endl;
Person p3 = p1 + p2;
cout << "Person p3 = p1 + p2" << endl;
cout << "相加后p3.m_A=" << p3.m_A << endl << "相加后p3.m_B=" << p3.m_B << endl<<endl;
Person p4 = p1 +100;
cout << "Person p4 = p1 - 100;" << endl;
cout << "相减后p4.m_A=" << p4.m_A << endl << "相加后p4.m_B=" << p4.m_B << endl;
}
int main() {
test01();
system("pause");
return 0;
}
运算符重载,也可以发生函数重载!!!
成员函数重载的调用的本质:Person p3=p1.operator+(p2)
成员函数重载运算符只有一个参数!!
简化:Person p3=p1+p2
全局函数重载的调用的本质:Person p3=operator+(p1,p2)
全局函数重载运算符有两个参数!
简化:Person p3=p1+p2
既然成员函数和全局函数简化后写法都是一样的,所以想要重载运算符的话,写一个重载函数就行了。
重载-号
#include<iostream>
using namespace std;
#include<string>
// 加号运算符重载
class Person {
public:
int m_A;
int m_B;
// 1.通过成员函数重载-号
Person operator-(Person& p) {
Person temp;
temp.m_A = this->m_A - p.m_A;
temp.m_B = this->m_B - p.m_B;
return temp;
}
};
// 2.全局函数重载-号
/*Person operator-(Person& p1, Person& p2) {
Person temp;
temp.m_A = p1.m_A - p2.m_A;
temp.m_B = p1.m_B - p2.m_B;
return temp;
}*/
// 运算符重载,也可以发生函数重载
Person operator-(Person& p, int num) {
Person temp;
temp.m_A = p.m_A - num;
temp.m_B = p.m_B - num;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 15;
cout << "p1.m_a=" << p1.m_A << endl << "p1.m_B=" << p1.m_B << endl << endl;
Person p2;
p2.m_A = 20;
p2.m_B = 25;
cout << "p2.m_A=" << p2.m_A << endl << "p2.m_B=" << p2.m_B << endl << endl;
Person p3 = p1 - p2;
cout << "Person p3 = p1 - p2" << endl;
cout << "相减后p3.m_A=" << p3.m_A << endl << "相减后p3.m_B=" << p3.m_B << endl << endl;
Person p4 = p1 - 100;
cout << "Person p4 = p1 - 100;" << endl;
cout << "相减后p4.m_A=" << p4.m_A << endl << "相减后p4.m_B=" << p4.m_B << endl;
}
int main() {
test01();
system("pause");
return 0;
}
重载/号
#include<iostream>
using namespace std;
#include<string>
// 加号运算符重载
class Person {
public:
int m_A;
int m_B;
// 1.通过成员函数重载/号
Person operator/(Person& p) {
Person temp;
temp.m_A = this->m_A / p.m_A;
temp.m_B = this->m_B / p.m_B;
return temp;
}
};
// 2.全局函数重载/号
/*Person operator/(Person& p1, Person& p2) {
Person temp;
temp.m_A = p1.m_A / p2.m_A;
temp.m_B = p1.m_B / p2.m_B;
return temp;
}*/
// 运算符重载,也可以发生函数重载
Person operator/(Person& p, int num) {
Person temp;
temp.m_A = p.m_A / num;
temp.m_B = p.m_B / num;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 15;
cout << "p1.m_a=" << p1.m_A << endl << "p1.m_B=" << p1.m_B << endl << endl;
Person p2;
p2.m_A = 20;
p2.m_B = 25;
cout << "p2.m_A=" << p2.m_A << endl << "p2.m_B=" << p2.m_B << endl << endl;
Person p3 = p1 / p2;
cout << "Person p3 = p1 / p2" << endl;
cout << "相除后p3.m_A=" << p3.m_A << endl << "相除后p3.m_B=" << p3.m_B << endl << endl;
Person p4 = p1 / 100;
cout << "Person p4 = p1 / 100;" << endl;
cout << "相除后p4.m_A=" << p4.m_A << endl << "相除后p4.m_B=" << p4.m_B << endl;
}
int main() {
test01();
system("pause");
return 0;
}
重载*号
#include<iostream>
using namespace std;
#include<string>
// 加号运算符重载
class Person {
public:
int m_A;
int m_B;
// 1.通过成员函数重载*号
/*Person operator*(Person& p) {
Person temp;
temp.m_A = this->m_A * p.m_A;
temp.m_B = this->m_B * p.m_B;
return temp;
}
*/
};
// 2.全局函数重载*号
Person operator*(Person& p1, Person& p2) {
Person temp;
temp.m_A = p1.m_A * p2.m_A;
temp.m_B = p1.m_B * p2.m_B;
return temp;
}
// 运算符重载,也可以发生函数重载
Person operator*(Person& p, int num) {
Person temp;
temp.m_A = p.m_A * num;
temp.m_B = p.m_B * num;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 15;
cout << "p1.m_a=" << p1.m_A << endl << "p1.m_B=" << p1.m_B << endl << endl;
Person p2;
p2.m_A = 20;
p2.m_B = 25;
cout << "p2.m_A=" << p2.m_A << endl << "p2.m_B=" << p2.m_B << endl << endl;
Person p3 = p1 * p2;
cout << "Person p3 = p1 * p2" << endl;
cout << "相乘后p3.m_A=" << p3.m_A << endl << "相乘后p3.m_B=" << p3.m_B << endl << endl;
Person p4 = p1 * 100;
cout << "Person p4 = p1 * 100;" << endl;
cout << "相乘后p4.m_A=" << p4.m_A << endl << "相乘后p4.m_B=" << p4.m_B << endl;
}
int main() {
test01();
system("pause");
return 0;
}