|
定义一个Shapecontainer类,对从SHAPE类中派生的各种图形类(circle,rectangle,trigon..)的对象进行添加,删除及面积累积计算。我想,一定要先在shapecontainer中设计一个存放各个对象的数组数据成员,并且就是shapecontainer类的,但始终不成功。请大家能给点建议吗?
下面是我写的程序,错误百出,但还望不吝赐教。
//shapecontainer.h
#include<iostream>
#include<stdlib.h>
class shape
{
protected:
double x;
double y;
public:
shape(){}
shape(double xx,double yy);
~shape(){}
void dispName(){}
double getArea(){}
bool equal(const shape&){};
};
class circle:virtual public shape
{
protected:
double radius;
double area;
public:
circle(double cx=1,double cy=1,double radi=1);
~circle(){}
void dispName();
double getArea();
bool equal(const circle&);
};
class rectangle:virtual public shape
{
protected:
double length;
double width;
double area;
public:
rectangle(double cx=1,double cy=1,double len=2,double wid=1);
~rectangle(){}
void dispName();
double getArea();
bool equal(const rectangle&);
};
class trigon:virtual public shape
{
protected:
double length;
double high;
double area;
public:
trigon(double cx=1,double cy=1,double len=2,double h=1);
~trigon(){}
void dispName();
double getArea();
bool equal(const trigon&);
};
const int max=10;
class shapecontainer:private circle,rectangle,trigon
{
int size;
public:
shapecontainer figur[max];
shapecontainer(double cx=1,double cy=1,double radi=1,double len=2,double wid=1,double h=1);
~shapecontainer(){}
shapecontainer& operator= (const shapecontainer&);
bool operator== (const shape&,const shapecontainer&);
int container_size();
shapecontainer getdata(int pos) const;
void add(const shapecontainer&);
void remove(const shapecontainer&);
void printcontainer(const shapecontainer&);
double sumarea(const shapecontainer&);
}
//shapecontainer.cpp
#include"shapecontainer.h"
shape::shape(double xx,double yy):x(xx),y(yy)
{}
circle::circle(double cx,double cy,double radi):shape(cx,cy),radius(radi)
{}
void circle::dispName()
{
std::cout << "Der Name des Shapes ist Kreis" << std::endl;
}
double circle::getArea()
{
return(area=3.14*radius*radius);
}
bool circle::equal(const circle& a)
{
return(x==a.x && y==a.y && radius==a.radius);
}
rectangle::rectangle(double cx,double cy,double len,double wid)
:shape(cx,cy),length(len),width(wid)
{}
void rectangle::dispName()
{
std::cout <<"Der Name des Shapes ist Viereck" << std::endl;
}
double rectangle::getArea()
{
return(area=length*width);
}
bool rectangle::equal(const rectangle& a)
{
return(x==a.x && y==a.y && length==a.length && width==width);
}
trigon::trigon(double cx,double cy,double len,double h)
:shape(cx,cy),length(len),high(h)
{}
void trigon::dispName()
{
std::cout <<"Der Name des Shapes ist Dreieck" << std::endl;
}
double trigon::getArea()
{
return(area=0.5*length*high);
}
bool trigon::equal(const trigon& a)
{
return(x==a.x && y==a.y && length==a.length && high==a.high);
}
shapecontainer::shapecontainer(double cx,double cy,double radi,double len,double wid,double h)
:circle(cx,cy,radi),rectangle(cx,cy,len,wid),trigon(cx,cy,len,h),size(0)
{}
shapecontainer& shapecontainer: perator= (const shapecontainer& a)
{
this->x = a.x;
this->y = a.y;
this->radius=a.radius;
this->length=a.rectangle::length;
this->length=a.trigon::length;
this->width=a.width;
this->high=a.high;
this->getArea()=a.circle::getArea();
this->getArea()=a.rectangle::getArea();
this->getArea()=a.trigon::getArea();
}
bool shapecontainer: perator== (const shapecontainer& a,shapecontainer& b)
{
return(a.circle::equal(b));
return(a.rectangle::equal(b));
return(a.trigon::equal(b));
}
int shapecontainer::container_size()
{
return(size);
}
shapecontainer shapecontainer::getdata(int pos) const
{
if(pos<0||pos>=size)
{
std::cerr<<"pos is out of range!"<<std::endl;
exit(1);
}
return figur[pos];
}
void shapecontainer::add(const shapecontainer& a)
{
if(size + 1 > max)
{
std::cerr <<"Maximum container size exceeded" <<std::endl;
exit(1);
}
figur[size]=a;
size++;
}
void shapecontainer::remove(const shapecontainer& a)
{
int i=0;
while(i<size && !(a == figur))
i++;
if(i<size)
{
while(i < size-1)
{
figur=figur[i+1];
i++;
}
size--;
}
void shapecontainer::printcontainer(const shapecontainer& a)
{
int i;
for(i=0;i<a.container_size();i++)
std::cout << a.getdata(i) << " ";
std::cout <<std::endl;
}
double shapecontainer::sumarea(const shapecontainer& a)
{
double sum=0;
int i=0;
for(i=0;i<a.container_size();i++)
sum = sum + a.circle::getArea();
sum = sum + a.rectangle::getArea();
sum = sum + a.trigon::getArea();
return sum;
}
int main(int argc,const char** argv)
{
circle circle_1(20,30,30);
circle circle_2(20,30,10);
rectangle rect_1(40,40,80,20);
/*Ö÷º¯Êý»¹Ã»À´µÃ¼°Ð´
*/
return 0;
} |
|