收藏 分销(赏)

湖南大学软件学院MINI数据库项目演示课件.ppt

上传人:精**** 文档编号:1846972 上传时间:2024-05-10 格式:PPT 页数:19 大小:2.17MB
下载 相关 举报
湖南大学软件学院MINI数据库项目演示课件.ppt_第1页
第1页 / 共19页
湖南大学软件学院MINI数据库项目演示课件.ppt_第2页
第2页 / 共19页
湖南大学软件学院MINI数据库项目演示课件.ppt_第3页
第3页 / 共19页
湖南大学软件学院MINI数据库项目演示课件.ppt_第4页
第4页 / 共19页
湖南大学软件学院MINI数据库项目演示课件.ppt_第5页
第5页 / 共19页
点击查看更多>>
资源描述

1、IBM researchC+How to Program编程与应用编程与应用IBM researchC+How to ProgramoutlineslRational Class Caselreviewlfriend Functionslopeator overloadlExtensionClasses and Objects IIIBM researchC+How to Programclass Rational/数据成员:分子,分母;int fm;int fz;/成员函数:输出信息;void show();定义一种数据类型:Rational,支持分数形式的有理数的输入输出和四则运算。如:3

2、/4+1/3=7/12 3/4*1/3=1/4private:public:int main()/声明一个有理数类型的对象声明一个有理数类型的对象Rational r;/输出输出r的值,调用的值,调用r的成员函的成员函数数showr.show();void show()coutfz/fmfm)*(y.fm);result.fz=(this-fz)*(y.fz);return result;Rational增加加法操作?增加加法操作?RationalIBM researchC+How to Program/rational.hclass Rational /数据成员声明;/成员函数声明;程序太长

3、。/rational.cpp/成员函数定义Rational:Rational().void Rational:show().int main()Rational a;a.show();/对象声明 /成员函数的调用#include“rational.h”IBM researchC+How to ProgramUser-defined(programmer-defined)types:classesData(data members)Functions(member functions or methods)Class instance:objectclass TypeName.;Member a

4、ccess specifierspublic:Accessible wherever object of class in scopeprivate:Accessible only to member functions of classprotected:Objects of classAfter class definition,Class name is new type specifierOperators to access class membersoperator(.)Object or Reference to objectoperator(-)PointersReviewIB

5、M researchC+How to ProgramClass scope Data members,member functionsWithin class scopeImmediately accessible by all member functionsReferenced by nameOutside class scopeReferenced through handlesObject name,reference to object,pointer to objectMember functions defined outside classBinary scope resolu

6、tion operator(:)Format for defining member functionsReturnType ClassName:MemberFunctionName()ReviewIBM researchC+How to ProgramConstructor functionSame name as classInitializes data membersCalled when object instantiatedSeveral constructorsFunction overloadingNo return typeDestructor functionSame na

7、me as class,Preceded with tilde()No return type&No arguments Cannot be overloadedPerforms“termination housekeeping”ReviewIBM researchC+How to ProgramTHANK YOUSUCCESS2024/5/8 周三周三10可编辑可编辑IBM researchC+How to Programfriend Functionsfriend function Delaration in classs scope Begin with keyword friend D

8、efined outside classs scope Right to access non-public membersformat:class A friend B();Properties of friendshipFriendship granted,not takenClass A must explicitly declare function B friendNot symmetric Not transitive IBM researchC+How to Programclass Rational /增加友元函数:相乘 rational multiply();修改乘法运算,使

9、它变成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 yIBM researchC

10、+How to Program通过函数调用来实现相乘,仍然不直观int main()Rational a(1,4);Rational b(1,3);Rational c;/c=multiply(a,b);/调用友元函数调用友元函数/c=a.multiply(b);/调用成员函数调用成员函数c=a*b;Overloading operatorsCreate a friend function for the classFunctionName operator followed by symboloperator*for the multiply operator*Can use existin

11、g operators with user-defined typesIBM researchC+How to Programclass 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.

12、fm)*(y.fm);result.fz=(x.fz)*(y.fz);return result;IBM researchC+How to ProgramOverloaded operatorLeft operand of type ostream&Such as cout object in cout needs istream&Thus,both must be non-member functionsclass Rational /增加友元函数:重载运算符friend ostream&operator(ostream&,Rational x);ostream&operator(ostre

13、am&output,Rational x)outputx.fz/x.fm;return output;int main()coutab;实现的重载,Rational a;cout operatorLeft operand of type istream&Such as cout object in cin classObjectmust be friend functionsclass Rational /增加友元函数:重载运算符friend istream&operator(istream&input,Rational x)inputx.fzx.fm;return input;int main()cinab;实现的重载,Rational a;cina;IBM researchC+How to ProgramIBM researchC+How to Program最简分数形式递归int gcd(int x,int y)/辗转相除private调用/构造函数中调用支持小数和分数的互换带前台界面性能扩展IBM researchC+How to ProgramTHANK YOUSUCCESS2024/5/8 周三周三19可编辑可编辑

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
百度文库年卡

猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服