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可编辑可编辑