How to use of "new" for polygons

Hi all,

The definitions of Circle and Polygon are here in Graph.h and graph.cpp
http://www.stroustrup.com/Programming/Graphics/

For some exercise I need to have some unnamed shapes which are made using the new keyword. Both Circle and polygon are kinds of Shape.

For example if I have a vector_ref<Circle> vc; I can using this statement add an unnamed

Circle into that vector: vc.push_back(new Circle (Point (p), 50)); because I can supply

parameters (which are a point and a radius) of a circle when defining it. But for

polygons the subject is different.

For having a polygon I must declare it first, say,
Polygon poly; then add points to it, say, poly.add(Point(p));

Now it has caused a problem for me.

Consider I have a vector of polygons, Vector_ref<Polygon> vp; Now how to add (that is push back) a polygon using the new keyword just like I did for circle please?
Last edited on
looks like you add it the same way as you would a Circle, but you needn't bother passing any parameters to it when you new one up.
OR
you new up a Polygon, the call add() on it as many times as you want, and then add it to your vector.
Last edited on
vp.push_back(new Polygon);
Thank you for your opinions:

The code I have is this. Having looked at it you can help me better :)
Is there a solution without using pointer (except for new)?

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
#include <GUI.h>
using namespace Graph_lib;

//---------------------------------

class Math_shapes : public Window {

public:
	Math_shapes(Point, int, int, const string&);

private:
	//Widgets
	Menu menu;
	Button quit_button;
	In_box x_coor;
	In_box y_coor;

	Vector_ref<Circle> vc;
	Vector_ref<Graph_lib::Rectangle> vr;
	Vector_ref<Graph_lib::Polygon> vt;
	Vector_ref<Graph_lib::Polygon> vh;

	//Action fucntions
	void circle_pressed()     { 
		int x = x_coor.get_int();
		int y = y_coor.get_int();
		vc.push_back(new Circle (Point(x,y), 50));
		attach(vc[vc.size()-1]);
		redraw();
	}
	void square_pressed()     { 
		int x = x_coor.get_int();
		int y = y_coor.get_int();
		vr.push_back(new Graph_lib::Rectangle (Point(x,y), Point(x+100,y+100)));
		attach(vr[vr.size()-1]);
		redraw();
	}
	void triangle_pressed()     { 
		int x = x_coor.get_int();
		int y = y_coor.get_int();
		vt.push_back(new Graph_lib::Polygon); // Problem is here!!
		attach(vt[vt.size()-1]);
		redraw();
		
	}
	void hexagon_pressed()     { 
		int x = x_coor.get_int();
		int y = y_coor.get_int();
		Graph_lib::Polygon h;
		h.add(Point(x,y)); h.add(Point(x+50,y+50)); h.add(Point(x+50,y+80));
		h.add(Point(x,y+100)); h.add(Point(x-50,y+80)); h.add(Point(x-50,y+50));
		vh.push_back(h);
		attach(vh[vh.size()-1]);
		redraw();
	}

	void quit()               { hide(); }

	// Call-back functions
	static void cb_circle   (Address, Address pw)  { reference_to<Math_shapes>(pw).circle_pressed(); }
	static void cb_square    (Address, Address pw)  { reference_to<Math_shapes>(pw).square_pressed(); }
	static void cb_triangle  (Address, Address pw)  { reference_to<Math_shapes>(pw).triangle_pressed(); }
	static void cb_hexagon   (Address, Address pw)  { reference_to<Math_shapes>(pw).hexagon_pressed(); }
	static void cb_quit     (Address, Address pw)  { reference_to<Math_shapes>(pw).quit(); } 
};

//----------------------------------------------------------------------------------

Math_shapes::Math_shapes(Point xy, int w, int h, const string& title):
	Window(xy, w, h, title),
	menu (Point(x_max()-150,70),120,30,Menu::vertical, "MathShapes"),
	quit_button (Point(x_max()-100, 20), 70,20, "Quit", cb_quit),
	x_coor(Point(x_max()-450,30),50,20,"x coordinate: "),
	y_coor(Point(x_max()-250,30),50,20,"y coordinate: ")
{

	attach(x_coor);
	attach(y_coor);
	attach(quit_button);
	menu.attach(new Button(Point(0,0),0,0,"Circle",cb_circle));
	menu.attach(new Button(Point(0,0),0,0,"Square",cb_square));
	menu.attach(new Button(Point(0,0),0,0,"Equilateral triangle",cb_triangle));
	menu.attach(new Button(Point(0,0),0,0,"Hexagon",cb_hexagon));
	attach(menu);
}

//-------------------------------------------

int main() 
	try {
		Math_shapes M_s(Point(100,100), 800, 600, "Math Shapes");
		return gui_main();
}

catch(...) 
{
	return 0;
}
You can do either:
1
2
vt.push_back(new Graph_lib::Polygon); 
vt.[vt.size()-1].add(/**/);
or
1
2
3
auto tmp = new Graph_lib::Polygon;
tmp.add(/**/);
vt.oush_back(tmp);
Your code seems to be just what I was looking for but I aware you of the results as soon as I tested it.
Thank you.
And what I wasn't aware of was that "auto". May you please give a small explanation of it?
Auto deducts type of the variable from initializing expression. So I would not have to write Graph_lib::Polygon* tmp = new Graph_lib::Polygon; (or, worse std::unordered_multimap<std::string, std::chrono::time_point<std::chrono::high_resolution_clock>>::const_iterator)
Thank you very much :)
I used this code for triangle:
1
2
3
4
vt.push_back(new Graph_lib::Polygon); 
vt.[vt.size()-1].add(/**/);
vt.[vt.size()-1].add(/**/);
vt.[vt.size()-1].add(/**/);


But at third add() (line 4) it faces with an exception and the windows vanish. So I used Closed_polyline itstead of Polygon. And it worked!
Now everything is as I and the exercise would want but I don't know why the code did't work with Polygon but worked with closed_polyline.

Last edited on
Topic archived. No new replies allowed.