一个C++新手菜鸟的课堂设计提问

lkllll 发布于 2017/06/05 18:11
阅读 59
收藏 2

1)实现以下继承关系:

(2)图形类Shape是一个虚基类,其中定义了名称、维度、颜色、坐标等数据成员,还拥有构造函数、析构函数、深拷贝构造函数、求图形面积的虚函数、输出图形信息的虚函数;其中,图形名称存放在一个动态字符数组中;维度为int类型;颜色为自定义Color类型,其中有表示红绿蓝三原色分量值的成员;坐标为自定义类型,其中有表示x轴y轴坐标的成员;

二维图形类中定义了求图形周长的虚函数,三维图形类中定义了求体积的虚函数;在之后派生的二维和三维具体图形类(正方形、圆、三角形、立方体、圆柱、三棱柱等)中重定义了虚函数,并根据各自的特点定义了新的数据成员和成员函数。

(3)再定义一个链表类,链表的节点中有一个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){

}                //
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(){

}

这个程序该怎么改啊

加载中
返回顶部
顶部