0

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" ) 
    {

    } 

};

4 Answers 4

2

You are redeclaring the string named 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* classification argument 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.

class Shape
{
  public:
    enum Classification { SQUARE, TRIANGLE, CIRCLE };
}

Shape::Area(float side, float height, Classification shapeClass)
{
  if(shapeClass == SQUARE) {} // Etc
}

Even better than that you would be using inheritence and polymorphism and overloading functions like area()

class Shape { virtual float Area(); };
class Triangle : public Shape { virtual float Area(); };
Sign up to request clarification or add additional context in comments.

3 Comments

I like the enumeration way but my question is how do i call it in main. would it be like.. Shapes myshapes; if (cin.getline()=="Square") myshapes(side,height, 1) ;
myshapes(side,height, Shapes::Square);
But as Luchian mentioned, this is really basic C++ stuff, and the way you're trying to do it is bad practice. You really need to learn about some of the concepts mentioned in the answers here, otherwise you're going to find your work confusing and painfully hard to manage.
1

In your code Shapes::area, you don't need to redefine a variable classification typed std::string, because one of your parameters string *classification there.

You can use your parameter *classification == "circle", the leading * is because you declare your parameter a pointer type. An alternatives is to declare classification as string &classification in C++, which is a reference. And with a reference parameter, you can use directly like classificaiton == "circle".

Hope that helps.

Comments

0

What is the correct way to pass the string argument to class members?

Don't. You already have access to the member inside the methods of a class, you don't need an extra variable, nor to pass it as a parameter.

float Shapes::area(float side, float height, string *classification)
{
    string classification;

In this case, you have a parameter called classification, a member with the same name, and a local variable with the same name.

Come to think of it, you don't even need pointers.

What you do need is to read a C++ book. I'm not being sarcastic nor mean. C++ is hard to get right, and you seem to have started working with pointers before you understand the even more basic concepts of variables or scope.

Comments

0

You have the parameter name the same as the class member. They are not the same variable.

If you want to make the class member a shadow copy of the parameter, you should do:

float Shapes::area(float side, float height, string classification)
{
     float area=0.0;
     this->classification = classification;
     getline(cin,classification);

     //rest goes here
}

Here, this is a pointer to the holder class.

You will need to change the class declaration:

class Shapes
{
    string classification;
}

Do not use pointer to class unless really necessary. Use reference if you want to change the value.

P.S. Do not put semi colon in the end of function definition.

4 Comments

I did that but now i'm getting: invalid conversion from std::string*' to char'
@Undermine2k Sorry I accidentally put a new there. There should not be new because new is for memory allocation but string() already does that.
Just to let you know, this->classification = string(classification); should be this->classification = classification; (its already a string) and getline(cin,string); should be getline(cin,classification); or some other string. However, it doesn't make sense to assign a string and then overwrite it on the next line though..
@JesseGood Yes you are right. Just realize that string::operator= already creates a duplicate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.