求大神帮忙。求求;额

笔寞 2015-06-22 11:02:11
期末考查试题:
利用C++编程完成以下任务:
(1)实现以下继承关系:
如图 http://61.187.92.238:8392/download/20150609153833001.png
(2)图形类Shape是一个虚基类,其中定义了名称、维度、颜色、坐标等数据成员,还拥有构造函数、析构函数、深拷贝构造函数、求图形面积的虚函数、输出图形信息的虚函数;其中,图形名称存放在一个动态字符数组中;维度为int类型;颜色为自定义Color类型,其中有表示红绿蓝三原色分量值的成员;坐标为自定义类型,其中有表示x轴y轴坐标的成员;
二维图形类TwoDimensionalShape中定义了求图形周长的虚函数,三维图形类ThreeDimensionalShape中定义了求体积的虚函数;在之后派生的二维和三维具体图形类(正方形、圆、三角形、立方体、圆柱、三棱柱等)中重定义了虚函数,并根据各自的特点定义了新的数据成员和成员函数。
(3)再定义一个ShapeList链表类,链表的节点中有一个Shape类型的指针;在主函数中生成各个具体的图形对象,将它们加入链表中,并通过链表来计算并显示各种图形的名称、维度、颜色、面积、周长(或体积)以及其他相关信息,要求实现运行时多态。]

我已经编了大部分了,求补充下主程序,
#include<iostream>
#include<cmath>
using namespace std;

class Color{
private:
char blue, green, red;
};
class Point{
private:
int x, y;
public:
Point(int xx=0, int yy=0){
x = xx;
y = yy;
}
Point(Point &a){
x = a.x;
y = a.y;
}
void print(){
cout << "(" << x << "," << y << ")" << endl;

}

};
class Shape :virtual public Color,virtual public Point {

private:
char *name;//名称
int wd;//维度
Color yanse;//颜色
Point zb;
public:
Shape(const char*z,int WD,int x,int y):Color(),Point(x,y){

name = new char[strlen(z) + 1];
strcpy(name,z);
wd = WD;
}
~Shape(){

delete[] name;
}
Shape(Shape &z) :Color(), zb(z.zb){

name = new char[strlen(z.name) + 1];
strcpy(name, z.name);
wd = z.wd;
}
virtual double area(){

return 0.0; //求面积

}
virtual double information(){

cout << "图形:"; //显示图形的信息

}



};

class TwoDimensionalShape :virtual public Shape {
public:
TwoDimensionalShape(const char *s, int x, int y, int wd = 2) :Shape(s, wd, x, y){

} //详情请见P264
virtual double zhouchang(){

return 0.0; //周长
}


};
class Square :public TwoDimensionalShape { //正方形
private:
double bianchang;
public:
Square(double b, const char *s, int x, int y, int wd = 2) :TwoDimensionalShape(s, x, y,wd),Shape(s,x,y,wd){

bianchang = b; //对于继承的基类,也要进行构造

}
double zhouchang(){
return bianchang * 4;

}
double area(){

return bianchang*bianchang;
}
double information(){

cout << "正方形" << endl;
}

};
class Circular :public TwoDimensionalShape{ //圆形

private:
double radius;
public:
Circular(double r, const char *s, int x, int y, int wd = 2) :TwoDimensionalShape(s, x, y, wd), Shape(s, x, y, wd){
radius = r;

}
double zhouchang(){

return 3.14*radius * 2;
}
double area(){

return 3.14*radius*radius;
}
double information(){

cout << "圆形" << endl;
}
};
class Rectangle :public TwoDimensionalShape{
private:
double width, length; //长方形
public:
Rectangle(double w, double l, const char *s, int x, int y, int wd = 2) :TwoDimensionalShape(s,x,y,wd),Shape(s,x,y,wd){
width = w;
length = l;

}
double zhouchang(){

return (width + length) * 2;
}
double area(){

return width*length;
}
double information(){

cout << "长方形:" << endl;
}
};
class Triangle :public TwoDimensionalShape{

private: //三角形
double sideA, sideB, sideC;
public:
Triangle(double sa, double sb,double sc, const char *s, int x, int y, int wd = 2) :TwoDimensionalShape(s,x,y,wd),Shape(s,x,y,wd){

sideA = sa;
sideB = sb;
sideC = sc;

}
double zhouchang(){

return (sideA + sideB + sideC);
}
double area(){
double d;
d = sideA + sideB + sideC;

return sqrt(d*(d - sideA)*(d - sideB)*(d - sideC));
}
double information(){
cout << "三角形" << endl;

}

};
class ThreeDimensionalShape :virtual public Shape
{
public:
virtual double tiji(){
return 0;
}
ThreeDimensionalShape(const char *s, int x, int y, int wd = 3) :Shape(s, wd, x, y)
{
}

};
class Cube :public ThreeDimensionalShape, public Square{ //跟正方体继承的立方体
private:
double higth;
public:
Cube(double h, double b, const char *s, int x, int y, int wd = 3) :ThreeDimensionalShape(s, x, y, wd), Square(b,s,x,y,wd), Shape(s, x, y, wd){
higth = h;

}
double Volume(){
return Square::area()*higth;

}
double information(){
cout << "立方体" << endl;

}



};

