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!
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
intconst ZERO = 0;
constdouble 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()
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).