已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
编程与应用,outlines,Rational Class Case review friend Functions opeator overload Extension,Classes and Objects II,class Rational /数据成员:分子,分母; int fm; int fz; /成员函数:输出信息; void show(); ;,定义一种数据类型:Rational,支持分数形式的有理数的输入输出和四则运算。 如:3/4 + 1/3 = 7/12 3/4 *1/3 = 1/4,private:,public:,int main() /声明一个有理数类型的对象 Rational r; /输出r的值,调用r的成员函数show r.show(); ,void show() coutfz“/“fmendl;,Rational:,class Rational /增加成员函数:初始化数据成员 ;,要对有理数的分子和分母进行初始化 构造函数,public:,int main() /声明对象时自动调用构造函数 Rational r; /r的值等于初始值0/1 r.show(); ,Rational:Rational() fz = 0; fm = 1;,Rational();,支持多种初始化方式,比如Rational b(1,2)? 构造函数重载,class Rational /增加成员函数:相乘 multiply( ); ;,要对有理数进行乘法运算,public:,int main() Rational a(1,4); Rational b(1,3); Rational c; /调用mutilpy() c = a.multiply(b); ,Rational Rational:multiply(Rational y) Rational result; result.fm = (this-fm) * (y.fm); result.fz = (this-fz) * (y.fz); return result;,Rational,增加加法操作?,Rational,/rational.h class Rational /数据成员声明; /成员函数声明; ;,程序太长。,/rational.cpp /成员函数定义 Rational:Rational() . void Rational:show() . .,int main() Rational a; a.show(); /对象声明 /成员函数的调用 ,User-defined (programmer-defined) types: classes Data (data members) Functions (member functions or methods) Class instance: object class TypeName . ; Member access specifiers public: Accessible wherever object of class in scope private:Accessible only to member functions of class protected: Objects of class After class definition,Class name is new type specifier Operators to access class members operator (.) Object or Reference to object operator (-) Pointers,Review,Class scope Data members, member functions Within class scope Immediately accessible by all member functions Referenced by name Outside class scope Referenced through handles Object name, reference to object, pointer to object Member functions defined outside class Binary scope resolution operator (:) Format for defining member functions ReturnType ClassName:MemberFunctionName( ) ,Review,Constructor function Same name as class Initializes data members Called when object instantiated Several constructors Function overloading No return type Destructor function Same name as class,Preceded with tilde () No return type & No arguments Cannot be overloaded Performs “termination housekeeping”,Review,2019/9/1,10,可编辑,friend Functions,friend function Delaration in classs scope Begin with keyword friend Defined outside classs scope Right to access non-public members format: class A friend B(); Properties of friendship Friendship granted, not taken Class A must explicitly declare function B friend Not symmetric Not transitive,class Rational /增加友元函数:相乘 rational multiply( ); ;,修改乘法运算,使它变成rational类的友元函数,int main() Rational a(1,4); Rational b(1,3); Rational c; c = multiply(a,b); ,Rational multiply(Rational x,Rational y) Rational result; result.fm = (x.fm) * (y.fm); result.fz = (x.fz) * (y.fz); return result;,friend,友元函数不属于该类,是个旁观者,所以每个操作数都必须声明,Rational x,Rational y,通过函数调用来实现相乘,仍然不直观,int main() Rational a(1,4); Rational b(1,3); Rational c; /c = multiply(a,b);/调用友元函数 /c = a.multiply(b);/调用成员函数 c = a * b; ,Overloading operators Create a friend function for the class FunctionName operator followed by symbol operator* for the multiply operator * Can use existing operators with user-defined types,class Rational /增加友元函数:重载乘法运算符* /friend Rational multiply(Rational x,Rational y); ;,实现乘法运算符*的重载,使它支持有理数类型的数据,friend Rational opeator*(Rational x,Rational y);,int main() c = a * b; ,Rational opeator* (Rational x,Rational y) Rational result; result.fm = (x.fm) * (y.fm); result.fz = (x.fz) * (y.fz); return result;,Overloaded needs istream ,实现的重载
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国蔬菜机行业市场前景预测及投资价值评估分析报告
- 中国螺纹套丝机行业市场前景预测及投资价值评估分析报告
- 中国装卸码头升降机行业市场前景预测及投资价值评估分析报告
- 中国调距切纸刀行业市场占有率及投资前景预测分析报告
- 中国起重装卸机行业市场前景预测及投资价值评估分析报告
- DB3311∕T 48─2015 板栗主要病虫生态控制技术规程
- 中国超高强度抽油杆行业市场占有率及投资前景预测分析报告
- DB3509∕T 002-2022 养殖大黄鱼等级评定技术规范
- DB3201∕T 1136-2023 紫云英-晚粳稻高效轮作栽培技术规程
- 《6 的乘法口诀》教学设计-2024-2025学年二年级上册数学人教版
- 《国际散装运输危险化学品船舶构造和设备规则》(《IBC 规则》)
- 关节镜设备及器械
- 第十七章-全身系统性疾病在口腔的表现
- 干部人事档案目录(样表)
- GB/T 5080.7-1986设备可靠性试验恒定失效率假设下的失效率与平均无故障时间的验证试验方案
- 人物《袁隆平》PPT介绍
- GB/T 13576.1-2008锯齿形(3°、30°)螺纹第1部分:牙型
- 内蒙古鄂尔多斯煤矿
- 高中英语3500词汇
- 幼儿园中班社会:《田野里有什么》 课件
- 集中供水点项目邀标书
评论
0/150
提交评论