程序设计实习 运算符重载

上传人:ji****72 文档编号:48498148 上传时间:2018-07-16 格式:PPT 页数:62 大小:160KB
返回 下载 相关 举报
程序设计实习 运算符重载_第1页
第1页 / 共62页
程序设计实习 运算符重载_第2页
第2页 / 共62页
程序设计实习 运算符重载_第3页
第3页 / 共62页
程序设计实习 运算符重载_第4页
第4页 / 共62页
程序设计实习 运算符重载_第5页
第5页 / 共62页
点击查看更多>>
资源描述

《程序设计实习 运算符重载》由会员分享,可在线阅读,更多相关《程序设计实习 运算符重载(62页珍藏版)》请在金锄头文库上搜索。

1、高等程序设计课程(C+ Programming Practice)程序设计实习运算符重载回顾:类和对象o类的定义、成员属性、成员函数、类的作用域o对象的创建、存储、访问o构造函数、析构函数n定义、调用时机o特殊的构造函数:复制构造函数、转换构造函数、初始 化列表o类的特殊成员:static成员、const成员、引用成员oconst对象o成员对象和封闭类o友元othis指针课堂问题o指出下列各题中的错误,并说明如何改正nvoid Time(int);nclass Time private:int hour = 12;int minute = 0, second = 0; nint Time(in

2、t nHour, int nMin, int nSec);o以下关于 this 指针的说法中不正确的是:nA. const成员函数内部不可以使用this 指针 B. 成员函数内的this 指针,指向成员函数所作用的对象。 C. 在构造函数内部可以使用this指针 D. 在析构函数内部可以使用 this 指针构造函数和析构 函数不能有返回 类型;成员属性不能在 类定义时初始化const成员函数内部 可以使用this 指针: 是一个const指针, 不能改变this的地址 及所指向的值课堂问题o指出下列题中的错误,并说明如何改正nclass X X (int i, int j): base(i),

3、 rem(base % j) int rem, base; o如下定义和申明中哪些是错误的,如何改正:n/example.h class Example public:static double rate = 6.5;static const int nSize = 20;static Time Time(12, 0, 0); ; /example.c #include “example.h” double Example:rate; Time Example:Time;Base应在rem之前定 义;或rem的初始化时不 使用base,直接用i静态成员变量或成员 对象不能在定义时初 始化,应在

4、.c程序中以 全局变量的方式初始 化; 静态const类型则可以课堂问题o以下程序编译、连接都能通过,请写出运行时输出的结 果。你认为没有输出的,就写“无输出“ #include using namespace std; class Sample int A; static int B; public: Sample(int a)A=a,B+=a; static void func(Sample s); ; void Sample:func(Sample s) cout 、!= 、n只能用于基本的数据类型:整型、实型、字符型 、逻辑型、ncin和cout使用运算符“”进行流操作时, 要求操作数

5、是基本数据类型oC+提供了数据抽象的手段,允许用户定义抽象数据类 型:类n通过调用类的成员函数,对它的对象进行操作o但是,在有些时候,用类的成员函数来操作对象时,很 不方便。例如n对一个群体,按照他们的体重指数进行排序:涉 及不同对象中的“体重指数”成员属性n在数学上,两个复数可以直接进行+、-等运算。 但在C+中,直接将+或-用于复数是不允许的抽象数据类型与运算符重载运算符重载o我们希望:对一些抽象数据类型,也能够直接使用C+提供的 运算符n程序更简洁n代码更容易理解o例如nbool compareQuata = Bill ”和“ word_b pdate_b = date_a + nn有时

6、使用函数调用更好 polder(student_a, student_b)的语义比student_a student_b更 清晰:年龄大小、身材高矮、体型胖瘦、o重载不改变运算符的优先级、结合性、语法结构及参数 个数o以下运算符不能被重载:“.”、“.*”、“:”、“?:”、sizeofo教材p.100列出了可重载的运算符运算符重载的形式o重载为类的成员函数 return_type operator operator_symbol(argument-list) function-body o重载为类的友员函数 friend return_type operator perator_symbol

7、(argument -list) function-body ooperator_symbol必须是C+中可以重载的运算符 符号,例如“+”、“-”、o重载运算符“调用()、下标、成员访问-或者 赋值运算符=”时,运算符重载函数必须声明为 类的成员函数运算符重载为成员函数return_type operator operator_symbol(argument-list) function-body oargument-list中参数的个数比原operator_symbol所需要的参数个数 少一个(后置“+”、“-”除外)o例如 class Complex public: Complex( d

8、ouble = 0.0, double = 0.0 ); / constructor Complex operator+( const Complex / addition private: double real; / real part double imaginary; / imaginary part ; Complex Complex:operator+( const Complex 运算符重载为成员函数实现单目运算o单目运算: op operandn假如operand是类A的对象nop应该重载为A的成员函数,该函数没有参数 preturn_type operator op() pr

9、eturn_type是op operand的类型o例如:! string_s,等价于string_s.operator!() class String public: String( const char * = “ ); / conversion/default constructor String(); / destructor bool operator!() const return length = 0;/ is String empty? private: int length; / string length char *sPtr; / pointer to start of s

10、tring ; 运算符重载为成员函数实现双目运算o双目运算:operand_1 op operand_2 n假设operand_1是类A的对象nop应该重载为A的成员函数,该函数只有一个参数 preturn_type operator op(argument_type argument) preturn_type是operand_1 op operand_2的类型 pargument_type是operand_2的类型o被重载双目运算符的两操作数类型可以相同 class String public: String( const char * = “ ); / conversion/defaul

11、t ctor String(); / destructor bool operator=( const String / test s1 = s2 private: int length; / string length char *sPtr; / pointer to start of string ; 运算符重载为成员函数实现双目运算o被重载双目运算符的两操作数类型可以不同o例如:set_a + element_a ,等价于 set_a.operator+(element_a) class CSet public: CStet(); / constructor const CSet pri

12、vate: int size; / number of elements CElement *ptrElementList; ;运算符重载为成员函数:小结o将运算符op重载为类A的成员函数时nop是单目运算:在op所在的表达式中, op右边的 操作数是类A的对象 preturn_type operator op() pop operand等价于operand.operator op()nop是双目运算:在op所在的表达式中, op左边的 操作数是类A的对象 preturn_type operator op(argument_type argument) poperand_1 op operan

13、d_2等价于operand_1.operator op(operand_2) poperand_2可以是A的对象,也可以不是运算符重载为友员函数o但是,在一些情况下,对类A进行双目运算符 op的重载时,类A的对象只能作为op所在表达式 的右操作数,左操作数不是A的对象n将流操作符“( istream public: String( const char * = “” ); / conversion/default constructor String( const String / copy constructor String(); / destructor private: int len

14、gth; / string length char *sPtr; / pointer to start of string ; 运算符重载为友员函数o实现单目运算: op operandnoperand是类A的对象nop应该重载为A的友员函数,该函数有一个参数 pfriend return_type operator op(A arg) preturn_type是op operand的类型o例如:! string_s,等价于operator!(string_s) class String friend bool operator!(const String / is String empty?

15、 public: String( const char * = “ ); / conversion/default ctor String(); / destructor private: int length; / string length char *sPtr; / pointer to start of string ; bool operator!(const String 运算符重载为友员函数o实现双目运算:operand_1 op operand_2nop被重载为A的友员函数,该函数有两个参数 pfriend return_type operator op(argT1 arg1, argT2 arg2 ) preturn_type是operand_1 op operand_2的类型 pargT1是operand

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号