当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

中国水利水电出版社:《C++语言程序设计》课程电子教案(PPT教学课件)第8章 多态性

资源类别:文库,文档格式:PPSX,文档页数:43,文件大小:271.04KB,团购合买
8.1 运算符重载 8.2 运算符重载为类的成员函数 8.3 运算符重载为类的友元函数 8.4 虚函数
点击下载完整版文档(PPSX)

第8章多态性 8.1运算符重载 82运算符重载为类的成员函数 83运算符重载为类的友元函数 84虚函数

第8章 多态性 8.1 运算符重载 8.2 运算符重载为类的成员函数 8.3 运算符重载为类的友元函数 8.4 虚函数

第8章多态性 8.1运算符重载 81.1问题的提出 例6.3的复数类 CComplex CComplex: Add(ccomplex c) include iostream h class CComplex CComplex temp; private: temp real real + c real; double real; temp imag imag C imag; double imag return temp; public CComplex(double r, double i);i CComplex CComplex: Sub(CComplex c) void print(; CComplex Add (ccomplex c); CComplex temp; cComplex Sub(CComplex c); temp. real real -C real; temp imag imag-C imag; return temp;

8.1 运算符重载 8.1.1 问题的提出 例6.3的复数类 #include "iostream.h" class CComplex { private: double real; double imag; public: CComplex(double r, double i); void Print(); CComplex Add(CComplex c); CComplex Sub(CComplex c); }; 第8章 多态性 CComplex CComplex::Add(CComplex c) { CComplex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } CComplex CComplex::Sub(CComplex c) { CComplex temp; temp.real = real - c.real; temp.imag = imag - c.imag; return temp; }

第8章多态性 8.1运算符重载 81.1问题的提出(续一) void main(void) CComplex a(1, 2),b(3.0,4.0),c, d C=a.Add(b; d =a sub(b); cout≤<"c="; 复数加减法只能调用成员函数实现, 不能使用符号“+”和“”,可以通 C Print(; 过重载“+”、“-”运算符,实现如 cout <<"d=w c≡a+b这样的调用方式 d Print(; 运算符重载:运算符重载的实质就是对已有的运算符赋予多重含义,使同一个运算符 作用于不同类型的数据时,产生不同的行为。运算符重载的实质就是函数重载

8.1 运算符重载 8.1.1 问题的提出(续一) void main(void) { CComplex a(1, 2), b(3.0, 4.0), c,d; c = a.Add(b); d = a.Sub(b); cout << "c = "; c.Print(); cout << "d = "; d.Print(); } 第8章 多态性 复数加减法只能调用成员函数实现, 不能使用符号“+”和“-”,可以通 过重载“+”、“-”运算符,实现如 c=a+b这样的调用方式 运算符重载:运算符重载的实质就是对已有的运算符赋予多重含义,使同一个运算符 作用于不同类型的数据时,产生不同的行为。运算符重载的实质就是函数重载

第8章多态性 例8.1用运算符实现复数的加减运算 include"iostream h class CComplex private: double real: double imag public CComplex(double r=0, double i=o) void Printo CComplex operator +(CComplex c) CComplex operator-(CComplex c) CComplex: CComplex (double r, double i eal=r Imag=I

例8.1 用运算符实现复数的加减运算 #include "iostream.h" class CComplex { private: double real; double imag; public: CComplex(double r=0, double i=0); void Print(); CComplex operator +(CComplex c); CComplex operator -(CComplex c); }; CComplex::CComplex (double r, double i) { real = r; imag = i; } 第8章 多态性

第8章多态性 例8.1(续一) void CComplex. Printo cout≤<"("<srea<<","<<imag≤")"<<endl; CComplex CComplex:: operator +(CComplex c) CComplex temp temp. real real c real temp. imag= imag c imag return temp; CComplex CComplex operator-(CComplex c) CComplex temp H temp. real=real-creal temp. imag= imag -C imag turn temp

例8.1 (续一) void CComplex::Print() { cout << "(" << real << "," << imag << ")" << endl; } CComplex CComplex::operator +(CComplex c) { CComplex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } CComplex CComplex::operator -(CComplex c) { CComplex temp; temp.real = real - c.real; temp.imag = imag - c.imag; return temp; } 第8章 多态性

第8章多态性 例8.1(续二) void main(void) CComplex a(1,2),b(3.0,4.0),c,d; c= atb d= a-b: Cout <s uc= 该语句相当于对函数。 perator+( CComplex c) C Printo 的调用:“c= a operator+(b)”,实现两个复数 的加法运算 cout < " d=" d Printo 程序运行结果为 c=(4,6) d=(-2,-2)

例8.1 (续二) void main(void) { CComplex a(1, 2), b(3.0, 4.0), c,d; c = a+b; d = a-b; cout << "c = "; c.Print(); cout << "d = "; d.Print(); } 第8章 多态性 该语句相当于对函数operator +(CComplex c) 的调用:“c=a.operator +(b)”,实现两个复数 的加法运算。 程序运行结果为: c = (4, 6) d = (-2, -2)

第8章多态性 8.1运算符重载 81.2运算符重载的格式与规则 1.运算符重载的格式 运算符重载为类的成员函数 运算符重载为类的友元函数 运算符重载的为类的成员函数,在类中声明的格式为: 函数类型 operator运算符(参数表); 定义该函数的格式: 函数类型类名 operator运算符(参数表) 函数体 也可以将重载运算符函数的定义直接写在类中

8.1 运算符重载 8.1.2 运算符重载的格式与规则 1. 运算符重载的格式 ➢ 运算符重载为类的成员函数 ➢ 运算符重载为类的友元函数 运算符重载的为类的成员函数,在类中声明的格式为: 函数类型 operator 运算符(参数表); 定义该函数的格式: 函数类型 类名::operator 运算符(参数表) { 函数体; } 也可以将重载运算符函数的定义直接写在类中。 第8章 多态性

第8章多态性 8.1运算符重载 8.1.2运算符重载的格式与规则(续) 2.运算符重载的规则 (1)除“."、“*”、“"、“?:”和“szeo「等几个运算符不能 重载外,C++中几乎所有的运算符都可以重载 2)运算符被重载后,其优先级和结合性不会改变。 (3)不能改变运算符操作对象的个数。 返回

8.1 运算符重载 8.1.2 运算符重载的格式与规则(续) 2. 运算符重载的规则 (1)除“.”、“*”、“::”、“?:”和“sizeof”等几个运算符不能 重载外,C++中几乎所有的运算符都可以重载。 (2)运算符被重载后,其优先级和结合性不会改变。 (3)不能改变运算符操作对象的个数。 第8章 多态性 返 回

第8章多态性 8.2运算符重载为类的成员函数 821双目运算符重载 双目运算符,如果重载为类的成员函数,其参数为一个,即 比运算对象少一个。 例82复数的乘法运算,在上例的基础上添加乘法运算符重 载函数。复数类乘法运算的定义如下: (a+bi)*(Ⅺyi)=a*Xb’y+(a’y+b*x)i

8.2 运算符重载为类的成员函数 8.2.1 双目运算符重载 双目运算符,如果重载为类的成员函数,其参数为一个,即 比运算对象少一个。 例8.2 复数的乘法运算,在上例的基础上添加乘法运算符重 载函数。复数类乘法运算的定义如下: (a+bi)*(x+yi)= a*x-b*y + (a*y + b*x)i 第8章 多态性

第8章多态性 例8.2复数乘法运算源程序 #include iostream h class CComplex private double real double imag; public CComplex(double r=0, double i =O) void Printo CComplex operator +(CComplex c) CComplex operator-(CComplex c) CComplex operator *(CComplex c) CComplex C Complex(double r, double i real =r Imag

例8.2 复数乘法运算源程序 #include "iostream.h" class CComplex { private: double real; double imag; public: CComplex(double r=0, double i=0); void Print(); CComplex operator +(CComplex c); CComplex operator -(CComplex c); CComplex operator *(CComplex c); }; CComplex::CComplex (double r, double i) { real = r; imag = i; } 第8章 多态性

点击下载完整版文档(PPSX)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共43页,可试读15页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有