Problem with list and node

Hi!
Please, it's very important for me to solve this problem. I'm very grateful for all hints, suggestions and h e l p with Your code I can get. Thank You in advance.

The problem:
1. The object is destroyed after that I put data in the object.
2. My doubly linked list ShapeList is not working.

The following must be anchanged (sorry):
1. Write an abstract class (e.g. Shape) with following subclases: Polygon and Circle. This methodes (functions) must be:
-An object Vertex with the definition here (It's ok to add functions but not change or delete the existing functions here)
-Polygon(int x, int y, Vertex *varr, int num);
-Circle(int x, int y, int radie);
-area()
-print()
2. Write a doubly linked list (e.g. ShapeList) of the objects above and must have the following methodes (functions):
-ShapeList()
-ShapeList(const ShapeList &shapes)
-add(const Shape& s) // a COPY of S into the list
-area() // The TOTAL area of ALL objects
-print()

All other things is open for changes.

The super class Shape:

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
//--- Shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
#include "Vertex.h"
using namespace std;

class Shape {
  protected:
    Vertex *PosShape;
    int AntalShape;
  public:
    Shape();
    virtual ~Shape();
    virtual double area() = 0;
    virtual void printShape();
};
#endif

//--- Shape.cpp

#include <iostream>
#include <cmath>
#include "Vertex.h"
#include "Shape.h"
using namespace std;

Shape::Shape() : AntalShape(0) {
  cout << "\tShape_Defaultconstructor" << endl;
  AntalShape = 0;
  PosShape = new Vertex[6];  // some objects for polygon and circle's position
  cout << "\tShape_Defaultconstructor_Finish" << endl;
  printShape();
}
Shape::~Shape() {
  cout << "\tShape_Destructor" << endl;
}
void Shape::printShape() {}


The subclasses Polygon and Circle (Vertex needs too):

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

//--- Vertex.h

#ifndef VERTEX_H
#define VERTEX_H

class Vertex {
  private:
    int x, y;
  public:
    Vertex();
    Vertex(int px, int py);
    ~Vertex();
    int Xvrd();
    int Yvrd();
    friend class Polygon;
};
#endif

//--- Vertex.cpp

#include <iostream>
#include "vertex.h"
using namespace std;

Vertex::Vertex() : x(0), y(0) { cout << "\t     Vertex_Defaultconstructor" << endl; }
Vertex::Vertex(int px, int py) : x(px), y(py) { cout << "\t     Vertex_Constructor" << endl; }
Vertex::~Vertex() { cout << "\t     Vertex_Destructor" << endl; }
int Vertex::Xvrd() { return x; }
int Vertex::Yvrd() { return y; }

//--- Polygon.h

#ifndef POLYGON_H
#define POLYGON_H
#include <iostream>
#include "Vertex.h"
#include "Shape.h"
using namespace std;

class Polygon : public Shape {
  private:
    Vertex *Pol;
    int pX, pY, storlek;
  public:
    Polygon();
    Polygon(int x, int y, Vertex *varr, int num);
    ~Polygon();
    double area();
    void printShape();
};
#endif

//--- Polygon.cpp

#include <iostream>
#include <cmath>
#include "polygon.h"
using namespace std;

Polygon::Polygon(int x, int y, Vertex *varr, int num) : pX(x), pY(y), storlek(num) {
  cout << "\t   Polygon_Constructor" << endl;
  if (storlek > 0) {
     Shape::PosShape[Shape::AntalShape++] = Vertex(pX, pY);
     Pol = new Vertex[storlek];
     for (int ix=0; ix<storlek; ix++)
         Pol[ix] = varr[ix];
   } else { Pol = 0; }
}
Polygon::~Polygon() {
  cout << "\t   Polygon_Destructor" << endl;
//  delete[] Pol;
}
double Polygon::area() {
  double s = 0.0;
  double r = 0.0;
  for (int ix=1; ix<storlek; ix++) {
      s += (Pol[ix -1].x * Pol[ix].y) - (Pol[ix].x * Pol[ix -1].y);
  }
  r = s / 2;
  return abs(r);
}
void Polygon::printShape() {
  cout << "POLYGON: (" << Shape::PosShape[Shape::AntalShape].Xvrd() << "," << Shape::PosShape[Shape::AntalShape].Yvrd() << ") { ";
  for (int ix=0; ix<storlek; ix++)
      cout << "(" << Pol[ix].x << ", " << Pol[ix].y << ")  ";
  cout << "}" << endl;
}

//--- Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include "Vertex.h"
#include "Shape.h"
using namespace std;

class Circle: public Shape {
  private:
    Vertex *CircPos;
    int pX, pY;
    double radius;
  public:
    Circle();
    Circle(int x, int y, int radie);
    ~Circle();
    virtual double area();
    virtual void printShape();};
#endif

//--- Circle.cpp

#include <iostream>
#include <cmath>
#include "Circle.h"
using namespace std;

Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) {
  cout << "\t   Circle_Constructor" << endl;
  if (radius > 0) {
     CircPos[Shape::AntalShape++] = Vertex(pX, pY);
  } else { CircPos = 0; }
}
Circle::~Circle() {
  cout << "\t   Circle_Destructor" << endl;
//  delete[] CircPos;
}
double Circle::area() { return M_PI * radius * radius; }
void Circle::printShape() {
  cout << "CIRCLE: (" << CircPos->Xvrd() << ", " << CircPos->Yvrd() << "), r=" << radius << endl;
}


