算数运算符重载
在c++中可以重载的算术运算符有:+,-,*,/,%
其他的不多说上代码:
#include<iostream>
using namespace std;
//双目算术运算符重载
class count1 {
//友元函数重载
??? friend count1 operator+(const count1& p1, const count1& p);
??? friend count1 operator-(const count1& p1, const count1& p);
??? friend count1 operator*(const count1& p1, const count1& p);
??? friend count1 operator%(const count1& p1, const count1& p);
??? friend count1 operator/(const count1& p1, const count1& p);
private:
??? int a;
??? int b;
public:
??? //构造函数
??? count1(int a, int b) :a(a), b(b) {};
??? //拷贝构造函数
??? count1(const count1& p) {
???????? cout << "拷贝函数" << endl;
???????? this->a = p.a;
???????? this->b = p.b;
??? }
??? ~count1() {}
??? double aValue() {
???????? return this->a;
??? }
??? double bValue() {
???????? return this->b;
??? }
??? //成员函数运算符重载
??? //加号重载
??? count1 operator+(const count1& p) {
???????? count1 temp(0, 0);
???????? temp.a = this->a + p.a;
???????? temp.b = this->b + p.b;
???????? return temp;
??? }
??? count1 operator-(const count1& p) {
???????? count1 temp(0, 0);
???????? temp.a = this->a - p.a;
???????? temp.b = this->b - p.b;
???????? return temp;
??? }
??? count1 operator*(const count1& p) {
???????? count1 temp(0, 0);
???????? temp.a = this->a * p.a;
???????? temp.b = this->b * p.b;
???????? return temp;
??? }
??? count1 operator/(const count1& p) {
???????? count1 temp(0, 0);
???????? temp.a = this->a / p.a;
???????? temp.b = this->b /p.b;
???????? return temp;
??? }
??? count1 operator%(count1& p) {
???????? count1 temp(0, 0);
???????? if (p.a == 0 || p.b == 0) {
???????????? cout << "除数不能等于0";
???????????? return temp;
???????? }
???????? temp.a = this->a %p.a;
???????? temp.b = this->b % p.b;
???????? return temp;
??? }
};
//友元函数重载
count1 operator+(const count1& p1, const count1& p) {
??? count1 temp(0, 0);
??? temp.a = p1.a + p.a;
??? temp.b = p1.b + p.b;
??? return temp;
}
count1 operator-(const count1& p1, const count1& p) {
??? count1 temp(0, 0);
??? temp.a = p1.a - p.a;
??? temp.b = p1.b - p.b;
??? return temp;
}
count1 operator*(const count1& p1, const count1& p) {
??? count1 temp(0, 0);
??? temp.a = p1.a - p.a;
??? temp.b = p1.b - p.b;
??? return temp;
}
count1 operator/(const count1& p1, const count1& p) {
??? count1 temp(0, 0);
??? temp.a = p1.a - p.a;
??? temp.b = p1.b - p.b;
??? return temp;
}
count1 operator%(const count1& p1, const count1& p) {
??? count1 temp(0, 0);
??? temp.a = p1.a - p.a;
??? temp.b = p1.b - p.b;
??? return temp;
}
void test() {
??? count1 add(10, 20);
??? count1 add1(10, 20);
??? //加号运算符
??? cout << "加号运算符" << endl;
??? count1 add2 = add + add1;
??? cout << "add2.a=" << add2.aValue() << endl;//add2.a=20
??? cout << "add2.b=" << add2.bValue() << endl;//add2.b = 40
??? //减号运算符
??? cout << "减号运算符" << endl;
??? count1 add3 = add2 - add1;
??? cout << "add3.a=" << add3.aValue() << endl;//add3.a=10
??? cout << "add3.b=" << add3.bValue() << endl;//add3.b=20
??? //乘号运算符重载
??? cout << "乘号运算符" << endl;
??? count1 add4 = add3 * add2;
??? cout << "add4.a=" << add4.aValue() << endl;//add4.a=200
??? cout << "add4.b=" << add4.bValue() << endl;//add4.b=800
??? //除号运算符重载
??? cout << "除号运算符" << endl;
??? count1 add5 = add3 / add2;
??? cout << "add5.a=" << add5.aValue() << endl;//add5.a=0 由于a为int类型,所以为0。若为浮点类型,则为0.5
??? cout << "add5.b=" << add5.bValue() << endl;//add5.b=0 由于b为int类型,所以为0。若为浮点类型,则为0.5
???
//取余运算符重载
??? cout << "乘号运算符" << endl;
??? count1 add6 = add3 % add2;
??? cout << "add6.a=" << add6.aValue() << endl;//10
??? cout << "add6.b=" << add6.bValue() << endl;//20
}
void main1() {
??? test();
??? system("pause");
}
|