Please, h e l p me !

Hi!
It's very important for me to solve this problem. I'm very grateful for all hints, proposition or h e l p I can get. Thank You in advance.

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

The following is given to me and 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()

Objects (Vertex, Shape, Polygon and Circle) definitions and implemetations:

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
134
135
136
137

//--- Vertex.h

#ifndef VERTEX_H
#define VERTEX_H
class Vertex {
 private:
   int x, y;
 public:
   Vertex() : x(0), y(0) {};
   Vertex(int px, int py) : x(px), y(py) {};
   ~Vertex();
   int Xvrd() { return x; };
   int Yvrd() { return y; };
   friend class Polygon; // Not necesary, just in case
};
#endif

//--- Shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
#include "Vertex.h"
using namespace std;
class Shape {
 protected:
   Vertex PosSh;
   int AntPlg, AntRec, AntCir, AntPoi;
 public:
   Shape(const Shape &s) : AntPlg(0), AntRec(0), AntCir(0), AntPoi(0) {};
   virtual ~Shape();
   virtual double area() = 0;
   virtual void printShape();
};
#endif

//--- 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, size;
 public:
   Polygon();
   Polygon(int x, int y, Vertex *varr, int num);
   ~Polygon();
   void add(Shape &s);
   double area();
   void printPol();
};
#endif

//--- Polygon.cpp

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

class Shape;

Polygon::Polygon(int x, int y, Vertex *varr, int num) : pX(x), pY(y), size(num) {
  if (size > 0) {
     Shape::PosSh = Vertex(pX,pY);
     Pol = new Vertex[size];
     for(int ix=0; ix<size; ix++)
        Pol[ix] = varr[ix];
   } else {
     Pol=0;}
}
Polygon::~Polygon() {}

double Polygon::area() {
  double s = 0.0;
  double r = 0.0;
  for (int ix=1; ix<size; ix++)
      s += (Pol[ix-1].x * Pol[ix].y) - (Pol[ix].x * Pol[ix-1].y);
  r = s / 2;
  return abs(r);
}
void Polygon::printPol() {
  for(int ix=0; ix<size; ix++)
     cout << "(" <<Pol[ix].x << "," << Pol[ix].y << ")";
}

//--- 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();
   void add(Shape &s);
   virtual double area();
   virtual void printCir();
};
#endif

//--- Circle.cpp

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

class Shape;

Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) {
  Shape::AntCir++;
  if (radius > 0){
     CircPos = new Vertex[Shape::AntCir];
     CircPos[Shape::AntCir] = Vertex(pX, pY);
  } else { CircPos=0; }
}
Circle::~Circle() {}
double Circle::area() { return M_PI*radius*radius; }
void Circle::printCir(){}


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);
  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
http://www.cplusplus.com/forum/articles/1295/
Highlights:
When asking about code
Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for. Posting a few hundred lines of code, saying "it doesn't work", will get you ignored. Posting a dozen lines of code, saying "after line 7 I was expecting to see <x>, but <y> occurred instead" is much more likely to get you a response.
And please use [code][/code] tags
Topic archived. No new replies allowed.