- 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第六章_多态性和虚函数
虚基类和抽象基类的应用(续) //声明Cylinder类 class Cylinder:public Circle {public: Cylinder (float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; virtual float area() const; virtual float volume() const; // 对纯虚函数进行再定义 virtual void shapeName() const {coutCylinder:;} friend ostream operator(ostream,const Cylinder); protected: float height; }; * 虚基类和抽象基类的应用(续) Cylinder::Cylinder(float a,float b,float r,float h) :Circle(a,b,r),height(h){} void Cylinder::setHeight(float h){height=h;} float Cylinder::getHeight() const {return height;} float Cylinder::area() const { return 2*Circle::area()+2*3.14159*radius*height;} float Cylinder::volume() const {return Circle::area()*height;} ostream operator(ostream output,const Cylinder cy) {output[cy.x,cy.y], r=cy.radius, h=cy.height; return output; } * 虚基类和抽象基类的应用(续) int main() {Point point(3.2,4.5); //建立Point类对象point Circle circle(2.4,12,5.6); //建立Circle类对象circle Cylinder cylinder(3.5,6.4,5.2,10.5); //建立Cylinder类对象cylinder point.shapeName(); //静态关联 coutpointendl; circle.shapeName(); //静态关联 coutcircleendl; cylinder.shapeName(); //静态关联 coutcylinderendlendl; Shape *pt; //定义基类指针 * 虚基类和抽象基类的应用(续) pt=point; //指针指向Point类对象 pt-shapeName(); //动态关联 coutx=point.getX(),y=point.getY()\narea=pt-area() \nvolume=pt-volume()\n\n; pt=circle; //指针指向Circle类对象 pt-shapeName(); //动态关联 coutx=circle.getX(),y=circle.getY()\narea=pt-area() \nvolume=pt-volume()\n\n; pt=cylinder; //指针指向Cylinder类对象 pt-shapeName(); //动态关联 coutx=cylinder.getX(),y=cylinder.getY()\narea=pt-area() \nvolume=pt-volume() \n\n; return 0;} * 虚基类和抽象基类的应用(续) Point:[3.2,4.5](point的数据:点的坐标) Circle:[2.4,1.2], r=5.6 (circle的数据:圆心和半径) Cylinder:[3.5,6.4], r=5.5, h=10.5 (cylinder的数据) Point:x=3.2,y=4.5 (输出Point类对象point的数据:点的坐标) area=0 (点的面积) volume=0 (点的体积) Circl
您可能关注的文档
最近下载
- 深发展银行黄金td业务开户流程 深发展银行贵金属递延业务开户流程_.ppt VIP
- 中国国家标准 GB/T 40344.3-2024真空技术 真空泵性能测量标准方法 第3部分:机械增压泵的特定参数.pdf
- 固定资产盘点流程和方案.docx VIP
- 第6章细菌的感染与免疫.ppt VIP
- 【医脉通•指南】左卡尼汀在男性不育中临床应用专家共识(2014版).pdf VIP
- 高考英语语法新视角(中级英语测试指导).doc VIP
- 2020年必威体育精装版版下肢静脉曲张临床路径.docx VIP
- 教育行为模式识别-洞察及研究.docx VIP
- 第12课《鱼纹话吉祥》教学课件-2024-2025学年岭南美版(2024)初中美术七年级上册.pptx VIP
- 特种设备安全总监职责.doc VIP
文档评论(0)