Problem with Node and virtual functions

I have to create an abstract class SHAPE with subclasses Polygon, Circle, Rectangle and Point. And I have to create dubbly linked list SHAPELIST but I can solve the problem:

// 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();
Vertex & operator[](int index) { return this[index]; }
Vertex operator[](int index) const { return this[index]; }
friend class Polygon;
};
#endif

// vertex.cpp
#include <iostream>
#include "vertex.h"
using namespace std;
Vertex::Vertex() : x(0),y(0) {}
Vertex::Vertex(int px, int py) : x(px),y(py) {}
Vertex::~Vertex() {}
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,Large;
public:
Polygon();
Polygon(int x,int y,Vertex *varr,int num);
~Polygon();
Vertex & operator[](int index) { return Pol[index]; }
Vertex operator[](int index) const { return Pol[index]; }
const Polygon & operator=(const Polygon & Parr);
friend ostream & operator<<(ostream &os,const Polygon &p);
void add(Shape &s);
double area();
int minx();
int miny();
int numVertices();
void polUtskr();
};
#endif

// Polygon.cpp
#include <iostream>
using namespace std;
#include "polygon.h"
#include "Vertex.h"
class Shape;
Polygon::Polygon() : pX(0),pY(0),Large(0),Pol(0) {}
Polygon::Polygon(int x,int y,Vertex *varr,int num) : pX(x),pY(y),Large(num) {
if (Large>0) {
Shape::PosSh=Vertex(pX,pY);
Pol=new Vertex[Large];
for (int ix=0;ix<Large;ix++)
Pol[ix]=varr[ix];
// Pol[Large++]= ?
} else { Pol=0; }
}
Polygon::~Polygon() {}
const Polygon & Polygon::operator=(const Polygon & Parr) {
if (this != &Parr) {
Large=Parr.Large;
delete[] Pol;
Pol=new Vertex[Large];
for(int ix=0;ix<Large;ix++)
Pol[ix]=Parr.Pol[ix];
}
return *this;
}
void Polygon::add(Shape &s) {
Vertex *tmp;
tmp=new Vertex[Large +1];
for(int ix=0;ix<Large;ix++)
tmp[ix]=Pol[ix];
delete[] Pol;
Pol=tmp;
}
double Polygon::area() {
double s=0.0;
double r=0.0;
for(int ix=1;ix<Large;ix++)
s +=(Pol[ix-1].x*Pol[ix].y)-(Pol[ix].x*Pol[ix-1].y);
r=s/2;
return abs(r);
}
int Polygon::minx() {
int m=2^32;
for(int ix=0;ix<Large;ix++) {
if(Pol[ix].x<m)
m=Pol[ix].x;
}
return m;
}
int Polygon::miny() {
int m=2^32;
for(int ix=0;ix<Large;ix++) {
if(Pol[ix].y<m)
m=Pol[ix].y;
}
return m;
}
int Polygon::numVertices() {
return Large;
}
void Polygon::polUtskr() {
for(int ix=0;ix<Large;ix++) {
cout <<"("<<Pol[ix].x<<","<<Pol[ix].y<<")"; }
}
ostream & operator<<(ostream &os, const Polygon &p) {
os<<"{";
for(int ix=0;ix<p.Large;++ix)
os<<"("<<p.Pol[ix].Xvrd()<<","<<p.Pol[ix].Yvrd()<<") ";
os <<"}";
return os;
}

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

class Rectangle : public Shape {
private:
Vertex *RecPos;
int pX,pY,bredd,hjod;
public:
Rectangle();
Rectangle(int x,int y,int width,int height);
~Rectangle();
friend ostream & operator<<(ostream &os,const Rectangle &p);
void add(Shape &s);
double area();
int minx();
int miny();
void recUtskr();
};
#endif

// Rectangle.cpp
#include <iostream>
#include <cmath>
using namespace std;
#include "Rectangle.h"
#include "Vertex.h"

class Shape;