The linked list:

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

//--- ShapeList.h

#ifndef SHAPELIST_H
#define SHAPELIST_H
#include "vertex.h"
#include "Shape.h"
using namespace std;

class ShapeNode;

class ShapeList{
 protected:
   ShapeNode *first;
 public:
   ShapeList();
   ShapeList(const ShapeList &shapes);
   ~ShapeList();
   void add(const Shape &s); // COPY of s into the list
   void remove(const Vertex &v);
   double area(); // return the TOTAL area of ALL figures
   void print(); // print out ALL the list
};
#endif

//--- ShapeList.cpp

#include <iostream>
#include "ShapeList.h"
#include "vertex.h"
using namespace std;

class Shape;

class ShapeNode{
protected:
  Shape *s;
  ShapeNode *prev, *next;
  ShapeNode(Shape *_s, _ShapeNode *_prev, ShapeNode *_next);
};
ShapeList::ShapeList() { first = 0; }
ShapeList::ShapeList(const ShapeList &shapes) {}
ShapeList::~ShapeList() {}
void ShapeList::add(const Shape &s) {
  ShapeNode *tmp = first;
  first = new ShapeNode(&s, tmp, 0); // not work: why ?
  tmp->prev = first;
}
void ShapeList::remove(const Vertex &v) {}
double ShapeList::area() {}
void ShapeList::print() {}


Main for test the code:

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

//--- Test.cpp

#include <iostream>
#include "vertex.h"
#include "Polygon.h"
#include "Circle.h"
#include "ShapeList.h"
using namespace std;

int main()
{
  ShapeList list;
  Vertex varr[] = { Vertex(0, 0), 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));
  Vertex nyarr[] = { Vertex(1, 1), Vertex(9, 2) };
  list.add(Polygon(6, 8, nyarr, 2));
  list.add(Rectangle(10, 4, 4, 2));
  list.add(Circle(3, 3, 5));
  list.add(Point(9, 6, 1));
  list.print();
  cout << "Total Area: " << list.area() << endl;
  ShapeList list2(list);
  list2.print();
  cout << "Total Area: " <<list2.area() << endl;
  list.remove(Vertex(5, 5));
  list.print();
  cout << "Total Area: " << list.area() << endl;
  list2.print();
  cout << "Total Area: " << list2.area() << endl;
  return 0;
}


Please, I'm very grateful for all h e l p !!!.
Thank You in advance.

Adalte.
Last edited on
first = new ShapeNode(&s, tmp, 0); // not work: why?

Three reasons (a novice hat-trick, if you will). First, your ShapeNode ctor is in the protected section, whereas it should be public. Secondly, your ShapeNode ctor is NOT IMPLEMENTED! Third, you are trying to access ShapeNode's protected members from ShapeList.

So you must make ShapeNode's ctor public, implement it, and add ShapeList as a friend of ShapeNode so it can access its protected (or private) members.

Also, you don't have a Rectangle or Point class, but you refer to them in main.
Topic archived. No new replies allowed.