版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
多态性8-1什么叫做多态性?在C++中是如何实现多态的?解:多态是指同样的消息被不同类型的对象接收时导致完全不同的行为,是对类的特定成员函数的再抽象。C++支持的多态有多种类型,重载(包括函数重载和运算符重载)和虚函数是其中主要的方式。8-2什么叫做抽象类?抽象类有何作用?抽象类的派生类是否一定要给出纯虚函数的实现?解:带有纯虚函数的类是抽象类。抽象类的主要作用是通过它为一个类族建立一个公共的接口,使它们能够更有效地发挥多态特性。抽象类声明了一组派生类共同操作接口的通用语义,而接口的完整实现,即纯虚函数的函数体,要由派生类自己给出。但抽象类的派生类并非一定要给出纯虚函数的实现,如果派生类没有给出纯虚函数的实现,这个派生类仍然是一个抽象类。8-3声明一个参数为整型,无返回值,名为fn1的虚函数。解:virtualvoidfn1(int);8-4在C++中,能否声明虚构造函数?为什么?能否声明虚析构函数?有何用途?解:在C++中,不能声明虚构造函数,多态是不同的对象对同一消息有不同的行为特性,虚函数作为运行过程中多态的基础,主要是针对对象的,而构造函数是在对象产生之前运行的,因此虚构造函数是没有意义的;可以声明虚析构函数,析构函数的功能是在该类对象消亡之前进行一些必要的清理工作,如果一个类的析构函数是虚函数,那么,由它派生而来的所有子类的析构函数也是虚函数。析构函数设置为虚函数之后,在使用指针引用时可以动态联编,实现运行时的多态,保证使用基类的指针就能够调用适当的析构函数针对不同的对象进行清理工作。8-5实现重载函数Double(x),返回值为输入参数的两倍;参数分别为整型、长整型、浮点型、双精度型,返回值类型与参数一样。解:源程序:#include<iostream.h>intDouble(int);longDouble(long);floatDouble(float);doubleDouble(double);intmain(){intmyInt=6500;longmyLong=65000;floatmyFloat=6.5F;doublemyDouble=6.5e20;intdoubledInt;longdoubledLong;floatdoubledFloat;doubledoubledDouble;cout<<"myInt:"<<myInt<<"\n";cout<<"myLong:"<<myLong<<"\n";cout<<"myFloat:"<<myFloat<<"\n";cout<<"myDouble:"<<myDouble<<"\n";doubledInt=Double(myInt);doubledLong=Double(myLong);doubledFloat=Double(myFloat);doubledDouble=Double(myDouble);cout<<"doubledInt:"<<doubledInt<<"\n";cout<<"doubledLong:"<<doubledLong<<"\n";cout<<"doubledFloat:"<<doubledFloat<<"\n";cout<<"doubledDouble:"<<doubledDouble<<"\n";return0;}intDouble(intoriginal){cout<<"InDouble(int)\n";return2*original;}longDouble(longoriginal){cout<<"InDouble(long)\n";return2*original;}floatDouble(floatoriginal){cout<<"InDouble(float)\n";return2*original;}doubleDouble(doubleoriginal){cout<<"InDouble(double)\n";return2*original;}程序运行输出:myInt:6500myLong:65000myFloat:6.5myDouble:6.5e+20InDouble(int)InDouble(long)InDouble(float)InDouble(double)DoubledInt:13000DoubledLong:130000DoubledFloat:13DoubledDouble:1.3e+218-6定义一个Rectangle类,有长itsWidth、宽itsLength等属性,重载其构造函数Rectangle。和Rectangle(intwidth,intlength)。解:源程序:#include<iostream.h>classRectangle{public:Rectangle();Rectangle(intwidth,intlength);~Rectangle(){}intGetWidth()const{returnitsWidth;}intGetLength()const{returnitsLength;}private:intitsWidth;intitsLength;};Rectangle::Rectangle(){itsWidth=5;itsLength=10;}Rectangle::Rectangle(intwidth,intlength){itsWidth=width;itsLength=length;}intmain(){RectangleRect1;cout<<"Rect1width:"<<Rect1.GetWidth()<<endl;cout<<"Rect1length:"<<Rect1.GetLength()<<endl;intaWidth,aLength;cout<<"Enterawidth:";cin>>aWidth;cout<<"\nEnteralength:";cin>>aLength;RectangleRect2(aWidth,aLength);cout<<"\nRect2width:"<<Rect2.GetWidth()<<endl;cout<<"Rect2length:"<<Rect2.GetLength()<<endl;return0;}程序运行输出:Rect1width:5Rect1length:10Enterawidth:20Enteralength:50Rect2width:20Rect2length:508-7定义计数器Counter类,对其重载运算符+。解:源程序:typedefunsignedshortUSHORT;#include<iostream.h>classCounter{public:Counter();Counter(USHORTinitialValue);~Counter(){}USHORTGetItsVal()const{returnitsVal;}voidSetItsVal(USHORTx){itsVal=x;}Counteroperator+(constCounter&);private:USHORTitsVal;};Counter::Counter(USHORTinitialValue):itsVal(initialValue){}Counter::Counter():itsVal(0){}CounterCounter::operator+(constCounter&rhs){returnCounter(itsVal+rhs.GetItsVal());}intmain(){CountervarOne(2),varTwo(4),varThree;varThree=varOne+varTwo;cout<<"varOne:"<<varOne.GetItsVal()<<endl;cout<<"varTwo:"<<varTwo.GetItsVal()<<endl;cout<<"varThree:"<<varThree.GetItsVal()<<endl;return0;}程序运行输出:varOne:2varTwo:4varThree:68-5定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。解:源程序:#include<iostream.h>classMammal{public:Mammal(){cout<<"Mammalconstructor...\n";}~Mammal(){cout<<"Mammaldestructor...\n";}virtualvoidSpeak()const{cout<<"Mammalspeak!\n";}};classDog:publicMammal{public:Dog(){cout<<"DogConstructor...\n";}~Dog(){cout<<"Dogdestructor...\n";}voidSpeak()const{cout<<"Woof!\n";}};intmain(){Mammal*pDog=newDog;pDog->Speak();return0;}程序运行输出:Mammalconstructor...Dogconstructor...Woof!Dogdestructor...Mammaldestructor...8-6定义一个Shape抽象类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。解:源程序:#include<iostream.h>classShape{public:Shape(){}~Shape(){}virtualfloatGetArea()=0;virtualfloatGetPerim()=0;};classCircle:publicShape{public:Circle(floatradius):itsRadius(radius){}~Circle(){}floatGetArea(){return3.14*itsRadius*itsRadius;}floatGetPerim(){return6.28*itsRadius;}private:floatitsRadius;};classRectangle:publicShape{public:Rectangle(floatlen,floatwidth):itsLength(len), itsWidth(width){};~Rectangle(){};virtualfloatGetArea(){returnitsLength*itsWidth;}floatGetPerim(){return2*itsLength+2*itsWidth;}virtualfloatGetLength(){returnitsLength;}virtualfloatGetWidth(){returnitsWidth;}private:floatitsWidth;floatitsLength;};voidmain(){Shape*sp;sp=newCircle(5);cout<<"TheareaoftheCircleis"<<sp->GetArea()<<endl;cout<<"TheperimeteroftheCircleis"<<sp->GetPerim()<<endl;deletesp;sp=newRectangle(4,6);cout<<"TheareaoftheRectangleis"<<sp->GetArea()<<endl;cout<<"TheperimeteroftheRectangleis"<<sp->GetPerim()<<endl;deletesp;}程序运行输出:TheareaoftheCircleis78.5TheperimeteroftheCircleis31.4TheareaoftheRectangleis24TheperimeteroftheRectangleis208-7对Point类重载++(自增)、--(自减)运算符解:#include<iostream.h>classPoint{public:Point&operator++();Pointoperator++(int);Point&operator--();Pointoperator--(int);Point(){_x=_y=0;}intx(){return_x;}inty(){return_y;}private:int_x,_y;};Point&Point::operator++(){_x++;_y++;return*this;}PointPoint::operator++(int){Pointtemp=*this;++*this;returntemp;}Point&Point::operator--(){_x--;_y--;return*this;}PointPoint::operator--(int){Pointtemp=*this;--*this;returntemp;}voidmain(){PointA;cout<<"A的值为:"<<A.x()<<","<<A.y()<<endl;A++;cout<<"A的值为:"<<A,x()<<","<<A,y()<<endl;++A;cout<<"A的值为:"<<A,x()<<","<<A,y()<<endl;A--;cout<<"A的值为:"<<A,x()<<","<<A,y()<<endl;--A;cout<<"A的值为:"<<A,x()<<","<<A,y()<<endl;}程序运行输出:A的值为:0,0A的值为:1,1A的值为:2,2A的值为:1,1A的值为:0,08-8定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),fn1()是虚函数,DerivedClass也有成员函数fn1()、fn2(),在主程序中定义一个DerivedClass的对象,分别用BaseClass和DerivedClass的指针来调用fn1()、fn2(),观察运行结果。解:#include<iostream.h>classBaseClass{public:virtualvoidfn1();voidfn2();};voidBaseClass::fn1(){cout<<"调用基类的虚函数fn1()"<<endl;}voidBaseClass::fn2(){cout<<"调用基类的非虚函数fn2()"<<endl;}classDerivedClass:publicBaseClass{public:voidfn1();voidfn2();};voidDerivedClass::fn1(){cout<<"调用派生类的函数fn1()"<<endl;}voidDerivedClass::fn2(){cout<<"调用派生类的函数fn2()"<<endl;}voidmain(){DerivedClassaDerivedClass;DerivedClass*pDerivedClass=&aDerivedClass;BaseClass*pBaseClass=&aDerivedClass;pBaseClass->fn1();pBaseClass->fn2();pDerivedClass->fn1();pDerivedClass->fn2();}程序运行输出:调用派生类的函数fn1()调用基类的非虚函数fn2()调用派生类的函数fn1()调用派生类的函数fn2()8-9定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass中定义虚析构函数,在主程序中将一个DerivedClass的对象地址赋给一个BaseClass的指针,观察运行过程。解:#include<iostream.h>classBaseClass{public:virtual~BaseClass(){cout<<"~BaseClass()"<<endl;}};classDerivedClass:publicBaseClass{public:~DerivedClass(){cout<<"~DerivedClass()"<<endl;}};voidmain(){BaseClass*bp=newDerivedClas
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Ro-67-7476-Standard-生命科学试剂-MCE
- 护理安全不良事件上报流程
- 2026-2030中国透气膜和薄膜行业市场发展趋势与前景展望战略分析研究报告
- 2025年工业PaaS平台性能优化
- 护理查房的基本原则
- 2026-2030中国炼油用压力容器市场供需形势分析与投资价值评估研究报告
- 2026-2030医用分子成像设备行业市场现状供需分析及重点企业投资评估规划分析研究报告
- 2026-2030热轧钢行业市场发展分析及竞争格局与投资战略研究报告
- 2026-2030中国疏浚工程行业发展趋势与投资策略分析研究报告
- 2026-2030中国餐饮供应链产业全景深度调研及投资风险预警报告
- 猪场种猪购买合同范本
- 2026年全国硕士研究生考试(英语一)真题及答案
- 中国农业大学2026年强基计划招生笔试模拟试题及答案解析二
- 公差配合与测量技术 第2版
- 小升初分班考2026年重庆市西南大学附语文模拟试卷 含答案
- 2026年量测设备行业分析报告及未来发展趋势报告
- 校长安全管理培训课件
- 2026年重庆高考数学考试卷附答案
- 双闭环比值控制系统设计与仿真
- 2026年人教版九年级道德与法治下册第一单元综合检测试卷及答案
- 机械加工质量控制标准汇编
评论
0/150
提交评论