声明一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有GetArea
//定义一个Shape基类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积。
//使用Rectangle类创建一个派生类Square。并应用相应类的对象测试。
#include<iostream.h>
class Shape
{
public:
Shape(){}
~Shape(){}
孕妇能不能吃驴肉 virtual float GetArea() const {return -1;}
};
class Circle :public Shape
{
public:
Circle(float r) :radius(r){}
~Circle(){};
float GetArea(){return 3.14f*radius*radius;}
车的英语怎么读private:石家庄到天津
float radius;牡丹芳
};
class Rectangle :public Shape
{
public:
Rectangle(float len, float width):m_len(len), m_width(width){}
~Rectangle(){}
float GetArea(){return m_len*m_width;}
float GetLength(){return m_len;}
孔子的名言
float GetWidth(){return m_width;}
private:
float m_len,m_width;
};
class Square: public Rectangle
{
public:
Square(float len);
~Square(){}
};
中国移动余额查询
Square::Square(float len):Rectangle(len,len)
{}
void main()
{如歌的岁月
Shape *sp;
sp=new Circle(5);逛灯会
cout<<"The area of the circle is"<<sp->GetArea()<<endl;
delete sp;
sp=new Rectangle(4,6);
cout<<"The area of the rectangle is"<<sp->GetArea()<<endl;
delete sp;
sp=new Square(5);
cout<<"The area of the Square is"<<sp->GetArea()<<endl;
delete sp;
}