c++第五次作业
运算符重载
运算符重载是对已有的运算符赋予多重类型,使同一个运算符作用于不同类型的数据时导致不同的行为。
运算符重载的实质就是函数重载。在实现过程中,首先把指定的运算表达式转化为对运算符函数的调用,将运算对象转化为运算符函数的实参,然后根据实参的类型来确认需要调用的函数。operator是定义运算符重载函数的关键字。
大多数的重载运算符可被定义为普通的非成员函数或者被定义为类的成员函数。如果定义函数为类的非成员函数,函数传递参数的个数与原操作数个数相同。如果定义函数为类的成员函数,函数的参数个数比原来的操作数个数要少一个。
下面是可重载的运算符列表
双目算术运算符: + (加),- (减),* (乘),/ (除),% (取模)
关系运算符: ==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
逻辑运算符: ||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符: + (正),- (负),* (指针),& (取地址)
自增自减运算符: ++ (自增),-- (自减)
位运算符: | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符: =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
其他运算符: ()(函数调用),->(成员访问),,(逗号),[](下标)
下面是不可重载的运算符列表
. :成员访问运算符
.* , ->* :成员指针访问运算符
:: :域运算符
sizeof :长度运算符
?: :条件运算符
# :预处理符号
运算符重载为类的成员函数的一般语法形式为
返回类型 operator 运算符(形参表) { 函数体 }
运算符重载为非成员函数的一般语法形式为
返回类型 operator 运算符(形参表) { 函数体 }
例1 类的成员函数运算符重载
#include<iostream> using namespace std; class Complex { public: Complex(double r=0.0,double i=0.0) : real(r) , imag(i) {}//构造函数 Complex operator+(const Complex& c2) const;//运算符+重载成员函数 Complex operator-(const Complex& c2) const;//运算符-重载成员函数 void display() const;//输出函数 private: double real;//复数实部 double imag;//复数虚部 }; Complex Complex::operator+(const Complex& c2) const//重载运算符函数实现 { return Complex(real + c2.real, imag + c2.imag);//创建一个临时无名对象作为返回值 } Complex Complex::operator-(const Complex& c2) const//重载运算符函数实现 { return Complex(real - c2.real, imag - c2.imag);//创建一个临时无名对象作为返回值 } void Complex::display() const { cout << "(" << real << "," << imag << ")" << endl; } int main() { Complex c1(5, 4), c2(2, 10), c3; cout << "c1=";c1.display(); cout << "c2=";c2.display(); c3 = c1 - c2; cout << "c3=c1-c2=";c3.display(); c3 = c1 + c2; cout << "c3=c1+c2=";c3.display(); return 0; }
输出结果
c1=(5,4)
c2=(2,10)
c3=c1-c2=(3,-6)
c3=c1+c2=(7,14)
例2 将单目运算符“++”重载为成员函数形式
#include<iostream> using namespace std; class Point { private: double x, y; public: Point(double x1=0.0,double y1=0.0) : x(x1),y(y1) {}//构造函数 Point& operator++();//前置单目运算符重载 Point operator++(int);//后置单目运算符重载 Point& operator--();//前置单目运算符重载 Point operator--(int);//后置单目运算符重载 void display() const;//输出函数 }; Point& Point::operator++() { x++; y++; return *this; } Point Point::operator++(int) { Point old; old = *this; ++(*this); return old; } Point& Point::operator--() { x--; y--; return *this; } Point Point::operator--(int) { Point old; old = *this; --(*this); return old; } void Point::display() const { cout << "(" << x << "," << y << ")" << endl; } int main() { Point a(1, 1); a.display(); (a++).display(); (++a).display(); (a--).display(); (--a).display(); return 0; }
输出结果
(1,1)
(1,1)
(3,3)
(3,3)
(1,1)
例3 以非成员函数形式重载Complex的加减法运算
非成员函数重载一般用友元函数实现
#include<iostream> using namespace std; class Complex { public: Complex(double r=0.0,double i=0.0) : real(r) , imag(i) {}//构造函数 friend Complex operator+(const Complex& c1,const Complex& c2);//运算符+重载函数 friend Complex operator-(const Complex& c1,const Complex& c2);//运算符-重载函数 void display() const;//输出函数 private: double real;//复数实部 double imag;//复数虚部 }; Complex operator+(const Complex& c1,const Complex& c2)//重载运算符函数实现 { return Complex(c1.real + c2.real, c1.imag + c2.imag);//创建一个临时无名对象作为返回值 } Complex operator-(const Complex& c1,const Complex& c2)//重载运算符函数实现 { return Complex(c1.real - c2.real, c1.imag - c2.imag);//创建一个临时无名对象作为返回值 } void Complex::display() const { cout << "(" << real << "," << imag << ")" << endl; } int main() { Complex c1(5, 4), c2(2, 10), c3; cout << "c1=";c1.display(); cout << "c2=";c2.display(); c3 = c1 - c2; cout << "c3=c1-c2=";c3.display(); c3 = c1 + c2; cout << "c3=c1+c2=";c3.display(); return 0; }
例4 “++”运算符的非成员函数重载
#include<iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(double r=0.0,double i=0.0) : real(r),imag(i){}//构造函数 friend Complex& operator++( Complex &c1);//前置“++”运算符的非成员函数重载 friend Complex operator++( Complex &c2,int);//后置“++”运算符的非成员函数重载 void display() const;//输出函数 }; Complex& operator++( Complex& c1) { c1.real++; c1.imag++; return c1; } Complex operator++( Complex& c2, int) { Complex c; c = c2; ++c2.real; ++c2.imag; return c; } void Complex::display() const { cout << "(" << real << "," << imag << ")" << endl; } int main() { Complex c(5, 4); c.display(); (c++).display(); (++c).display(); return 0; }
运行结果
(5,4)
(5,4)
(7,6)