第六章_多态性和虚函数.ppt

  1. 1、本文档共77页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话: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

文档评论(0)

yaocen + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档