Undermine2k
Undermine2k

Reputation: 1491

passing string pointers from class definition

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

    } 

};

Upvotes: 0

Views: 338

Answers (4)

Aesthete
Aesthete

Reputation: 18848

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(); };

Upvotes: 2

SwiftMango
SwiftMango

Reputation: 15284

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.

Upvotes: 0

Summer_More_More_Tea
Summer_More_More_Tea

Reputation: 13356

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.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

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.

Upvotes: 0

Related Questions