FLTK. Stroustup's striped closed polyline exercise.

Hi!
I study c++ by myself using Stroustrup's manual.
Problem that I faced is: when I defined new object of Striped_closed_polyline in main(), Striped_circle object stoped to draw stripes. Another words, if you delete Striped_closed_polyline, then Striped_circle's stripes comes back.
Can anyone show me where is mistake? I fight with it for a long time :)

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "..\..\..\Simple_window.h"
#include "..\..\..\std_lib_facilities.h"
#include "..\..\..\Graph.h"

struct Arc_F : Shape{
public:
	Arc_F(Point xy, int Width, int Height, int StartAngle, int EndAngle) 
		: x{ xy.x }, y{ xy.y }, w{ Width }, h{ Height },
		startangle{ StartAngle }, endangle{ EndAngle }{}
	int* get_parameters(){
		int temparray[4];
		temparray[0] = x;
		temparray[1] = y;
		temparray[2] = w;
		temparray[3] = h;
		return temparray;
	}
	void set_style(Line_style sty) { ls = sty; }
	void set_width(int wid) { width = wid; }
private:
	int x, y, w, h, width = 1;
	Line_style ls = 0;
	double startangle = 90.0, endangle = 180.0;
	void draw_lines() const {
		fl_line_style(ls.style(), width);
		fl_begin_points();
		fl_arc(x, y, w, h, startangle, endangle);
		fl_end_points();
	}
};
struct Box_F : Shape{
public:
	//use Width and Height bigger than arc diameters
	Box_F(Point xy, int Width, int Height, Arc_F &arc) 
		: x{ xy.x }, y{ xy.y }, lw{ Width - arc.get_parameters()[2] },
		lh{ Height - arc.get_parameters()[3] },
		w{ arc.get_parameters()[2] }, h{ arc.get_parameters()[3] }{}
	
	void set_style(Line_style sty) { ls = sty; }
private:
	int x, y, w, h;
	int lw, lh;
	Line_style ls = 0;
	double quarter = 90.0;
	double startangle = quarter, endangle = quarter*2;
	void draw_lines() const{
		fl_line_style(ls.style(), 1);
		fl_rectf(x + w / 2, y, lw, h + lh);
		fl_rectf(x, y + h / 2, lw+w, lh);
		fl_pie(x, y, w, h, startangle, endangle);
		fl_pie(x, y + lh, w, h, quarter * 2, quarter * 3);
		fl_pie(x + lw, y + lh, w, h, quarter * 3, quarter * 4);
		fl_pie(x + lw, y, w, h, 0.0, quarter);
	}
};
struct Arrow_F : public Shape{
public:
	
	Arrow_F(Point xy, Point xy1, int HeadSize)
		: x{ xy.x }, y{ xy.y }, x1{ xy1.x }, y1{ xy1.y },
		s{ HeadSize }{}

