泪洒腾格里吧 关注:2贴子:86
  • 10回复贴,共1

【C++】链表与类的使用

只看楼主收藏回复



通过百度相册上传1楼2013-11-17 14:52回复
    要求如下:
    一位小学教师Ken希望完成这样的任务:针对于小学生正在学习四边形(quadrangle)的特性,编写一个小软件,能够随机在屏幕上显示矩形(rectangle)、正方形(square)、平行四边形(parallelogram)、梯形(trapezoid)和菱形(diamond)五种形体之一,同时显示该形体的特性和关键数据(随机产生),学生复习形体的特性,然后根据给出的关键数据计算形体的面积,软件判断其结果的正确性。在学习过程中,软件记录产生的每一个形体,在学生选择不再继续后,将其学习的过程重放一遍,用以重温,加深印象。
    根据Ken老师的要求,需要至少编写六个类:
    1) Rect //注意:类名不要使用Rectangle
    2) Square
    3) Parallelogram
    4) Trapezoid
    5) Diamond
    6) List
    其中,前五个类用于描述五种形体。五种形体不用顶点坐标的表示形式,而只是简单地用它们的特征值表示。例如:矩形、平行四边形用长和高表示;正方形用边长表示;梯形用两条平行边长和高表示;菱形用两条对象线长表示。要求为这五种形体编写相应的类,类中包括至少如下数据成员和成员函数:
    1) 特征值,例如长和宽等;
    2) string name; 用于存储形体的类别。类别的名字就是类的名字。例如:Rect类的类别名就是“Rectangle”;
    3) double area(); 用于求形体的面积;
    4) void draw(); 用于绘制形体。不要求用图形的方式画图形,只需要简单地打印出形体的类别和特征值;
    5) 构造(包括拷贝构造)函数和析构函数。构造函数要带有相应的参数以能够用指定特征值初始化形体对象。
    此外,List类用于存储各种形体。形体的产生可以通过随机的方式完成。不过,List类的push_back()成员的代码需要自行完成。附件中给出了List类的大部分代码。
    重温过程实际上就是遍历List类对象的过程。这个过程可能有些复杂,附件中也给出了遍历的示例代码。


    2楼2013-11-17 14:52
    回复
      原理如下:
      数据封装将一组数据和这组数据有关的操作集合封装在一起,形成一个能动的实体,称为对象。用户不必知道对象行为的实现细节,只需根据对象提供的外部特性接口访问对象。
      面向对象技术试图通过建立一个合适的数据类型,将描述对象的属性(数据)和行为(函数)结合在一起,形成一个新的抽象数据类型,称为类类型(class)。类是C++的封装机制,其中包括了数据成员和成员函数。
      在C++类中,能确保数据只能由类中的成员函数进行访问和处理。在任何时候,都可以自由地改变数据成员的组织形式,只需改变成员函数的实现细节。由于这些成员函数的接口不改变,系统其他部分的程序(及使用者)就不会由于改动而受到影响。
      类的概念将数据和与这个数据有关的操作集合封装在一起,建立了一个定义良好的接口,人们只关心其使用,不关心其实现细节。这反应了抽象数据类型的思想。


      3楼2013-11-17 14:53
      回复
        关键技术分析:
        List类用链表的方式存储对象。这产生了一个问题:链表的节点中包含的数据域的类型是一样,而我们需要存储五种不同的形体。显然,节点中的数据域的类型不能是五种形体类中的任何一个,只能用一种相对统一的方式来完成。这里给出一个简单的思路:编写一个与五种形体都很相似的类Quadrangle类,这个类至少包含了与其它五种形体类一样的数据成员:name。这样,节点的数据域的类型就是Quandrangle *。但在遍历List类的对象时,通过name成员获得形体对象的标识,然后再通过类型强制转换获得正确的对象指针,最后在调用对象的相应成员函数完成指定的操作。


        4楼2013-11-17 14:54
        回复
          = =不能上传附件简直无情……
          rect头文件
          代码贴到这里:
          #ifndef RECT_H_
          #define RECT_H_
          using namespace std;
          class Quadrangle
          {
          public:
          string name; //形体的标识
          };
          typedef Quadrangle * QUADPTR;
          class Rect
          {
          public:
          string name; //形体的标识
          Rect (int w, int h, string nm);
          ~Rect ();
          void draw() const;
          double area() const;
          string what() const;
          int& Width(); //获取矩形的宽。如果是梯形,还需要一个成员int& Width2()来获取另一条平行边的长度
          int& Height();//获取矩形的高
          private:
          int width, height; //高和宽
          };
          class Square
          {
          public:
          string name; //形体的标识
          Square (int w, string nm);
          ~Square ();
          void draw() const;
          double area() const;
          string what() const;
          int& Width(); //获取矩形的宽。如果是梯形,还需要一个成员int& Width2()来获取另一条平行边的长
          private:
          int width; //高和宽
          };
          class Parallelogram
          {
          public:
          string name; //形体的标识
          Parallelogram (int, int h, string nm);
          ~Parallelogram ();
          void draw() const;
          double area() const;
          string what() const;
          int& Width(); //获取矩形的宽。如果是梯形,还需要一个成员int& Width2()来获取另一条平行边的长度
          int& Height();//获取矩形的高
          private:
          int width, height; //高和宽
          };
          class Trapezoid
          {
          public:
          string name; //形体的标识
          Trapezoid (int w1,int w2, int h, string nm);
          ~Trapezoid ();
          void draw() const;
          double area() const;
          string what() const;
          int& Width1();
          int& Width2();//获取矩形的宽。如果是梯形,还需要一个成员int& Width2()来获取另一条平行边的长度
          int& Height();//获取矩形的高
          private:
          int width1,width2, height; //高和宽
          };
          class Diamond
          {
          public:
          string name; //形体的标识
          Diamond (int w, int height,string nm);
          ~Diamond ();
          void draw() const;
          double area() const;
          string what() const;
          int& Width(); //获取矩形的宽。如果是梯形,还需要一个成员int& Width2()来获取另一条平行边的长度
          int& Height();//获取矩形的高
          private:
          int width, height; //高和宽
          };
          Rect::Rect (int w = 5, int h = 7, string nm = "Rectangle")
          {
          width = w;
          height = h;
          name = nm;
          }
          Rect::~Rect()
          {}
          void Rect::draw()const
          {
          cout << what() <<": width = " << width << ", height = " << height << endl;
          }
          double Rect::area()const
          {
          return (width * height);
          }
          string Rect::what()const
          {
          return name;
          }
          int& Rect::Width()
          {
          return width;
          }
          int& Rect::Height()
          {
          return height;
          }
          Square::Square(int w = 5,string nm = "Square")
          {
          width = w;
          name = nm;
          }
          Square::~Square(){}
          void Square::draw()const
          {
          cout << what() <<":" << "width = " << width << endl;
          }
          double Square::area()const
          {
          return (width * width);
          }
          string Square::what()const
          {
          return name;
          }
          int& Square::Width()
          {
          return width;
          }
          Parallelogram::Parallelogram (int w = 5, int h = 7, string nm = "Rectangle")
          {
          width = w;
          height = h;
          name = nm;
          }
          Parallelogram::~Parallelogram()
          {}
          void Parallelogram::draw()const
          {
          cout << what() <<": width = " << width << ", height = " << height << endl;
          }
          double Parallelogram::area()const
          {
          return (width * height);
          }
          string Parallelogram::what()const
          {
          return name;
          }
          int& Parallelogram::Width()
          {
          return width;
          }
          int& Parallelogram::Height()
          {
          return height;
          }
          Trapezoid::Trapezoid(int w1 = 5,int w2 = 6,int h = 7,string nm = "Trapezoid")
          {
          width1 = w1;
          width2 = w2;
          height = h;
          name = nm;
          }
          Trapezoid::~Trapezoid(){}
          void Trapezoid::draw()const
          {
          cout << what() << ": width1 = " << width1 << ",width2 = " << width2 << ",height = " << height << endl;
          }
          string Trapezoid::what()const
          {
          return name;
          }
          double Trapezoid::area()const
          {
          return ((width1 + width2) * height / 2);
          }
          int& Trapezoid::Width1()
          {
          return width1;
          }
          int& Trapezoid::Width2()
          {
          return width2;
          }
          int& Trapezoid::Height()
          {
          return height;
          }
          Diamond::Diamond (int w = 5, int h = 7, string nm = "Diamond")
          {
          width = w;
          height = h;
          name = nm;
          }
          Diamond::~Diamond()
          {}
          void Diamond::draw()const
          {
          cout << what() <<": width = " << width << ", height = " << height << endl;
          }
          double Diamond::area()const
          {
          return (width * height / 2);
          }
          string Diamond::what()const
          {
          return name;
          }
          int& Diamond::Width()
          {
          return width;
          }
          int& Diamond::Height()
          {
          return height;
          }


          5楼2013-11-17 14:58
          回复


            IP属地:浙江来自Android客户端6楼2013-12-13 18:15
            回复
              好可怕


              来自手机贴吧7楼2014-01-04 10:53
              收起回复
                哦哦,main()函数怎么写,谢谢楼主了!!!


                IP属地:黑龙江8楼2014-10-22 22:17
                收起回复