Object-oriented Geometry Calculator

I'm taking a language-independent class (Programming Logic and Design) where we are programming in C++. C++ is not a requirement, and we are not being taught C++.

I have a working Geometry Calculator (single file) and just realized that I need separate files for each class. I have no idea how to approach this and would appreciate any suggestions. Thanks!

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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int const ZERO = 0;
const double PI = 3.14159; // Set PI as constant

int main()
{
	
int selection; // Menu selection
double radius, areaCircle, // For area of a circle
length, width, areaRectangle, // For area of a rectangle
base, height, areaTriangle; // For area of a triangle

cout <<"This program will loop until the EXIT is selected."<<endl;

do 
{
// Display the menu and allow user to choose 1-4.
cout <<endl;
cout << "Geometry Calculator\n\n";
cout << "1. Calculate the Area of a Circle\n";
cout << "2. Calculate the Area of a Rectangle\n";
cout << "3. Calculate the Area of a Triangle\n";
cout << "4. Quit\n\n";
cout << "Enter your selection: ";
cin >> selection;
cout << fixed << showpoint << setprecision(4);

// Validate menu selection.
if (selection < 1 || selection > 4)
{
cout << endl;
cout << "*Error: Menu option must be 1, 2, 3, or 4.\n\n";

}

// Respond to user's menu selection.
switch (selection)
{
case 1: // Find area of a circle
cout << endl;
cout << "Enter the radius for the circle:"<<endl;
cin >> radius;
if (radius < 0)
{
cout << endl;
cout << "\t**Error: Radius cannot be negative.\n\n";
}
else
{
areaCircle = PI * pow(radius, 2.0);
cout << areaCircle << endl;

}
break;

case 2: // Find area of a rectangle
cout << endl;

cout << "Enter the length and width of the rectangle:"<<endl;
cin >> length;
cin >> width;

if (length <= 0 || width <= 0)
{
cout << endl;
cout << "*Error: Length and width must be positive ";
cout << "values.\n\n";

}
else
{
areaRectangle = length * width;
cout << areaRectangle <<endl;

}
break;

case 3: // Find area of a triangle
cout << endl;
cout << "Area of a Triangle\n\n";
cout << "Enter the base and height of the triangle:"<<endl;
cin >> base;
cin >> height;

if (base <= 0 || height <= 0)
{
cout << endl;
cout << "*Error: Base and height must be positive ";
cout << "values.\n\n";

}
else
{
areaTriangle = base * height * 0.5;
cout << areaTriangle << endl;
}

case 4: // Quit the program

cout << "Good Bye";

}

} while(selection != 4);

 

} // End main() 
Last edited on
Step 1: identify classes. Currently, this code contains no classes of your own creation.
That would be Circle, Rectangle, and Triangle. Would my menu remain here (in main), and each of the classes and their associated calculations in separate files?
Thanks!
Unlike Java, in C++ the decision to split your classes across files is up to you, you can do it, not do it, or do a mix of it. You could technically have all the classes defined in the same file as main, but my recommendation is to do it the standard way of separate hpp and cpp for each class (but no cpp for templates).
Topic archived. No new replies allowed.