第六章运算符重载
C++基础系列——运算符重载

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运算符重载其实就是定义⼀个函数,在函数体内实现想要的功能,当⽤到该运算符时,编译器会⾃动调⽤这个函数,它本质上是函数重载。
chap6_运算符重载

//testComplex.cpp #include <iostream.h> #include"complex.h" void main() { Complex a(10,10),b(15,15),c; a.disp(); b.disp(); c=a.addition(b); c.disp(); c=a.subtraction(b); c.disp(); }
• 单目运算符
– 无参数 – 程序中出现
• 运算符C1 或 运算符 C1, 例如 : –C1 • 编译器将其解释为C1.operator 运算符 例如 C1.operator –() 运算符(),例如
• 有些运行符只能重载为成员函数,而不能重载为 有些运行符只能重载为成员函数, 友元函数,它们是: 友元函数,它们是:=, (), [] 和 -> >
运算符函数的使用
//testComplex.cpp #include <iostream.h> #include"complex.h" void main() { Complex a(10,10),b(15,15),c; a.disp(); b.disp(); c=a+b; c.disp(); c=a-b; c.disp(); }
可否写做: 可否写做: c=a+b; c=a-b;
10+10i 15+15i 25+25i -5-5i
2 运算符函数 operator function
• 在对复数对象进行算术运算时,使用算术运算符 应最直观最方便,如a+b,那么能否用相似的语法 计算两个复数类对象的和?
– a+b
6运算符重载

