C++程序问题
将Shape基类定义为抽象类,其成员函数GetArea()为虚函数,由Shape类派生出Circle类(圆)和Rectangle类(矩形),并由Rectangle类派生出...
将Shape基类定义为抽象类,其成员函数GetArea()为虚函数,由Shape类派生出Circle类(圆)和Rectangle类(矩形),并由Rectangle类派生出Square类(正方形),它们都利用GetArea()函数计算图形面积。根据下列调用语句,分别写出各个类的定义,并且成功地调试运行程序
下面是已经给出的主函数:
Shape *s[5];
s[0]=new Circle(2);
s[1]=new Circle(3);
s[2]=new Rectangle(3, 4);
s[3]=new Rectangle(4, 5);
s[4]=new Square(5);
for(int i=0; i<5; i++) cout<<s[i]->GetArea()<<endl;
整个程序怎么写啊 展开
下面是已经给出的主函数:
Shape *s[5];
s[0]=new Circle(2);
s[1]=new Circle(3);
s[2]=new Rectangle(3, 4);
s[3]=new Rectangle(4, 5);
s[4]=new Square(5);
for(int i=0; i<5; i++) cout<<s[i]->GetArea()<<endl;
整个程序怎么写啊 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
2014-11-20
展开全部
#include<iostream>
#define PI 3.14
using namespace std;
class Shape
{
public:
virtual double GetArea()
{
return 0.0;
}
};
class Circle:public Shape
{
protected:
double r;
public:
Circle(double r)
{
this->r=r;
}
virtual double GetArea()
{
return PI*r*r;
}
};
class Rectangle:public Shape
{
protected:
double x,y;
public:
Rectangle(double x,double y)
{
this->x=x;
this->y=y;
}
virtual double GetArea()
{
return x*y;
}
};
class Square:public Rectangle
{
public:
Square(double x):Rectangle(x,x)
{
}
virtual double GetArea()
{
return x*y;
}
};
int main()
{
Shape *s[5];
s[0]=new Circle(2);
s[1]=new Circle(3);
s[2]=new Rectangle(3, 4);
s[3]=new Rectangle(4, 5);
s[4]=new Square(5);
for(int i=0; i<5; i++) cout<<s[i]->GetArea()<<endl;
}
#define PI 3.14
using namespace std;
class Shape
{
public:
virtual double GetArea()
{
return 0.0;
}
};
class Circle:public Shape
{
protected:
double r;
public:
Circle(double r)
{
this->r=r;
}
virtual double GetArea()
{
return PI*r*r;
}
};
class Rectangle:public Shape
{
protected:
double x,y;
public:
Rectangle(double x,double y)
{
this->x=x;
this->y=y;
}
virtual double GetArea()
{
return x*y;
}
};
class Square:public Rectangle
{
public:
Square(double x):Rectangle(x,x)
{
}
virtual double GetArea()
{
return x*y;
}
};
int main()
{
Shape *s[5];
s[0]=new Circle(2);
s[1]=new Circle(3);
s[2]=new Rectangle(3, 4);
s[3]=new Rectangle(4, 5);
s[4]=new Square(5);
for(int i=0; i<5; i++) cout<<s[i]->GetArea()<<endl;
}
追问
因为他的比较好理解,所以我采用它的了
追答
我提交的是候没有注意到已经有人提交了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
class Shape
{
public:
//Shape();
virtual double GetArea()=0;
//virtual ~Shape();
};
class Circle: public Shape
{
private: double _r;
public:Circle(double a)
{
_r=a;
}
virtual double GetArea()
{
return 3.14*_r*_r;
}
// virtual ~Circle();
};
class Rectangle:public Shape
{
private: double _width;
double _height;
public:Rectangle(double width,double height)
{
_width=width;
_height=height;
}
Rectangle()
{
_width=0;
_height=0;
}
virtual double GetArea()
{
return _width*_height;
}
// virtual ~Rectangle();
};
class Square: public Rectangle
{
private: double _width;
public:Square(double width)
{
_width=width;
}
virtual double GetArea()
{
return _width*_width;
}
// virtual ~Square();
};
void main()
{
Shape *s[5];
s[0]=new Circle(2);
s[1]=new Circle(3);
s[2]=new Rectangle(3, 4);
s[3]=new Rectangle(4, 5);
s[4]=new Square(5);
for(int i=0; i<5; i++) cout<<s[i]->GetArea()<<endl;
}
using namespace std;
class Shape
{
public:
//Shape();
virtual double GetArea()=0;
//virtual ~Shape();
};
class Circle: public Shape
{
private: double _r;
public:Circle(double a)
{
_r=a;
}
virtual double GetArea()
{
return 3.14*_r*_r;
}
// virtual ~Circle();
};
class Rectangle:public Shape
{
private: double _width;
double _height;
public:Rectangle(double width,double height)
{
_width=width;
_height=height;
}
Rectangle()
{
_width=0;
_height=0;
}
virtual double GetArea()
{
return _width*_height;
}
// virtual ~Rectangle();
};
class Square: public Rectangle
{
private: double _width;
public:Square(double width)
{
_width=width;
}
virtual double GetArea()
{
return _width*_width;
}
// virtual ~Square();
};
void main()
{
Shape *s[5];
s[0]=new Circle(2);
s[1]=new Circle(3);
s[2]=new Rectangle(3, 4);
s[3]=new Rectangle(4, 5);
s[4]=new Square(5);
for(int i=0; i<5; i++) cout<<s[i]->GetArea()<<endl;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询