大神帮我看看这c++代码怎么写,帮忙,谢谢! 15
设计一个类层次,定义一个抽象类--形状(类名:Shape),其中包括有求形状的面积的抽象方法(方法名:calArea)。继承该抽象类定义三角形(类名:Triangle)、...
设计一个类层次,定义一个抽象类--形状(类名:Shape),其中包括有求形状的面积的抽象方法(方法名:calArea)。
继承该抽象类定义三角形(类名:Triangle)、矩形(类名:Rectangle)、圆(Circle)。分别创建一个三角形、矩形、圆存入一个数组中,将数组中各类图形的面积输出。 展开
继承该抽象类定义三角形(类名:Triangle)、矩形(类名:Rectangle)、圆(Circle)。分别创建一个三角形、矩形、圆存入一个数组中,将数组中各类图形的面积输出。 展开
展开全部
#include <iostream>
#include <math.h>
using namespace std;
const double PAI=3.14;
//位置点类
class Position{
int x,y;
public:
Position(){}
Position(int xx,int yy){x=xx;y=yy;}
Position(Position &p){ x=p.x; y=p.y; }
int getX(){return x;}
int getY(){return y;}
void setXY(int xx,int yy){ x=xx; y=yy; }
};
//接口类
class IElements
{
public:
//在此定义哪些属性可读写
virtual double getArea()=0;
virtual Position getPos() =0;
virtual void setPos(int xx,int yy)=0;
virtual void setRecNo(int n )=0;
virtual int getRecNo()=0;
//定义提供哪些方法
virtual void outPutR()=0;
virtual double calArea()=0;
};
//图形元素基类
class Shape:public IElements
{
protected:
double area;
Position pos;
int recNo;
static int recNum;
public:
Shape(){ area=0; recNum++; recNo=recNum; }
Shape(Position p){pos=p;area=0; recNum++; recNo=recNum; }
double getArea(){return area;}
Position getPos() {return pos;}
void setPos(int xx,int yy) { pos.setXY(xx,yy); }
void setRecNo(int n ){recNo=n;}
int getRecNo(){return recNo;}
static int getNum(){return recNum;}
static void setNum(int n){recNum=n;}
void outPutR()
{
cout<<"编号:"<<recNo<<"\t";
cout<<"("<<pos.getX()<<","<<pos.getY()<<")\t面积:"<<area<<endl;
}
double calArea(){return 0;}
};
int Shape :: recNum = 0;
//三角形类
class Triangle : public Shape
{
private:
int down, high;
public:
Triangle(){}
Triangle(int d, int h, Position p);
void setDown(int d);
void setHigh(int h);
double calArea();
};
Triangle::Triangle(int d, int h, Position p)
{
down=d;
high=h;
pos=p;
}
void Triangle::setDown(int d)
{
down=d;
}
void Triangle::setHigh(int h)
{
high=h;
}
double Triangle::calArea()
{
Shape :: calArea();
return area = 0.5*high*down;
}
//矩形类
class Rectangle : public Shape
{
private:
int len,wide;
public:
Rectangle(){}
Rectangle(int l,int w,Position p);
void setLen(int l);
void setWide(int w);
double calArea();
};
Rectangle::Rectangle(int l,int w,Position p):Shape(p),len(l),wide(w)
{
}
/*也可以写成
Rectangle::Rectangle(int l,int w,Position p)
{
len=l;
wide=w;
pos=p;
}*/
void Rectangle::setLen(int l)
{
len=l;
}
void Rectangle::setWide(int w)
{
wide=w;
}
double Rectangle::calArea()
{
Shape :: calArea();
return area = len*wide;
}
//圆形类
class Circular : public Shape
{
private:
int radius;
public:
Circular(){}
Circular(int r, Position p){radius=r; pos=p;}
void setRadius(int r){radius=r;}
int getRadius(){return radius;}
double calArea();
};
double Circular :: calArea()
{
Shape :: calArea();
return area = PAI*radius*radius;
}
void main ( )
{
int i;
IElements * elements[4];
Position p(1,1);
Shape s(p);
elements[0]=&s;
Triangle t(1,1,p);
elements[1]=&t;
Rectangle r(2,2,p);
elements[2]=&r;
Circular c(3, p);
elements[3]=&c;
for(i=0;i<4;i++)
{
elements[i]->calArea();
elements[i]->outPutR();
}
}
#include <math.h>
using namespace std;
const double PAI=3.14;
//位置点类
class Position{
int x,y;
public:
Position(){}
Position(int xx,int yy){x=xx;y=yy;}
Position(Position &p){ x=p.x; y=p.y; }
int getX(){return x;}
int getY(){return y;}
void setXY(int xx,int yy){ x=xx; y=yy; }
};
//接口类
class IElements
{
public:
//在此定义哪些属性可读写
virtual double getArea()=0;
virtual Position getPos() =0;
virtual void setPos(int xx,int yy)=0;
virtual void setRecNo(int n )=0;
virtual int getRecNo()=0;
//定义提供哪些方法
virtual void outPutR()=0;
virtual double calArea()=0;
};
//图形元素基类
class Shape:public IElements
{
protected:
double area;
Position pos;
int recNo;
static int recNum;
public:
Shape(){ area=0; recNum++; recNo=recNum; }
Shape(Position p){pos=p;area=0; recNum++; recNo=recNum; }
double getArea(){return area;}
Position getPos() {return pos;}
void setPos(int xx,int yy) { pos.setXY(xx,yy); }
void setRecNo(int n ){recNo=n;}
int getRecNo(){return recNo;}
static int getNum(){return recNum;}
static void setNum(int n){recNum=n;}
void outPutR()
{
cout<<"编号:"<<recNo<<"\t";
cout<<"("<<pos.getX()<<","<<pos.getY()<<")\t面积:"<<area<<endl;
}
double calArea(){return 0;}
};
int Shape :: recNum = 0;
//三角形类
class Triangle : public Shape
{
private:
int down, high;
public:
Triangle(){}
Triangle(int d, int h, Position p);
void setDown(int d);
void setHigh(int h);
double calArea();
};
Triangle::Triangle(int d, int h, Position p)
{
down=d;
high=h;
pos=p;
}
void Triangle::setDown(int d)
{
down=d;
}
void Triangle::setHigh(int h)
{
high=h;
}
double Triangle::calArea()
{
Shape :: calArea();
return area = 0.5*high*down;
}
//矩形类
class Rectangle : public Shape
{
private:
int len,wide;
public:
Rectangle(){}
Rectangle(int l,int w,Position p);
void setLen(int l);
void setWide(int w);
double calArea();
};
Rectangle::Rectangle(int l,int w,Position p):Shape(p),len(l),wide(w)
{
}
/*也可以写成
Rectangle::Rectangle(int l,int w,Position p)
{
len=l;
wide=w;
pos=p;
}*/
void Rectangle::setLen(int l)
{
len=l;
}
void Rectangle::setWide(int w)
{
wide=w;
}
double Rectangle::calArea()
{
Shape :: calArea();
return area = len*wide;
}
//圆形类
class Circular : public Shape
{
private:
int radius;
public:
Circular(){}
Circular(int r, Position p){radius=r; pos=p;}
void setRadius(int r){radius=r;}
int getRadius(){return radius;}
double calArea();
};
double Circular :: calArea()
{
Shape :: calArea();
return area = PAI*radius*radius;
}
void main ( )
{
int i;
IElements * elements[4];
Position p(1,1);
Shape s(p);
elements[0]=&s;
Triangle t(1,1,p);
elements[1]=&t;
Rectangle r(2,2,p);
elements[2]=&r;
Circular c(3, p);
elements[3]=&c;
for(i=0;i<4;i++)
{
elements[i]->calArea();
elements[i]->outPutR();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询