Rectangle::Rectangle() : pX(0),pY(0),bredd(0),hjod(0),RecPos(0) {}
Rectangle::Rectangle(int x, int y, int width, int height) : pX(x),pY(y),bredd(width),hjod(height) {
if(bredd>0 || hjod>0) {
Shape::AntRec++;
RecPos=new Vertex[Shape::AntRec];
RecPos[Shape::AntRec]=Vertex(pX, pY);
} else { RecPos=0; }
}
Rectangle::~Rectangle() {}
void Rectangle::add(Shape &s) {}
double Rectangle::area() {
return bredd * hjod;}
int Rectangle::minx() {}
int Rectangle::miny() {}
void Rectangle::recUtskr() {}
ostream & operator<<(ostream &os,const Rectangle &p) {}

// 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,radius;
public:
Circle();
Circle(int x,int y,int radie);
~Circle();
friend ostream & operator<<(ostream &os,const Circle &p);
void add(Shape &s);
double area();
int minx();
int miny();
void cirUtskr();
};
#endif

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

class Shape;

Circle::Circle() : pX(0),pY(0),radius(0),CircPos(0) {}
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() {}
void Circle::add(Shape &s) {}
double Circle::area() {
return M_PI * radius * radius;}
int Circle::minx() {}
int Circle::miny() {}
void Circle::cirUtskr() {}
ostream & operator<<(ostream &os, const Circle &p) {}

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

class Point : public Shape {
private:
Vertex *PoiPos;
int AntPoi,pX,pY,Langd;
public:
Point();
Point(int x,int y,int size);
~Point();
friend ostream & operator<<(ostream &os,const Point &p);
void add(Shape &s);
double area();
int minx();
int miny();
void poiUtskr();
};
#endif

// Point.cpp
#include <iostream>
#include <cmath>
#include "Point.h"
#include "Vertex.h"
using namespace std;

class Shape;

Point::Point() : AntPoi(0),pX(0),pY(0),Langd(0),PoiPos(0) {}
Point::Point(int x,int y,int size) : pX(x),pY(y),Langd(size) {
if(Langd>0) {
AntPoi++;
PoiPos=new Vertex[AntPoi];
PoiPos[AntPoi]=Vertex(pX, pY);
} else { PoiPos=0; }
}
Point::~Point() {}
void Point::add(Shape &s) {}
double Point::area() {
return Langd;
}
int Point::minx() {}
int Point::miny() {}
void Point::poiUtskr() {}
ostream & operator<<(ostream &os,const Point &p) {}

// 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();
Shape(const Shape &s);
virtual ~Shape();
virtual void add(Shape &s)=0;
virtual void remove(const Vertex &v);
virtual double area();
virtual void ShapeUtskr();
};
#endif

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

Shape::Shape() : AntPlg(0),AntRec(0),AntCir(0),AntPoi(0) {}
Shape::Shape(const Shape &s) {}
Shape::~Shape() {}
void Shape::remove(const Vertex &v) {}
double Shape::area() {}
void Shape::ShapeUtskr() {}

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

class ShapeList {
protected:
ShapeList *next;
ShapeList *back;
int Cant;
public:
ShapeList();
ShapeList(const ShapeList &shapes);
~ShapeList();
void add(const Shape &s);
void remove(const Vertex &v);
double area();
void print();
};
#endif

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

ShapeList() {
ShapeList *first = new ShapeList();
first->next = first->back = first;
}
ShapeList(const ShapeList &shapes) {}
~ShapeList() {}
void add(const Shape &s) {}
void remove(const Vertex &v) {}
double area() {}
void print() {}

// Test.cpp
#include <iostream>
#include "vertex.h"
#include "Polygon.h"
#include "Rectangle.h"
#include "Circle.h"
#include "Point.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));
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;
}

I use Vertex for figure's positions and Polygons points: It's a condition. Thousand Thanks !!!
Last edited on
Topic archived. No new replies allowed.