PPP2 Chapter 13 Exercise 8 Help Needed

Exercise Specifications:

Define a class Regular_hexagon (a regular hexagon is a six-sided polygon
with all sides of equal length). Use the center and the distance from the
center to a corner point as constructor arguments.


Here's my current code:
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
// Osman Zakir
// 5 / 23 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 8
// Exercise Specifications:
/**
 * Define a class Regular_hexagon (a regular hexagon is a six-sided polygon
 * with all sides of equal length). Use the center and the distance from the
 * center to a corner point as constructor arguments.
 */

#include <iostream>
#include "../../Graph.h"
#include "../../Simple_window.h"

struct Regular_hexagon : Graph_lib::Shape
{
	Regular_hexagon::Regular_hexagon(Graph_lib::Point center, int distance)
		: m_center{ center }, m_distance{ distance } {}
	Regular_hexagon::Regular_hexagon()
		: m_center{}, m_distance{} {}
	Graph_lib::Point get_center() const { return m_center; }
	int get_distance() const { return m_distance; }
	int side_length() const { return m_side_length; }
	void draw_lines() const;
private:
	Graph_lib::Point m_center;
	int m_distance; // distance from center to a corner point
	int m_side_length;
};

int main()
{
	
}

void Regular_hexagon::draw_lines() const
{
	
}


I need help with the draw_lines() function, specifically. How should I go about defining it? Any help is appreciated. And if someone could let me know if what I have so far is good enough, that would also be greatly appreciated. Thanks in advance.
https://en.wikipedia.org/wiki/Regular_polygon#/media/File:Regular_polygon_6_annotated.svg
What can you tell about regular hexagon?


What is "m_side_length"? You don't initialize it anywhere.
Do you need the default constructor?
I put in the default constructor just in case I need to declare regular hexagon with everything default values by default.

I know what a regular hexagon is. It's a polygon with six sides of equal length. The book also mentions it, so I'd find out from there even I hadn't known beforehand.

m_side_length is (meant to be) the length of one side of the hexagon. Where should I initialize it, then?

Also, I'm having trouble making exception messages show up for some reason. This is my current code:
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
// Osman Zakir
// 5 / 23 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 8
// Exercise Specifications:
/**
 * Define a class Regular_hexagon (a regular hexagon is a six-sided polygon
 * with all sides of equal length). Use the center and the distance from the
 * center to a corner point as constructor arguments.
 */

#include <iostream>
#include "../../Graph.h"
#include "../../Simple_window.h"

struct Regular_hexagon : Graph_lib::Shape
{
	Regular_hexagon::Regular_hexagon(Graph_lib::Point center, int distance)
		: m_center{ center }, m_distance{ distance } {}
	Regular_hexagon::Regular_hexagon()
		: m_center{}, m_distance{} {}
	Graph_lib::Point get_center() const { return m_center; }
	int get_distance() const { return m_distance; }
	int side_length() const { return m_side_length; }
	void draw_lines() const;
private:
	Graph_lib::Point m_center;
	int m_distance; // distance from center to a corner point
	int m_side_length;
};

int main()
{
	using namespace Graph_lib;
	using namespace std;

	constexpr int win_width = 600, win_height = 400;
	const Point win_tl{ 200, 200 };
	Simple_window win{ win_tl, win_width, win_height, "Regular hexagons" };

	try
	{
		Regular_hexagon reg_hex{ {win_width / 2, win_height / 2}, 5 };
		reg_hex.set_color(Color::black);
		win.attach(reg_hex);
	}
	catch (const exception &e)
	{
		Text err_msg_start{ {(win_width / 2), (win_height / 2)}, "Exception: " };
		Text err_msg{ {(win_width / 2) + 10, (win_height / 2)}, e.what() };
		err_msg_start.set_color(Color::black);
		err_msg.set_color(Color::black);
		win.attach(err_msg_start);
		win.attach(err_msg);
	}
	catch (const runtime_error &e)
	{
		Text err_msg_start{ { (win_width / 2), (win_height / 2) }, 
			"Runtime_error: " };
		Text err_msg{ { (win_width / 2) + 10, (win_height / 2) }, e.what() };
		err_msg_start.set_color(Color::black);
		err_msg.set_color(Color::black);
		win.attach(err_msg_start);
		win.attach(err_msg);
	}
	catch (...)
	{
		Text err_msg{ { (win_width / 2), (win_height / 2) },
			"Unknown exception: " };
		err_msg.set_color(Color::black);
		win.attach(err_msg);
	}

	win.wait_for_button();
}

void Regular_hexagon::draw_lines() const
{
	if (number_of_points() < 6)
	{
		error("Can't have a hexagon with less than 6 points");
	}
	Closed_polyline cpl;
	cpl.draw_lines();
}


When I ran it the first time, it got aborted due to an unhandled exception. I tried to catch said exception, but now nothing's show up on my window. No message anything.

I need help with that and also with figuring out how to use draw_lines() and the non-default constructor to correctly initialize the hexagon.

Help with the exception-handling part first, please, though. Then the hexagons come afterwards.
Last edited on
Topic archived. No new replies allowed.