




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GSNOR-IN-1-生命科学试剂-MCE
- 2025福建泉州晋江国际机场招聘25人模拟试卷及答案详解(新)
- 安全培训效果评价计划课件
- 2025吉林农业大学招聘高层次人才7人模拟试卷及参考答案详解1套
- 广州货架安全管理培训课件
- 2025年第2批次浙江宁波前湾产业集团有限公司招聘9人考前自测高频考点模拟试题及完整答案详解
- 首饰产品质量承诺书6篇
- 智能制造产业智能化生产
- 2025福建泉州市安溪县部分公办学校专项招聘编制内新任教师65人(三)考前自测高频考点模拟试题及参考答案详解
- 直播带货合伙人合同协议书5篇
- 2023混凝土结构耐久性电化学修复技术规程
- 万科郡西别墅课件
- 西南科技大学833材料科学基础2016-2022年考研初试真题
- 香港注册社会工作者工作守则
- GB/T 15115-1994压铸铝合金
- GB/T 12357.1-2004通信用多模光纤第1部分:A1类多模光纤特性
- 胸外科围手术期呼吸功能锻炼的意义培训课件
- 朗读技巧与朗读教学课件
- (新版)海南自由贸易港建设总体方案考试题库(含答案)
- 丰田汽车新产品开发及项目管理
- 人教版新教材高中英语选择性必修一全册课文及翻译(中英word)
评论
0/150
提交评论