The problem i’m having is with the string parameter. I’m not exactly sure how to use it. I just want classification to have undefined length string from the onset until entered by user. The error i’m getting is declaration of ‘std::string classification’ shadows a parameter when i type string classification. What is the correct way to pass the string argument to class members?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Shapes
{ //Begin Class Definition
private:
float side;
float height;
int exponent;
string *classification;
public:
Shapes(float side, float height, string * classification);
//CONSTRUCTOR
~Shapes(){};
//DESTRUCTOR
float area(float side, float height, string *classification);
float perimeter(float side, float height, string *classification);
}; // End Class Definition
int power(float side, int exponent)
{
int i;
int total[exponent];
float sum;
for ( i = 0 ; i < exponent ; i ++ )
{
total[i]= side;
sum *= total[i] ;
}
return sum;
}
float Shapes::area(float side, float height, string *classification)
{
float area=0.0;
string classification;
getline(cin,string);
if (classification == "square" )
{
area = power(side,2);
return area;
}
if (classification == "triangle" )
{
area = (side* height) / 2 ;
return area;
}
if (classification == "hexagon" )
{
float constant = 2.598706;
area= constant * power(side,2);
return area;
}
if (classification == "circle" )
{
}
};
You are redeclaring the
stringnamed classification. You only have to declare that variable in the class declaration once for it to be used in all your member functions. You are also using the same name for your arguments which is confusing and dangerous.You should also be careful what you are doing with pointers here, it seems like you’re not exactly sure when to use them, or use references. If you indeed tried to compare your
string* classificationargument like this,if (classification == "triangle" ), you would realise that you can’t compare std::string* to const char*Ideally, you should be using enumerations here, like so.
Even better than that you would be using inheritence and polymorphism and overloading functions like
area()