6.1.2 运算符重载的语法形式
➢ 运算符通常是对类中的私有成员进行操作,故重载运算符应 能访问类中的私有成员,所以重载运算符一般采用成员函数或 友员函数的形式。 ➢ 成员函数的语法形式为:
类型 类名 :: operator op ( 参数表 )
{
// 相对于该类定义的操作
else
{ cout << "\n Data overflow !" << endl ;
Hale Waihona Puke #include<iostream.h> #include<stdlib.h> class Calculator { public:
Calculator() { value = 0 ; } ; void operator ++ () ; void operator -- () ; unsigned int operator() () ; private: unsigned int value; }; void main()
第6章 运算符重载
运算符重载使得用户自定义的数据以一种更简洁的方式工作,
就是赋予已有的运算符多重含义。
能表示为
例如
? c1 = c1 + c2 ;
int x , y ; y=x+y;
complex c1 , c2 ; c1 = Cadd (c1 , c2 ) ;
定能义表示为
? // 运复m算数1类符=对重m象载1 +函m数2 ;
{
// 相对于该类定义的操作
} ➢ 一个运算符被重载后,原有意义没有失去,只是定义了相对 一特定类的一个新运算符
C++程序设计简明教程2Eppt 第6章运算符重载PPT教学课件

2020/12/11
7
重载 = 运算符
➢ 赋值运算符“=”可以被重载,但不能被继承 ➢ 类中重载赋值运算符依旧延续赋值运算符原有含义 ➢ 即将赋值号右边对象的私有数据依次拷贝到赋值号 左边对象的私有数据域中。
2020/12/11
8
程序6.8 重载 = 运算符举例1
const max=20 ;
class student
class date
{ int mo,da,yr;
public: date(int m,int d,int y){mo=m;da=d;yr=y;}
void display() {cout <<mo<<"/"<<da<<"/"<<yr;} friend date operator +(int,date&); date operator + (int day)
{char *name;int age;float score;
程序输出结果: 3/13/99
{ date dt=*this; day+=dt.da;
while(day>dys[dt.mo-1])
{ day-=dys[dt.mo-1];if(++dt.mo==13){dt.mo=1; dt.yr++;} }
dt.da=day; return dt;} };
date operator +(int day,date& dt) {dt=dt+day; return dt; }
dt++;
//错误语句
}
c++学习资料第 06 章 运算符重载

第 6 章 运 算 符 重 载
{ return Complex( c.Real, c.Image ); } void Complex::print()const { cout << '(' << Real << " , " << Image << ')' << endl; } void main() { Complex c1( 2.5,3.7 ), c2( 4.2, 6.5 ) ; Complex c ; c = c1 c2 ; // operator(c1,c2) c.print() ; c = 25 + c2 ; // operator+(25,c2) c.print() ; c = c2 + 25 ; // operator+(c2,52) c.print() ; c = c1 ; // operator(c1) c.print() ; } 当一个运算符的操作需要修改类对象状态时,应该以成员函数重载。例如,需要左值操 作数的运算符(如 =,*=,++ 等)应该用成员函数重载。如果以友员函数重载,可以使用 引用参数修改对象。 当运算符的操作数(尤其是第一个操作数)希望有隐式转换,则重载算符时必须用友员 函数或普通函数。 C++中不能用友员函数重载的运算符有 = () [] -> 6.3 几个典型运算符重载 本节讨论在数学类中常用的几个运算符重载的特点和应用。 6.3.1 重载 ++ 与 自增和自减运算符有前置和后置两种形式。每个重载运算符的函数都必须有明确的特 征,使编译器确定要使用的版本。C++规定,前置形式重载为一元运算符函数,后置形式重 载为二元运算符函数。 【例 64】例 62 中使用了成员函数重载++和运算符。本例用友员函数重载++运算符。 设有简单类定义 class Increase { public : Increase() ; … friend Increase operator ++ ( Increase & ) ; friend Increase operator ++ ( Increase & , int ) ; private : unsigned value ; } ; 则前置重载的实现为: Increase operator++ ( Increase & a ) { a.value ++ ;
C_运算符重载_各类详细介绍

▪ 说明
运算符重载函数 operator@()可以返回任何类型,甚至可 以是 void类型,但通常返回类型与它所操作的类的类型 相同,这样可使重载运算符用在复杂的表达式中。例如, 在例7-2中,可以将几个复数连续进行加、减、乘、除的 运算。
用友元函数重载单目运算符时,需要一个显式的操作数, 例7-3中,用友元函数重载单目运算符“-”
#include<iostream.h> class nclass{ int a,b; public:
nclass(int x=0,int y=0) { a=x;b=y;} friend nclass operator -(nclass obj); void show(); };
▪ complex operator+(complex com1,complex com2) { return complex(com1.real+com2.real,com1.imag+com2.imag;}
▪ 这种方法是直接将一个无名临时对象创建到主调函数中,那么 运行效率高于前一种。
▪ 单目运算符重载
nclass operator-(nclass obj) { obj.a=-obj.a;
obj.b=-obj.b; return obj;} void nclass::show() { cout<<"a="<<a<<" b"<<b;} ▪ main() ▪{ ▪ nclass ob1(10,20),ob2; ▪ ob1.show(); ▪ ob2=-ob1; ▪ ob2.show(); ▪ return 0; ▪}
第6章运算符重载

6.1 运算符重载基础
C++允许程序员通过重载扩展运算符的功能,使重载 后的运算符能够对用户自定义的数据类型进行运算。
比如,设有复数类Complex,其形式如下:
class Complex{ double real,image;
public: ...... };
假设定义了下面的复数对象,并且要实现两个复数相 加的运算。
}display();
};
13
void main(void) {
Complex c1(1,2),c2(3,4),c3,c4,c5,c6; c3=c1+c2; c4=c1-c2; c5=c1*c2; c6=c1/c2; c1.display(); c2.display(); c3.display(); c4.display(); c5.display(); c6.display();
② 对于不要求左值且可以交换参数次序的运算符(如+、 、*、/ 等运算符),最好用非成员形式(包括友元和 普通函数)的重载运算符函数实现。
③ 对于前面分析过的“2+c2”和“c2+2”之类的对称运算 表达式,也可以直接通过友元运算符重载实现。
21
【例6-3】 用友元运算符重载实现复数与实数的加法运 算。实数与复数的实部相加,复数的虚部保持不变。
void f(X a)
public:
{
void operator+(int); X(int); }; void operator+(X,X);
a+2; //a.operator+(2) 2+a; //::operator+(X(2),a) a+2.0; //::operator+(X,double);
运算符重载的方法

运算符重载的方法
运算符重载是指在类中重新定义某个运算符,使其能够用于类的实例。
重载运算符可以方便地对类对象进行加、减、乘、除等基本运算,也可以定义自定义运算符,以实现类特定的功能。
运算符重载的方法如下:
1.编写运算符重载函数:将要重载的运算符的符号作为函数名,如“+”、“-”、“*”、“/”等,对于自定义运算符可以自己命名。
2.指定参数个数和类型:重载函数需要参数,这些参数有不同的类型,可以是类的成员变量或其他类型的变量。
3.重载运算符要求:
(1)操作数至少有一个是类的实例,重载函数可以定义为类的成员函数或全局函数。
(2)运算符重载不能改变原有操作数的类型或个数。
(3)运算符重载函数可以返回任何类型的值,甚至可以是类的实例。
4.使用运算符:一旦重载运算符函数定义好之后,就可以像使用普通的运算符一样使用它。
需要注意的是,运算符重载不是万能的,对于某些运算符,如“&&”、“”等,是不能够重载的。
此外,在使用运算符重载时应该避免过于复杂的实现方式,以防止代码难以理解和维护。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
复数 a = 2.3 + 4.6i 复数 b = 6.7 + 9.8i
a+b = 9.0 + 14.4i a-b = -4.4 - 5.2i
. . .
. .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
编程题回顾:复数
抽象
double real; double image;
Complex AddComplex (Complex right) Complex SubComplex (Complex right) Complex MulComplex (Complex right)
. . .
. .
.
. . . . . . . .
运算符重载
耿耀君
计算机科学系
June 19, 2015
. . .
. .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
耿耀君 (计算机科学系)
运算符重载
June 19, 2015
1 / 51
1
导引 运算符重载
2
. . .
. .
耿耀君 (计算机科学系)
double real; double image;
Complex AddComplex (Complex right) Complex SubComplex (Complex right) Complex MulComplex (Complex right) Complex DivComplex (Complex right)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
耿耀君 (计算机科学系)
运算符重载
June 19, 2015
8 / 51
编程题回顾:复数 III
void Complex :: print () { cout <<'('<<real <<','<<image <<')'<<endl; }
用 Complex 表示复数,它由 实部和虚部组成,定义两个 double 类型变量 real,imag 分别表示实部和虚部. Complex AddComplex (a, b) Complex SubComplex (a, b) Complex MulComplex (a, b) Complex DivComplex (a, b)
复数 a = 2.3 + 4.6i 复数 b = 6.7 + 9.8i
. . .
. .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
编程题回顾:复数
抽象
用 Complex 表示复数,它由 实部和虚部组成,定义两个 double 类型变量 real,imag 分别表示实部和虚部. Complex AddComplex (a, b)
Complex AddComplex (Complex right)
. . .
. .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
编程题回顾:复数
抽象
复数 Complex 由实部和虚部 组成,定义两个 double 类型 变量 real,imag 分别表示实 部和虚部. Complex AddComplex (a, b) Complex SubComplex (a, b)
double real; double image;
Complex AddComplex (Complex right) Complex SubComplex (Complex right)
. . .
. .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
复数 a = 2.3 + 4.6i 复数 b = 6.7 + 9.8i
a+b = 9.0 + 14.4i
. . .
. .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
编程题回顾:复数
抽象
用 Complex 表示复数,它由 实部和虚部组成,定义两个 double 类型变量 real,imag 分别表示实部和虚部. Complex AddComplex (a, b) Complex SubComplex (a, b)
耿耀君 (计算机科学系)
运算符重载
June 19, 2015
4 / 51
编程题回顾:复数
抽象
复数 Complex 由实部和虚部 组成,定义两个 double 类型 变量 real,imag 分别表示实 部和虚部.
double real; double imHale Waihona Puke ge;. . .. .
.
. . . . . . . .
.
. . . . . . . .
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
耿耀君 (计算机科学系)
运算符重载
June 19, 2015
2 / 51
编程题回顾:复数
题目一
编写一个程序实现复数的四则运算。
提示
首先考虑如何表示复数,一个复数有实部和虚部组成,而且可以和 其他复数进行四则运算。 复数类由两部分组成,一部分数据成员表示实部和虚部,一部分成 员函数表示复数的四则运算。 在复数类里面设计一个用于初始化复数的成员函数。 在主函数中定义复数对象,验证程序能否正确进行复数四则运算
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
编程题回顾:复数
抽象
复数 Complex 由实部和虚部 组成,定义两个 double 类型 变量 real,imag 分别表示实 部和虚部. Complex AddComplex (a, b) Complex SubComplex (a, b) Complex MulComplex (a, b) Complex DivComplex (a, b)
. .
. .
. .
. .
.
编程题回顾:复数
抽象
复数 Complex 由实部和虚部 组成,定义两个 double 类型 变量 real,imag 分别表示实 部和虚部. Complex AddComplex (a, b) Complex SubComplex (a, b) Complex MulComplex (a, b)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
运算符重载
June 19, 2015
5 / 51
编程题回顾:复数
创建复数类
class Complex { private : double real; double image ; public : Complex ( double re = 0.0, double im = 0.0); Complex AddComplex ( Complex & right ); Complex SubComplex ( Complex & right ); Complex MulComplex ( Complex & right ); Complex DivComplex ( Complex & right ); void print (); };
. . . . . . . .
. . . . . . . . .
. .
. .
. .
. .
.
编程题回顾:复数
抽象
复数 Complex 由实部和虚部 组成,定义两个 double 类型 变量 real,imag 分别表示实 部和虚部. Complex AddComplex (a, b)
double real; double image;
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
耿耀君 (计算机科学系)
运算符重载
June 19, 2015
6 / 51
编程题回顾:复数 I
成员函数定义
Complex :: Complex ( double re , double im) { real = re; image = im; } Complex Complex :: AddComplex ( Complex right ) { Complex c; c.real=real+right .real; c.image =image +right .image ; return c; } Complex Complex :: SubComplex ( Complex right ) { Complex c;