class Cuboid :ThreeDimensionalShape, public Rectangle{// 跟长方形继承的长方体
private:
double higth;
public:
Cuboid(double h, double l, double w, const char *s, int x, int y, int wd = 3) :ThreeDimensionalShape(s, x, y, wd), Rectangle(l, w, s, x, y, wd), Shape(s, x, y, wd){

higth = h;
}
double Volume(){

return Rectangle::area()*higth;
}
double information(){

cout << "长方体" << endl;
}


};
class Cylindrical :ThreeDimensionalShape, public Circular{//跟圆形继承的圆柱

private:
double higth;
public:
Cylindrical(double h, double r, const char *s, int x, int y, int wd = 3) :ThreeDimensionalShape(s, x, y, wd), Circular(r, s, x, y, wd), Shape(s, x, y, wd){
higth = h;

}
double Volume(){
return Circular::area()*higth;

}
double information(){

cout << "圆柱" << endl;
}
};
class Sanlingzhu :ThreeDimensionalShape, public Triangle{//跟三角形继承的三菱拄
private:
double higth;
public:
Sanlingzhu(double h, double sa, double sb, double sc, const char *s, int x, int y, int wd = 3) :ThreeDimensionalShape(s, x, y, wd),Triangle(sa, sb, sc, s, x, y, wd), Shape(s, x, y, wd){

higth = h;

}
double Volume(){
return Triangle::area()*higth;

}
double information(){

cout << "三菱拄" << endl;
}
};
struct Node
{
Shape*data;
Node*next;
};
class ShapeList{

private:
Node *head;
public:
ShapeList()
{
Node *s = new Node;
head = s;
head->data = NULL;
head->next = NULL;
}
void initList()
{
Node*temp = head->next;
while (temp != NULL)
{
Node*temp2 = temp;
temp = temp->next;
delete temp2;
}
}
void add(Node*temp2)
{
Node*temp = head;
for (; temp->next != NULL; temp = temp->next)
{
}
temp->next = temp2;
}
void delete_(int wz)
{
Node*temp = head;
int i;
for (i = 1; temp->next != NULL; temp = temp->next, i++)
{
if (i == wz)
{
Node*temp2 = temp->next;
temp->next = temp->next->next;
delete temp2;
return;
}
}
}
void print(Node*temp)
{
temp->data->print();
}
void printall()
{
Node*temp = head;
int i = 1;
for (; temp->next != NULL; temp = temp->next)
{
cout << i++ << "、" << endl;
temp->next->data->print();
}
}
void found(int x)
{
int i;
Node *temp = NULL;
for (i = 1, temp = head; temp->next != NULL; temp = temp->next, i++)
{
if (i == x)
temp->next->data->print();
}
}
~ShapeList()
{
Node*temp = head->next;
while (temp != NULL)
{
Node*temp2 = temp;
temp = temp->next;
delete temp2;
}
delete head;
}
};
int main(){

}
...全文
213 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_37646511 2017-06-14
  • 打赏
  • 举报
回复
在吗 我也想求这个题的答案

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