
编写程序,定义抽象基类Shape(形状),由他派生出三个派生类
编写程序,定义抽象基类Shape(形状),由他派生出三个派生类:Circle(圆形)、Rectangle(矩形)和Square(正方形),用函数函数ShowArea()分...
编写程序,定义抽象基类Shape(形状),由他派生出三个派生类:Circle(圆形)、Rectangle(矩形)和Square(正方形),用函数函数ShowArea()分别显示各种图形的面积,最后还有显示所有图形的总面积。要求用基类指针数组,使他的每一个元素指向一个派生类对象
展开
2个回答
展开全部
不晓得你显示所有图形的总面积的函数放在哪里?
const double PI = 3.14;
class Shape
{
public:
virtual double ShowArea() = 0;
};
class Circle:public Shape
{
private:
double radius;
public:
Circle(double r):radius(r){}
double ShowArea(){return PI*radius*radius;}
};
class Rectangle:public Shape
{
private:
double length;
double width;
public:
Rectangle(double l, double w):length(l), width(w){}
double ShowArea(){return length*width;}
};
class Square:public Shape
{
private:
double length;
public:
Square(double l):length(l){}
double ShowArea(){return length*length;}
};
void main()
{
Circle circle(2);
Rectangle rect(3,4);
Square square(5);
Shape * arr[3] = {&circle, &rect, &square};
for(int i=0; i<3; i++)
{
cout << arr[i]->ShowArea() << endl;
}
}
const double PI = 3.14;
class Shape
{
public:
virtual double ShowArea() = 0;
};
class Circle:public Shape
{
private:
double radius;
public:
Circle(double r):radius(r){}
double ShowArea(){return PI*radius*radius;}
};
class Rectangle:public Shape
{
private:
double length;
double width;
public:
Rectangle(double l, double w):length(l), width(w){}
double ShowArea(){return length*width;}
};
class Square:public Shape
{
private:
double length;
public:
Square(double l):length(l){}
double ShowArea(){return length*length;}
};
void main()
{
Circle circle(2);
Rectangle rect(3,4);
Square square(5);
Shape * arr[3] = {&circle, &rect, &square};
for(int i=0; i<3; i++)
{
cout << arr[i]->ShowArea() << endl;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询