int main() Circle c(3.5,6.4,5.2); cout<<"original circle:\nx="<<c.getx(<<",y=" <<c.getYO<<",r="<<c.getRadius() <<"area="<<c.area()<<endl; c.setRadius(7.5); c.setPoint(5,5); cout长<"new circle:n"<<c;/用重载运算符“<<"输出圆对 象的信息 Point &pRef=c; cout<<"pRef:"<<pRef; return 0; 2017年4月26日星期 第12章多态性与虚函数 16 H0雪2时14分40秒 BACK NEXT
HOME2017年4月26日星期 三12时14分40秒 第12章 多态性与虚函数 16 int main( ) { Circle c(3.5,6.4,5.2); cout<<"original circle:\nx="<<c.getX()<<" , y=" <<c.getY()<<" , r="<<c.getRadius( ) <<" , area="<<c.area( )<<endl; c.setRadius(7.5); c.setPoint(5,5); cout<<"new circle:\n"<<c; //用重载运算符“<<”输出圆对 象的信息 Point &pRef=c; cout<<"pRef:"<<pRef; return 0; }
(3)声明Circle的派生类Cylinder class Cylinder:public Circle public; Cylinder (double a=0,double b=0,double r=0,double h=0):Circle(a,b,r),height(h) void setHeight(double h)height=h;} double getHeight()const {return height; double area()const; double volume()const; friend ostream&operator<<(ostream&,const Cylinder&);// 重载运算符 “<< protected: double height; 2017年4月26日星期 第12章多态性与虚函数 H0画2时14分40秒 BACK NEXT
HOME2017年4月26日星期 三12时14分40秒 第12章 多态性与虚函数 17 class Cylinder:public Circle { public: Cylinder (double a=0,double b=0,double r=0,double h=0) :Circle(a,b,r),height(h){} void setHeight(double h){height=h;} double getHeight( ) const {return height;} double area( ) const; double volume( ) const; friend ostream& operator<<(ostream&,const Cylinder&);// 重载运算符“<<” protected: double height; };
double Cylinder:area()const /计算圆柱表面积 {return 2*Circle:area()+2*3.14159*radius*height; double Cylinder:volume(const /计算圆柱体积 return Circle:areaO*height;} /重载运算符“<<” ostream &operator<<(ostream &output,const Cylinder&cy) output<<"Center=["<<cy.x<<","<<cy.y <<"],r="<<cy.radius<<",h="<<cy.height <<"narea="<<cy.area() <<",volume="<<cy.volume()<<endl; return output; 2017年4月26日星期 第12章多态性与虚函数 18 H0画2时14分40秒 BACK NEXT
HOME2017年4月26日星期 三12时14分40秒 第12章 多态性与虚函数 18 double Cylinder::area( ) const //计算圆柱表面积 { return 2*Circle::area( )+2*3.14159*radius*height;} double Cylinder::volume() const //计算圆柱体积 {return Circle::area()*height;} //重载运算符“<<” ostream &operator<<(ostream &output,const Cylinder& cy) { output<<"Center=["<<cy.x<<" , "<<cy.y <<"],r="<<cy.radius<<" ,h="<<cy.height <<"\narea="<<cy.area( ) <<" ,volume="<<cy.volume( )<<endl; return output; }
int main() { Cylinder cy1(3.5,6.4,5.2,10); cout<<"\noriginal cylinder:\n"<<cyl <<endl; cyl.setHeight(15);/设置圆柱高 cy1.setRadius(7.5); /设置圆半径 cy1.setPoint(5,5);∥设置圆心坐标值x,y cout<<"nnew cylinder:\n"<<cy1; Point &pRef=cy1; cout<<"\npRef as a Point:"<<pRef; Circle &cRef=cy1; cout<<"\ncRef as a Circle:"<<cRef; return 0; 2017年4月26日星期 第12章多态性与虚函数 19 H0雪2时14分40秒 BACK NEXT
HOME2017年4月26日星期 三12时14分40秒 第12章 多态性与虚函数 19 int main( ) { Cylinder cy1(3.5,6.4,5.2,10); cout<<"\noriginal cylinder:\n"<<cy1 <<endl; cy1.setHeight(15); //设置圆柱高 cy1.setRadius(7.5); //设置圆半径 cy1.setPoint(5,5); //设置圆心坐标值x,y cout<<"\nnew cylinder:\n"<<cy1; Point &pRef=cy1; cout<<"\npRef as a Point:"<<pRef; Circle &cRef=cy1; cout<<"\ncRef as a Circle:"<<cRef; return 0; }
12.3虚函数 12.3.1虚函数的作用 在多重继承时,从不同的基类中会继承一些重复的 数据。这就是继承的成员同名而产生的二义性 (ambiguous)问题。 )两个基类有同名成员 (2)两个基类和派生类三者都有同名成员 (3)两个基类是从同一个基类派生的 cl.A:a=3;c1.A:display0;/W同名覆盖与虚基类 017年4月26日星期 HOME 第12章多态性与虚函数 20 12时14分40秒 BACK NEXT
HOME2017年4月26日星期 三12时14分40秒 第12章 多态性与虚函数 20 在多重继承时,从不同的基类中会继承一些重复的 数据。这就是继承的成员同名而产生的二义性 (ambiguous)问题。 (1) 两个基类有同名成员 (2) 两个基类和派生类三者都有同名成员 (3) 两个基类是从同一个基类派生的 c1.A::a=3; c1.A::display();//同名覆盖与虚基类