C++入门学习:运载符的重载(+,-,++,--)

xiaoxiao2021-02-28  53

对于运算符的操作数,编译器只能识别基础的数据类型,如果要使用符合的数据类型(如结构体),则需要对运算符的操作数进行拓展是运算符支持复合数据类型,C++中的运算符重载就解决了这一问题。

运算符函数定义的一般格式如下:

数据类型  operator<运算符符号>(参数列表){        <函数体>

}

有一些特殊的运算符不能被重载:域解析符(::),条件运算符(?:),直接成员访问运算符(.),类成员指针引用运算符(.*),sizeof运算符(sizeof)

运算符重载分为类的内部方式和全局方式,同一种运算符,全局和内部方式只能存在一种。当无法修改左操作数的类时,使用全局函数进行重载

eg:计算虚数

class Complex{ friend Complex operator+(Complex &c1, Complex &c2);//使用到类的私有变量,需要设为友元函数 friend Complex operator+(Complex &c1, int num);public: Complex(int a = 0, int b = 0) { this->a = a; this->b = b; } void show() { printf("%d + %di\n", a, b);

}

        Complex add(Complex &c)//Complex add(Complex*const this, Complex &c) { Complex tmp(a+c.a, b+c.b); return tmp; }

Complex operator-(Complex &c)//Complex operator-(Complex*const this, Complex &c) { Complex tmp(a-c.a, b-c.b); return tmp; } private: int a;//实部 int b;//虚部

};

//全局方式:加法运算符的重载:对象+对象Complex operator+(Complex &c1, Complex &c2){ Complex tmp(c1.a+c2.a, c1.b+c2.b); return tmp;

}

//全局方式:加法运算符的重载:对象+常量Complex operator+(Complex &c1, int num){ Complex tmp(c1.a+num, c1.b); return tmp;

}

//全局方式:全局函数重载运算符int main(){ Complex c1(1,2), c2(3,4), c3; cout << "c1 = 1+2i; c2 = 3+4i" << endl; cout << "全局方式" << endl; //编译器不知道如何运算自定义类型,所以不允许直接进行运算 //需要自己写运算规则,写执行函数 // 1.编译器发现c1和c2是自定义类型 // 2.它们要进行 + 运算 // 3.调用函数执行相应的功能: operator+(c1,c2) // 4.如果存在operator+(c1,c2)则执行成功,如果不存在则报错 c3 = c1+c2;                    // operator+(c1,c2) 对象+对象 cout << "operator+(c1,c2):"; c3.show(); c3 = c1 + 10;                  // operator+(c1, 10)  对象+常量 cout << "operator+(c1, 10):"; c3.show(); return 0;

}

//内部方式:类的内部成员(函数) int main(){ Complex c1(1,2), c2(3,4), c3; cout << "c1 = 1+2i; c2 = 3+4i" << endl; cout << "内部方式" << endl; c3 = c1.add(c2);/* add省略参数c1,因为调用的是c1中的add函数, 对象c1中有this指针可提供c1的地址, 等价于 add(&c1, c2) =====>c1 + c2*/ c3 = c2.add(c1);//add(&c2, c1)=====>c2 + c1, 参数顺序要清楚,在- * /运算时会影响结果 cout << "c3 = c1 - c2" << endl; c3 = c1 - c2;                          // c1.operator-(c2) =====>  operator-(&c1, c2) cout << "operator-(&c1, c2):"; c3.show(); cout << "c3 = c2 - c1" << endl; c3 = c2 - c1;                    // c2.operator-(c1) =====> operator-(&c2, c1) cout << "operator-(&c2, c1):"; c3.show(); return 0;

}

单目运算符的重载(++,--)

#include <iostream> using namespace std; class Complex { friend Complex& operator++(Complex &c);//++i friend Complex operator++(Complex &c, int);//i++ public: Complex(int a = 0, int b = 0) { this->a = a; this->b = b; } void show() { printf("%d + %di\n", a, b); } Complex operator+(Complex &c) { Complex tmp(a+c.a, b+c.b); return tmp; } Complex& operator--() { --a; --b; return *this; } Complex operator--(int) { Complex tmp = *this; a--; b--; return tmp; } private: int a; int b; }; //++i Complex& operator++(Complex &c) { ++c.a; ++c.b; return c; } //i++ //区分前置++和后置++:通过一个int型占位参数来区分, 后置++函数中有一个int型的占位参数 Complex operator++(Complex &c, int) { Complex tmp = c; c.a++; c.b++; return tmp; } //-- int main3() { Complex c1(1,2), c2(3,4), c3; //c3 = --c1 + c2; c3 = c1-- + c2; c1.show(); c3.show(); return 0; } //后置++运算符 int main2() { return 0; }

转载请注明原文地址: https://www.6miu.com/read-2623696.html

最新回复(0)