Pergunta

I'm trying to pass down the print function from an abstract class "shape" to the derived classes "circle" and "square". It should print out "Name: " followed by the name of the shape. For some reason, it isn't working right. Do I need to re-declare and re-define print for each derived class? Or did I not pass it down properly or is the name is not stored properly in the derived functions?

For clarification: I just want to make it print out the names properly as it loops through the array. Any advice would be appreciated.

Thanks!

These should be the pertinent bits of code:

print() is declared in the shape header file.

In shape.cpp

void shape::setName( const string &shapeName )
{
    name = shapeName;
}

//return name
string shape::getName() const
{
    return name;
}


void shape::print() const
{
    cout<<"Name: "<<getName()<<endl;
}

constructor in square: //(identical to other derived classes)

square::square(const string &name, const int &sideLength)
    : shape ( name )
{
    setSideLength(sideLength);
}

In the main:

//create derived class objects with side lengths:
    square square1(
        "square", 3);

//an object array named shapesArray is created with an instance of square and circle

for(int x = 0; x < 3; x++)
    {
        shapesArray[x]->print();
        cout<<"The distance around this shape is: "<<shapesArray[x]->getDistanceAround()<<endl;
Foi útil?

Solução

I think what you are trying to do is as below.note that you need to set "name" correctly for the derived classes either by calling setname method exclusively or in their constructors itself.

  class Shape
  {
   public:
   string name; 
   void setName( const string &shapeName )
   {
     name = shapeName;
    }
   string getName() const
  {
   return name;
   }
   void print() const
  {
   cout<<"Name: "<<getName()<<endl;
   }
  };

  class Circle:public Shape
  {
   public:
    Circle()
    {
      name = "Circle";
    } 
   };

   class Square:public Shape
   { 
    public:
    Square()
    {
      name = "Square";
    }
    };
  int main()
  {
   Shape* shapesArray[5];
   shapesArray[0]->setName("check");
   Circle lCircle;      
   shapesArray[1]=&lCircle;
   Square lSquare;
shapesArray[2]=&lSquare;  

   for(int x = 0; x < 3; x++)
   {
     shapesArray[x]->print();

  }

}

Outras dicas

If you are going to use a pointer, it is best to use the C++ features of dynamic binding with a virtual function:

virtual string shape::getName() const //in shape
{
    return "shape";
}
virtual string square::getname() const //in square
{
    return "square";
}

shape *s();
s = &shapeobj;
s.getName(); //shape
s = &squareobj;
s.getName(); //square

From your title, I assume you have an abstract base class for shape in a header shape.h, which you haven't shown us, and which declares print().

That counts as a declaration in each derived class. Now you need to define print() in each derived class, to provide an implementation. It looks something like:

void square::print() {
  // implementation
}
void triangle::print() {
  // implementation
}

Wherever you provide a definition in the cpp file, you also need to provide a declaration in the header file. The header is the declaration of your class, the cpp is just the implementation.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top