定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )#include 10
定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea()#includeclassShape{[①定义GetArea和GetPe...
定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )#includeclass Shape{ [①定义GetArea 和GetPerim函数______]};class Circle:public Shape{public: Circle(float radius):itsRadius(radius){} float GetArea(){return 3.14*itsRadius*itsRadius;} float GetPerim(){return 6.28*itsRadius;}private: float itsRadius;}; class Rectangle : public Shape{public: [②定义Rectangle构造函数_____________] [③定义 GetArea()和 GetPerim()函数_______]private: float GetLength(){return itsLength;} float GetWidth(){return itsWidth;}private: float itsWidth; float itsLength;};
展开
1个回答
展开全部
AreaCacul 类就是你的Shape抽象类,主控函数最下方 类中现在计算的是矩形和三角形两个继承类的面积。你参考改下!
class Areaclass{
public:
Areaclass(double x = 0, double y = 0): height(x), width(y) {}
protected:
double height, width;
};
class AreaCacul : public Areaclass
{
public:
AreaCacul(double x, double y):Areaclass(x,y){}
virtual double Area()=0;
};
class Box : public AreaCacul
{
public:
Box(double x,double y):AreaCacul(x,y){};
virtual double Area()
{
return height*width;
}
};
class Isosceles : public AreaCacul
{
public:
Isosceles(double x,double y):AreaCacul(x,y){};
virtual double Area()
{
return (height*width)/2;
}
};
#include <iostream>
#include "test_class.h"
using namespace std;
int main(int argc, char *argv[])
{
AreaCacul *temp[] = {new Box(10,10),new Isosceles(10,10)};
cout<<temp[0]->Area()<<endl;
cout<<temp[1]->Area()<<endl;
system("pause");
return 0;
}
class Areaclass{
public:
Areaclass(double x = 0, double y = 0): height(x), width(y) {}
protected:
double height, width;
};
class AreaCacul : public Areaclass
{
public:
AreaCacul(double x, double y):Areaclass(x,y){}
virtual double Area()=0;
};
class Box : public AreaCacul
{
public:
Box(double x,double y):AreaCacul(x,y){};
virtual double Area()
{
return height*width;
}
};
class Isosceles : public AreaCacul
{
public:
Isosceles(double x,double y):AreaCacul(x,y){};
virtual double Area()
{
return (height*width)/2;
}
};
#include <iostream>
#include "test_class.h"
using namespace std;
int main(int argc, char *argv[])
{
AreaCacul *temp[] = {new Box(10,10),new Isosceles(10,10)};
cout<<temp[0]->Area()<<endl;
cout<<temp[1]->Area()<<endl;
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询