[URGENT] storing of vertices

Hi, I have need to do a program which consist of Base class 'Shape' and it consist of 3 sub classes (Square,Rectangle and cross). Im new to C++ thus my codes doesn't really show much. I would need help in how to store the vertices for x and y for each of them.

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
#include "ShapeTwoD.h"

ShapeTwoD::ShapeTwoD()
{
	this->name = "";
	this->containsWarpSpace = true;
}

ShapeTwoD::ShapeTwoD(string name, bool containsWarpSpace)
{
	this->name = name;
	this->containsWarpSpace = containsWarpSpace;
}

//Getter
string ShapeTwoD::getName()
{
	return name;
}

bool ShapeTwoD::getContainsWarpSpace()
{
	return containsWarpSpace;
}



//Setter
void ShapeTwoD::setName(string name)
{
	this->name = name;
}

void ShapeTwoD::setContainsWarpSpace(bool containsWarpSpace)
{
	this->containsWarpSpace = containsWarpSpace;
}








// UNSURE
double ShapeTwoD::computeArea()
{
	return 0.00;
}

string  ShapeTwoD::toString()
{
}

bool ShapeTwoD::isPointInShape(int x , int y)
{
	this-> x = x;
	this-> y = y;
}

bool ShapeTwoD::isPointOnShape(int x , int y)
{
	this-> x = x;
	this-> y = y;
}


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
#ifndef ShapeTwoD_H
#define ShapeTwoD_H
#include <iostream>

#include <string>

#include <sstream>

using namespace std;

class ShapeTwoD
{

	protected:
	string name;
	bool containsWarpSpace;
	int x,y;	//added
	double area;
	
	public:
	ShapeTwoD();			//Default Constructor
	ShapeTwoD(string, bool);	//takes in string name and bool containsWarpSpace

	//Getter
	string getName();
	bool getContainsWarpSpace();
	
	virtual double computeArea();
	virtual string toString();
	virtual bool isPointInShape(int , int);
	virtual bool isPointOnShape(int , int);

	//Setter
	void setName(string name);
	void setContainsWarpSpace(bool containsWarpSpace);
	
	
};

#endif /* ShapeTwoD_H */



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "ShapeTwoD.h"

class Square:public ShapeTwoD{

	private:
  	int xVal,yVal;

	public:
	Square();
 	Square(string,bool,int,int);

	void setSquareCord();

 	int xvalue[4];
        int yvalue[4];	


};


1
2
3
4
5
6
7
#include "Square.h"

Square::Square(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval)
{	
	xVal = xval;
	yVal = yval;
}



I'm not sure on how what to add in the sub classes so that i could store square into ArrayofShapes and then display their individual computeArea for all 3 of them. I'm very new to C++ so please pardon me.
Last edited on
Your base class should not contain any information not available to any descendant. Also, it appears that your base class should be named "Shape", right?

A shape cannot be turned into another type of shape (can it?) so the name of the shape should also not be modifiable. The name() method should be const.

Likewise, computing the area of a shape does not change the shape, so the area() method should also be const.

A shape cannot exist by itself. (You cannot have a 'dog', but you can have a 'Beagle' or a 'Corgi'.) Hence, the base class's virtual functions must be abstract -- which is why they have that = 0 at the end.

1
2
3
4
5
6
class Shape
{
  public:
    virtual string name() const = 0;
    virtual double area() const = 0;
};

Descendant classes should provide additional members specific to that type of thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Square: public Shape
{
  private:
    double length;

  public:
    Square( double length = 0 ): length( length ) { }  // Constructor

    virtual string name() const { return "Square"; }  // A square is always a square.
    virtual double area() const
    {
      return length * length;
    }

    double GetLength() const { return length; }

    void SetLength( double x ) { length = x; }
};

If you wish your object to have a position in space such that you can compute whether or not a point is inside it, you can also define an upper-left corner.

1
2
  private:
    double x, y, length;

Many squares and rectangles define coordinates with both upper-left and lower-right corners. (If you do this, though, you have to guarantee that no setter is allowed to make your square non-square.)

1
2
  private:
    double x0, y0, x1, y1;

You can then add a virtual function to your base "Shape" class which each descendant class can provide a meaning for:

1
2
3
4
5
class Shape
{
  public:
    ...
    virtual bool isPointInShape( double x, double y ) const = 0;

Hope this helps.
Last edited on
Hi, thanks for your reply. Would be glad if you can see your pm to get a better view.
This is the latest code i could provide:


For the shape class, this is the bare requirement to be in this format.

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
#include "ShapeTwoD.h"

ShapeTwoD::ShapeTwoD()
{
	this->name = "";
	this->containsWarpSpace = true;
}

ShapeTwoD::ShapeTwoD(string name, bool containsWarpSpace)
{
	this->name = name;
	this->containsWarpSpace = containsWarpSpace;
}

//Getter
string ShapeTwoD::getName()
{
	return name;
}

bool ShapeTwoD::getContainsWarpSpace()
{
	return containsWarpSpace;
}



//Setter
void ShapeTwoD::setName(string name)
{
	this->name = name;
}

void ShapeTwoD::setContainsWarpSpace(bool containsWarpSpace)
{
	this->containsWarpSpace = containsWarpSpace;
}


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
#define ShapeTwoD_H
#include <iostream>

#include <string>

#include <sstream>

using namespace std;



 

class ShapeTwoD
{

	protected:
	string name;
	bool containsWarpSpace;
	
	public:
	ShapeTwoD();			//Default Constructor
	ShapeTwoD(string, bool);	

	//Getter
	string getName();
	bool getContainsWarpSpace();
	
	virtual double computeArea();
	

	//Setter
	void setName(string name);
	void setContainsWarpSpace(bool containsWarpSpace);
	
	
};


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
struct Point{
int x, y;
}

int choice;

bool containsWarpSpace;

int vertices;

double area;

string shape, stype;

int main()

{

	cout << "[Input sensor data]\n";

	cout << "Please enter name of shape :";

	cin >> shape;


	cout << "Please enter special type :";

	cin >> stype;



	//Get bool for containsWarpSpace

	if (stype =="WS")

	{

	containsWarpSpace = true;

	}

	else

	{

	containsWarpSpace = false;

	}

	

	if (shape == "Square")

	{

	  

	vertices = 4;

	for (int i = 0; i < vertices; i++){

 //if it's a square. It prompt 4 times for x n y , if cross 12
 //create a square object with the array of vertices
 //stay shape into objarray.          
 
 
 //I have problem storing this Point[] as it's a struct which i declare at main.cpp and it couldnt pass it / read in Square when compiled.
 Point ordinates[i];

 

            cout << "Please enter x-ordinate of pt. " << (i+1) << ":";

            cin >> ordinates[i].x;


            cout << "Please enter y-ordinate of pt. " << (i+1) << ":";

            cin >> ordinates[i].y;

          Square square(shape, containsWarpSpace, vertices, ordinates[i]);

            //shape2d.push_back(square);



        	}


The Square class which can be done anyway in order to achieve to create a square and store into an array to compute it's area there after with the vertices.

1
2
3
4
5
6
7
square.h

Square::Square(string ShapeName, bool warpspace, int vertices, Point ordinates[]):ShapeTwoD(ShapeName, warpspace)
{
	vert = vertices
	ord[] = ordinates[]
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "ShapeTwoD.h"
#include "Point.h"

class Square:public ShapeTwoD{

//It wont be able to read my Point here as it's a struct at main.cpp
	private:
	Point ordinate[];  

	public:
	Square();
 	Square(string,bool,int,Point);

//a compute formula but not yet added here. (a virtual function)  




This is really all i could provide you to understand what i'm doing as i'm running out of time to solve the issue.

1. To store vertices into Square
2. after creating square, store in an obj array
for e.g.

1
2
3
4
5
6
7
Shape *arrayofShape[size];
arrayofShape[size] = &shape;
size++;
//when size ++ it prepare to store next shape that will be created.

for (int i=0; i<size; i++)
arrayofShapes[i]->computeArea();


I hope to hear from you soon as i'm really urgent. Thanks. If possible do update my square files and main.cpp to show so allow me to understand how could i add vertices and to store into an array. Much appreciated, thanks.
Topic archived. No new replies allowed.