urgent help needed C++

Hi, I would need urgent help to solve my following issue:
1. To store the vertices into 'Square'.
2. Store Square into an ObjArray.


This is the latest code i could provide as i am really quite new to C++ and i'm rushing for time to get this task done in 1 day time. I could't figure out for days, will appreciate if someone will be willing to help.

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.
Do you really need to store 4 vertices a square? It's fully defined by two vertices on a diagonal. In other words Point ordinate[2];

In Square(string,bool,int,Point); I understand that the string and the bool are the parameters for the ShapeTwoD, but what are the int and Point? Without know what they are supposed to be, it's hard to know if the code is right.
Hi dhayden, int is the number of vertices and Point is the vertices cin from user. the 4 ppoints.
Okay. I'll accept that you're going to store 4 points for the square. So a square is simply a ShapeTwoD with 4 points:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Square : public ShapeTwoD {
	private:
	Point ordinate[4];  

	public:
	Square();
 	Square(const string &name,bool warpspace, Point points[4]);
};
...
Square::Square(const string &name,bool warpspace, Point points[4]) :
    ShapeTwoD(name, warpspace)
{
    for (int =0; i<4; ++i) ordinate[i] = points[i];
}
If the user can give four points, then the system has to check that they form a valid square.

Furthermore, even if the user gives four points, the Square does not have to store them. It only has to store "necessary data".
@dhayden - we have a warpspace parameter to Square's constructor, so the geometry might be non-Euclidean? :)

@OP - I'm kind of unclear where you're stuck. I do have a number of comments though.

ShapeTwoD.h - Your include guard is incomplete. You define a symbol, but fail to exclude the contents of the head if the symbol is already defined. I don't see this as a problem currently, as you don't appear to include the header multiple times. Just a comment that you need to research include guards and use them correctly.
http://www.cplusplus.com/forum/general/71787/

ShapeTwoD.h line 8: Not a good idea to put using namespace std inside an include file.

main.cpp lines 5-13: It's best practice to avoid the use of globals. These variables should be locals within main.

4th snippet: I see an include of points.h, but I don't see that you posted it, although there is a struct in main.cpp named Point. Not sure if these are the same.

5th snippet:
Line 1: I don't see size declared anywhere. I'm going to assume it is properly declared and has a const value. If it's not a const value, you can't declare an array with a non-const value.

line 2: This is an out of bounds assignment. Lets assume size is 10. elements of arrayofShape are [0]-[9]. You're trying to store into [10].


line 3: Why are you incrementing size? This will cause the subsequent for loop to go out of bounds. (size now equals 11). You may have been assuming size was 0 here, but that would make your allocate illegal.

Have you learned about std::vector yet? If the number of points in the array is going to change, this would best be done with a vector<Shape>. If not, then what I think you want is something like the following:
1
2
3
4
5
6
7
8
9
  Shape *arrayofShape;
  int  size = 10;            //  Number of shapes to be stored
  int numShapes = 0;   // Number of shapes currently stored
  
  // Do once
  arrayofShape = new Shape * [size];  // Allocate array of pointers to shapes

  //   Do for each share to be stored
  arrayofShape[numShapes++] = &someshape;       // Assume someshape exists 

Note that size and numShapes are independent variables.

Don't forget to deallocate arrayofShape when you're done with it, or you will have a memory leak.



Last edited on
Topic archived. No new replies allowed.