	void set_style(Line_style sty) { ls = sty; }
	void set_width(int wid) { width = wid; }
private:
	int x, y, x1, y1, s, width = 1;
	Line_style ls = 0;
	int lw, lh;
	void draw_lines() const{
		fl_line_style(ls.style(), width);
		fl_line(x, y, x1, y1);
		fl_begin_polygon();
		fl_circle(x1, y1, s);
		fl_end_polygon();
		//fl_polygon(x1, y1, x1 - s, y1 - s / 2, x1 - s, y1 + s / 2);
	}
};
class Smiley : public Circle{
public:
	Smiley(Point p, int rr) : Circle(p, rr){}
	void draw_lines() const {
		Circle::draw_lines();
		fl_arc(center().x - radius()/2, center().y + radius()/3, radius(), radius() / 2, 180, 360);
		fl_circle(center().x - radius() / 4, center().y - radius() / 4, 2);
		fl_circle(center().x + radius() / 4, center().y - radius() / 4, 2);
	}
};
class Frowny : public Circle{
public:
	Frowny(Point p, int rr) : Circle(p, rr){}
	void draw_lines() const {
		Circle::draw_lines();
		fl_arc(center().x - radius() / 2, center().y + radius() / 3, radius(), radius() / 2, 0, 180);
		fl_circle(center().x - radius() / 4, center().y - radius() / 4, 2);
		fl_circle(center().x + radius() / 4, center().y - radius() / 4, 2);
	}
};
class SmileyHat : public Smiley{
public:
	SmileyHat(Smiley& sm) : Smiley(sm.center(), sm.radius()){}
	void draw_lines() const {
		//fl_begin_polygon();
		fl_line(center().x + radius(), center().y - radius(), center().x,
			center().y - radius() * 2, center().x - radius(),
			center().y - radius());
		fl_arc(center().x - radius(), center().y - radius() - radius()/4,
			radius() * 2, radius()/2, 180, 360);
		//fl_end_polygon();
	}
};
class FrownyHat : public Frowny{
public:
	FrownyHat(Frowny& fn) : Frowny(fn.center(), fn.radius()){}
	void draw_lines() const {
		fl_line(center().x + radius(), center().y - radius(), 
			center().x+radius()/2,	center().y - radius()/2*3,
			center().x - radius(),	center().y - radius());
		fl_arc(center().x - radius(), center().y - radius() - radius() / 4,
			radius() * 2, radius() / 2, 180, 360);
	}
};
class Striped_rectangle : public Shape{
public:
	Striped_rectangle(Point xy, Point xy1) : x{ xy.x }, y{ xy.y }, x1{ xy1.x }, y1{ xy1.y }, polyline{ false } { add(xy); add(xy1); }
	Striped_rectangle(Point center, int rr) : r{ rr }, x{ center.x }, y{ center.y }, polyline{ false } { add(center); }
	Striped_rectangle() : polyline{ true } {}
	~Striped_rectangle(){}
	void add(Point p){ Shape::add(p); }
	void draw_lines() const{
		if (!polyline){
			switch (number_of_points()) {
			case 2:
				fl_rect(x, y, x1 - x, y1 - y);
				for (int i = y; i < y1; i += style().width() + 1){
					fl_line(x, i, x1 - 1, i);
				}
				break;
			case 1:
				fl_color(FL_RED);
				fl_circle(x + r, y + r, r);
				for (int i = y; i < y1; i += style().width() + 1){
					fl_line((x + r) + 1 - sqrt(pow(r, 2) - pow(i - (y + r), 2)), i, sqrt(pow(r, 2) - pow(i - (y + r), 2)) + (x + r) - 1, i);
				}
				break;
			default:
				break;
			}
		}
		else{
			fl_begin_loop();
			for (int i = 0; i < number_of_points(); ++i){
				fl_vertex(point(i).x, point(i).y);
			}
			fl_end_loop();
			for (int i = point(0).y; i < point(number_of_points()-1).y; i += style().width() + 1){
				fl_line(x, i, x1 - 1, i);
			}
		}
		return;
	}
private:
	int x, y, x1, y1;
	int r;
	bool polyline;
};
class Striped_circle : public Striped_rectangle{
public:
	Striped_circle(Point p, int rr) : Striped_rectangle (p, rr){}
};
class Striped_closed_polyline : public Striped_rectangle{
public:
	Striped_closed_polyline() : Striped_rectangle(){}
};
//-------------------------------------------------
//ex. p.526
class B1{
public:
	virtual string vf(){
		return "B1::vf ";
	}
	string f(){
		return "B1::f ";
	}
	virtual string pvf() = 0;
};
class D1 : public B1{
public:
	string vf(){
		return "D1::vf ";
	}
	string f(){
		return "D1::f ";
	}
	string pvf(){
		return "D1::pvf | ";
	}
};
class D2 : public D1{
public:
	string pvf(){
		return "D2::pvf | ";
	}
};
class B2{
public:
	virtual string pvf() = 0;
};
class D21 : public B2{
public:
	string pvf(){
		return str;
	}
	string str = "string ";
};
class D22 : public B2{
public:
	string pvf(){
		return to_string(i)+" ";
	}
	int i = 777;
};
string f(B2& b){
	return b.pvf();
}
//-------------------------------------------------
int main(){
	try{
		//Fl_Window win(600, 600, "Header");
		Simple_window win(Point(10,10),600,600,"Header");
		Smiley sm(Point(150, 150), 30);
		win.attach(sm);
		SmileyHat smh(sm);
		win.attach(smh);
		Frowny fn(Point(250, 150), 30);
		win.attach(fn);
		FrownyHat fnh(fn);
		win.attach(fnh);

		Striped_rectangle rect1(Point(50, 50), Point(100, 100));
		rect1.set_style(Line_style(Line_style::solid,1));
		win.attach(rect1);
		
		Striped_circle circle1(Point(250, 250), 25);
		circle1.set_style(Line_style(Line_style::solid, 1));
		win.attach(circle1);
		
		Striped_closed_polyline scp;
		scp.set_style(Line_style(Line_style::solid, 1));
		scp.add(Point(400, 400));
		scp.add(Point(450, 450));
		scp.add(Point(420, 475));
		scp.add(Point(470, 500));
		scp.add(Point(410, 520));
		win.attach(scp);
		
		/*Arc_F arc1(Point(0, 0), 35, 35, 0, 90);
		arc1.set_color(Color::blue);
		win.attach(arc1);

		Box_F box1(Point(100, 100), 100, 100, arc1);
		box1.set_color(Color::red);
		win.attach(box1);

		Arrow_F arrow1(Point(100, 50), Point(150,150), 4);
		arrow1.set_color(Color::white);
		win.attach(arrow1);

		//B1 b1;
		D1 d1;
		B1& b1ptr = d1;
		D2 d2;
		D21 d21;
		D22 d22;

		Text text1(Point(100,100),to_string(arrow1.number_of_points()));
		Text text1(Point(70, 70), 
			b1.vf()+b1.f()+d1.vf() + d1.f() + d1.pvf() + 
			b1ptr.vf() + b1ptr.f() + b1ptr.pvf() + 
			d2.vf() + d2.f() + d2.pvf() +
			f(d21) + f(d22));
		win.attach(text1);*/

		//win.redraw();
		win.show();
		win.wait_for_button();
	}
	catch (exception& e){
		return 1;
	}
	catch (...){
		return 2;
	}
}
up
Topic archived. No new replies allowed.