Problem with constructor

Hi!
In my linked list I have some shapes but I need to create a second list with the first list. I wander:
1. How like look the code for that constructor?
2. I get problem with remove function because the elements in the list are const declare and the remove-parameter is not. How I can make comparison and delete the right shape from the list?
3. Is it possible to convert this linked list to doubly linked list?

Here is Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  ShapeList list; // The first list
  Vertex varr[] = { Vertex(1, 1), Vertex(10, 0), Vertex(5, 2), Vertex(5, 5) };
  list.add(Polygon(1, 4, varr, 4));
  list.add(Rectangle(4, 10, 2, 4));
  list.add(Circle(5, 5, 3));
  list.add(Point(6, 7, 1));
  list.print(); // To check
  ShapeList list2(list); // The second list !
  list2.print();
  return 0;
}


ShapeList.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include "Vertex.h"
#include "ShapeList.h"
using namespace std;

class ShapeNode {
public:
  const Shape* s;
  ShapeNode* bak;
  ShapeNode* fram;
  ShapeNode(const Shape* _s, ShapeNode* _bak, ShapeNode* _fram)
    : s(_s), bak(_bak), fram(_fram) {}
  ~ShapeNode() { delete s; }
};

int ShapeList::Rkn = 0;

ShapeList::ShapeList() {
  ny = new ShapeNode(0, 0, 0);
  ny->fram = ny->bak = ny;
}
ShapeList::ShapeList(const ShapeList& shapes) {
// ???
}
ShapeList::~ShapeList() {}
void ShapeList::add(const Shape& s) {
  Rkn++;
  const Shape* Kopian = &s;
  Kopian = Kopian->clone();
  ShapeNode *tmp = ny;
  ny = new ShapeNode(Kopian, 0, tmp);
  tmp->bak = ny;
}
void ShapeList::remove(const Vertex& v) {
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
//      if (v.Xv() == Tb.Xv() && v.Yv() == Tb.Yv()) {}
      Cnt++;
  }
}
double ShapeList::area() {
  double TotArea;
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      TotArea += p->s->objArea();
      Cnt++;
  }
  return TotArea;
}
void ShapeList::print() {
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      p->s->printShape();
      Cnt++;
  }
}


Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
#include <string>
#include "Vertex.h"
using namespace std;

class Shape {
  protected:
  public:
    Shape() {}
    ~Shape() {}
    virtual Shape* clone() const = 0;
    virtual double objArea() const = 0;
    virtual void printShape() const = 0;
    friend class ShapeNode;
};
#endif 


Shapes.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cmath>
#include "Vertex.h"
#include "Shape.h"
using namespace std;

class Polygon : public Shape {
  private:
    Vertex* Pol;
    int storlek, pX, pY;
  public:
    Polygon::Polygon(int x = 0, int y = 0, Vertex* varr = 0, int num = 0) : pX(x), pY(y), storlek(num) {
      Pol = new Vertex[storlek];
      for (int ix=0; ix<storlek; ix++)
          Pol[ix] = varr[ix];
    }
    virtual Polygon::~Polygon() { delete[] Pol; }
    virtual Shape* Polygon::clone() const { return new Polygon(pX, pY, Pol, storlek); }
    double Polygon::objArea() const {
      double s = 0.0;
      for (int ix=1; ix<storlek; ix++)
          s += (Pol[ix -1].Xv() * Pol[ix].Yv()) - (Pol[ix].Xv() * Pol[ix -1].Yv());
      return abs(s / 2);
    }
    void Polygon::printShape() const {
      cout << "POLYGON: (" << pX << "," << pY << ")   { ";
      for (int ix=0; ix<storlek; ix++)
          cout << "(" << Pol[ix].Xv() << ", " << Pol[ix].Yv() << ")  ";
      cout << "}" << endl;
    }
};
class Circle: public Shape {
  private:
    int pX, pY;
    double radius;
  public:
    Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) {}
    virtual Circle::~Circle() {}
    virtual Shape* Circle::clone() const { return new Circle(pX, pY, radius); }
    double Circle::area() { return M_PI * radius * radius; }
    void Circle::printShape() {
      cout << "CIRCLE: (" << pX << ", " << pY << "), r=" << radius << endl;
    }
};

class Rectangle : public Shape {
  private:
    int pX, pY, bredd, hojd;
  public:
    Rectangle::Rectangle(int x, int y, int width, int height) : pX(x), pY(y), bredd(width), hojd(height) {}
    virtual Rectangle::~Rectangle() {}
    virtual Shape* Rectangle::clone() const {
      return new Rectangle(pX, pY, bredd, hojd);
    }
    double Rectangle::area() { return bredd * hojd; }
    void Rectangle::printShape() {
      cout << "RECTANGLE: (" << pX << ", " << pY << "), w=" << bredd << ", h=" << hojd << endl;
    }
};

class Point : public Shape {
  private:
    int AntPoi, pX, pY, Langd;
  public:
    Point::Point(int x, int y, int size) : pX(x), pY(y), Langd(size) {}
    virtual Point::~Point() {}
    virtual Shape* Point::clone() const { return new Point(pX, pY, Langd); }
    double Point::area() { return Langd; }
    void Point::printShape() {
      cout << "POINT: (" << pX << ", " << pY << "), s=" << Langd << endl;
    }
};


Thank You in advanced.

Adalte.
Last edited on
Topic archived. No new replies allowed.