第四讲运算符重载
- 格式:pdf
- 大小:505.19 KB
- 文档页数:20
C++基础系列——运算符重载1. 运算符重载简介所谓重载,就是赋予新的含义。
函数重载(Function Overloading)可以让⼀个函数名有多种功能,在不同情况下进⾏不同的操作。
同样运算符重载(Operator Overloading)可以让同⼀个运算符可以有不同的功能。
可以对 int、float、string 等不同类型数据进⾏操作<< 既是位移运算符,⼜可以配合 cout 向控制台输出数据也可以⾃定义运算符重载:class Complex{public:Complex();Complex(double real, double imag);Complex operator+(const Complex &a) const;void display() const;private:double m_real;double m_imag;};// ...// 实现运算符重载Complex Complex::operator+(const Complex &A) const{Complex B;B.m_real = this->m_real + A.m_real;B.m_imag = this -> m_imag + A.m_imag;return B;// return Complex(this->m_real + A.m_real, this->m_imag + A.m_imag);}int main(){Complex c1(4.3, 5.8);Complex c2(2.7, 3.7);Complex c3;c3 = c1 + c2; // 运算符重载c3.display();return 0;}运算结果7 + 9.5i运算符重载其实就是定义⼀个函数,在函数体内实现想要的功能,当⽤到该运算符时,编译器会⾃动调⽤这个函数,它本质上是函数重载。
1、多态性的基本概念2、派生类对象替换基类对象3、虚函数的定义4、抽象类的定义5、宠物类的设计6、运算符重载7、日期类对象判断大小8、分数类对象运算符重载☐运算符重载指赋予运算符新的操作功能,主要用于对类的对象的操作☐运算符+意味着多少对象类型的加法呢?☐还可以定义新的对象类型加法☐运算符重载定义形式:<类型><类名>::operator<操作符>(<参数表>){函数体}☐首先定义虚数类☐虚数可以描述为:a+bi☐a与b看成实数,定义成double类型☐成员函数除了构造与析构外,还有:☐输出虚数、修改虚数、得到实部a、得到虚部b ☐相加+、判相等==#include <iostream>using namespace std;class Complex{private:double real, imag;public:Complex(double r = 0, double i = 0): real(r), imag(i){ }double Real(){return real;}double Imag(){return imag;}Complex operator +(Complex&);Complex operator +(double);bool operator ==(Complex);~Complex(){ };Complex Complex::operator +(Complex &c)// 重载运算符+,两边是虚数对象{Complex temp;temp.real = real+c.real;temp.imag = imag+c.imag;return temp;}Complex Complex::operator +(double d)// 重载运算符+,左边是虚数对象,右边是双精度数{Complex temp;temp.real = real+d;temp.imag=imag;return temp;}bool Complex::operator ==(Complex c)// 重载运算符=={if (real == c.real && imag == c.imag)return true;elseint main(){Complex c1(3,4),c2(5,6),c3;cout << "C1 = " << c1.Real() << "+j" << c1.Imag() << endl;cout << "C2 = " << c2.Real() << "+j" << c2.Imag() << endl;c3 = c1+c2;cout << "C3 = " << c3.Real() << "+j" << c3.Imag() << endl;c3 = c3+6.5;cout << "C3 + 6.5 = " << c3.Real() << "+j" << c3.Imag() << endl;if ( c1==c2 )cout<<“两个复数相等”;elsecout<<“两个复数不相等”;return 0;☐运算符++分前置运算符和后置运算符☐例如: ++Y与Y++☐前置运算符定义Complex Complex::operator ++ () {real+=1;return *this;}☐后置运算符定义Complex Complex::operator ++ (int) {real+=1;return *this;}。
简述运算符重载的规则。
篇一:运算符重载是C/C++语言中一种强大的功能,允许程序员自定义函数的行为,以处理不同类型的数据。
运算符重载允许程序员在函数中重载算术、逻辑和位运算符,从而能够处理数组、结构体和指针等不同类型的数据。
以下是运算符重载的规则:1. 算术运算符重载算术运算符包括加号、减号、乘号和除号。
每个算术运算符都有一组默认的行为,可以通过运算符重载来自定义它们的行为。
例如,重载加号运算符可以使函数接受一个整数参数,并返回一个新的整数。
下面是一个简单的例子,演示了如何重载加号运算符:```c++struct MyStruct {int value;};MyStruct operator+(const MyStruct& other, int value) {return MyStruct(value + other.value);}int main() {MyStruct mystruct1 = { 10 };MyStruct mystruct2 = { 20 };int result = mystruct1 + mystruct2;std::cout << "result = " << result << std::endl;return 0;}```在上面的例子中,我们定义了一个名为`MyStruct`的结构体类型,其中包含一个整数类型。
然后,我们定义了一个重载加号运算符的函数,该函数接受一个整数类型的参数,并返回一个新的`MyStruct`对象。
在`main`函数中,我们定义了两个`MyStruct`对象`mystruct1`和`mystruct2`,并将它们相加,结果存储在`result`变量中。
2. 逻辑运算符重载逻辑运算符包括条件运算符和逻辑非运算符。
每个逻辑运算符都有一组默认的行为,可以通过运算符重载来自定义它们的行为。
程序设计实习(I): C++程序设计序设计实(I)C序设计第四讲运算符重载主要内容☐两种运算符重载的实现方式☐常见的运算符重载>>⏹流运算符:>> 、<<⏹自增运算符++、自减运算符--2自定义数据类型与运算符重载☐C++预定义了一组运算符,用来表示对数据的运算⏹+, -, *, /, %, ^, &, ~, !, |, =, <<, >>, != ……⏹只能用于基本的数据类型•整型、实型、字符型、逻辑型……3☐C++提供了数据抽象的手段,允许用户自己定义数据类型: 类通过调用类的成员函数对它的对象进行操作⏹通过调用类的成员函数,对它的对象进行操作☐有些时候,用类的成员函数来操作对象时,很不方便。
例如:-⏹在数学上,两个复数可以直接进行+、等运算⏹但在C++中,直接将+或-用于复数是不允许的4我希对象类型自类型☐我们希望:对一些抽象数据类型(即自定义数据类型),也能够直接使用C++提供的运算符⏹程序更简洁⏹代码更容易理解☐例如:⏹complex_a和complex_b是两个复数对象;⏹求两个复数的和, 希望能直接写:complex_a + complex_b5☐运算符重载⏹对已有的运算符(C++中预定义的运算符)赋予多重的含义⏹使同一运算符作用于不同类型的数据时导致不同类型的行为☐目的⏹扩展C++中提供的运算符的适用范围,以用于类所表示的抽象数据类型☐同一个运算符,对不同类型的操作数,所发生的行为不同⏹(5, 10i) + (4, 8i) = (9, 18i)⏹5 + 4 = 96算符重载的实质是函数重载☐运算符重载的实质是函数重载☐在程序编译时:⏹把含运算符的表达式转换成对运算符函数的调用⏹把运算符的操作数转换成运算符函数的参数⏹运算符被多次重载时,根据实参的类型决定调用哪个运算符函数⏹运算符可以被重载成普通函数,也可以被重载成类的成员函数7运算符重载为普通函数class Complex {public:Complex( double r = 0.0, double i= 0.0 ){real = r;imaginary = i;}double real; // real partd bl l//l tdouble imaginary; // imaginary part};8Complex operator+(const Complex & a ,const Complex & b){p(,return Complex( a.real+b.real,a.imaginary+b.imaginary);}(参表)表个象} // “类名参数表”就代表一个对象Complex a(1,2), b(2,3), c;Complex a(12)b(23)c;c = a + b; // 等效于c = operator+(a,b);•重载为普通函数时,参数个数为运算符目数9l C l 运算符重载为成员函数class Complex {public:Complex(double r=0.0,double m =0.0):real(r),imaginary(m){}//constructor Complex operator+(const Complex &);//addition Complex operator-(const Complex &);//subtraction private:double real;//real part double imaginary;//imaginary part }; 重载为成员函数时,参数个数为运算符目数减一10//Overloaded addition operatorComplex Complex::operator+(const Complex&operand2) {return Complex(real+operand2.real,imaginary+operand2.imaginary); }//Overloaded subtraction operatorComplex Complex::operator(const Complex&operand2)operator-{return Complex(real-operand2.real,imaginary-operand2.imaginary); }int main(){Complex x,y(4.3,8.2),z(3.3,1.1);=x y+z;//相当于什么?x=y-z;//相当于什么?return0;}11Complex x,y(4.3,8.2),z(3.3,1.1);x=y+z;//x=y.operator+(z)x=y-z;//x=y.operator-(z)t()12class Complex {赋值运算符‘=’ 重载class Complex {public:0000Complex(double r=0.0,double m =0.0):real(r),imaginary(m){}//constructor C l t +(t C l &)//dditi Complex operator+( const Complex & ); // addition Complex operator-( const Complex & ); //subtraction t C l &t (t C l &)const Complex &operator=( const Complex & ); // assignment i t private:double real; // real part d bl i i //i i double imaginary; // imaginary part };13// Overloaded = operatorconst Complex& Complex::operator=( const Complex &right ) { real = right.real;imaginary = right.imaginary;return *this;*hi}Complex x,y(4.3,8.2),z(3.3,1.1);x=y+z;//相当于什么?x=y-z;//?相当于什么14Complex x, y(4.3, 8.2), z(3.3, 1.1);Complex x y(4382)z(3311);x = y + z; // x.operator=(y.operator+(z)) x = y -z;// x.operator= (y.operator-(z))15重载赋值运算符的意义class MyString {l M St i{private: char *str;public:MyString () { str = NULL;}y g pMyString& operator = (const char * s) {if(str) delete [] str;str = new char[strlen( s)+1];t(t)strcpy(str, s);return * this;}~MyString( ) { if(str) delete [] str; };};MyString s;s = "Hello";16MyString S1, S2;S1 = “this”;S2 = “that”;S1 = S2;☐如不定义自己的赋值运算符,那么S1=S2实际上导致S1.str 和S2.str指向同一地方☐如果S1对象消亡,析构函数将释放S1.str指向的空间,则S2消亡时还要释放一次,不妥因此要在class MyString里添加成员函数:在成员函数MyString& operator = (const MyString& s) {if(str)delete[]str;if(str) delete [] str;str= new char[strlen(s.str)+1];strcpy(str, s.str);py(,);return * this;}这么做就够了吗?还有什么需要改进的地方?够有进17考虑下面语句:MyString s;s = "Hello";s = s;是否会有问题?正确写法:确写MyString & operator = (const MyString & s) {if(str == s.str)return * this;if(str)delete [] str;delete[]str;str = new char[strlen(s.str)+1];py(,);strcpy( str, s.str);return * this;}18y g为MyString类编写复制构造函数的时候,会面临和= 同样的问题,用同样的方法处理19运算符重载的注意事项☐C++不允许定义新的运算符载后算符含应该符合常习惯☐重载后运算符的含义应该符合日常习惯⏹complex_a+ complex_bword a>word b⏹word_a word_b⏹date_b= date_a+ n运算符重载不改变运算符的优先级☐☐以下运算符不能被重载:“.”,“.*”,“::”,“?:”, sizeof☐重载运算符()、[]、->或者赋值运算符=时,运算符重载函数必须声明为类的成员函数20。