I know I should break it off at the separate classes, but aside from telling me to do it, it doesn't show me how. I just feel completely lost.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
class CArea {
private:
string shape;
public:
CArea(const string& strShape) : shape(strShape) {}
void display_choice() {cout << "You have chosen " << shape;}
void output(const float& result) const
{
cout << "The area of the " << shape << " is: " <<
setprecision(2) << fixed << result << endl;
}
virtual void get_area() = 0;
template <class T>
static T get_number(const string& cstrPrompt, const T& ctMin = 0, const T& ctMax = 0)
{
T tRet = 0;
string strBuffer;
do {
cout << cstrPrompt;
getline(cin, strBuffer);
stringstream ss(strBuffer);
if (!(ss >> tRet)) {
cout << "Not a valid number!" << endl;
continue;
}
else if (ctMin || ctMax) {
if ((tRet < ctMin) || (tRet > ctMax)){
cout << "Range: " << ctMin << " <= x <= " << ctMax << endl;
}
else {
break;
}
}
else {
break;
}
}while(true);
return tRet;
}
};
class Circle : public CArea {
private:
float radius;
public:
Circle() : CArea("Circle"), radius(0.0f) {}
void get_area()
{
display_choice();
radius = get_number<float>("\nPlease enter Radius> ");
output(radius * radius * 3.14f);
}
};
class Square : public CArea {
private:
float side;
public:
Square() : CArea("Square"), side(0) {}
void get_area()
{
display_choice();
side = get_number<float>("\nPlease enter side> "),
output(side * side);
}
};
I know the main would get it's own section as well correct?
int main()
{
const string strMenu = "\n\n********* MENU ***********\n"
"\t1. Circle\n"
"\t2. Square\n"
"\t3. Exit\n"
"\tCHOICE> ";
int choice;
Circle c;
Square s;
do {
choice = CArea::get_number<int>(strMenu, 1, 3);
if (choice == 1)
c.get_area();
else if (choice == 2)
s.get_area();
else
break;
} while (true);
cout << "Thank you for using the program !" << endl;
return 0;
}