【问题标题】:Passing a function from an abstract class从抽象类传递函数
【发布时间】:2014-04-12 05:31:33
【问题描述】:

我正在尝试将打印函数从抽象类“shape”传递给派生类“circle”和“square”。它应该打印出“名称:”,然后是形状的名称。由于某种原因,它无法正常工作。我需要为每个派生类重新声明和重新定义打印吗?还是我没有正确传递它,或者名称没有正确存储在派生函数中?

为了澄清:我只想让它在遍历数组时正确打印出名称。任何建议将不胜感激。

谢谢!

这些应该是相关的代码:

print() 在 shape 头文件中声明。

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

方形构造函数://(与其他派生类相同)

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

主要是:

//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;

【问题讨论】:

  • 通过打印功能?你的意思是超载吗?您是否将其设为虚拟(看起来您是从指针调用)?
  • 我想我正在尝试通过它。有人告诉我使用指向它的指针而不是执行 print()。我认为 print 函数将包含在派生类和原始函数中,但它不会打印出它们的名称。
  • 您的shape 类或square 类中是否定义了print()
  • 你能发布print的实现吗?
  • @R Sahu 我唯一使用或引用它的地方是我在这里复制的代码中。我应该在他们的 cpp 文件中为每个派生类都有一个特定的实现吗?

标签: c++ polymorphism abstract-class derived-class


【解决方案1】:

我认为您正在尝试做的事情如下。请注意,您需要通过专门调用 setname 方法或在其构造函数本身中为派生类正确设置“名称”。

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

  }

}

【讨论】:

  • 在构造函数中这是一件奇怪的事情。感谢您建议我检查它!
【解决方案2】:

如果你要使用指针,最好使用带有virtual函数的动态绑定的C++特性:

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

【讨论】:

  • 我不能在这个程序中使用虚函数,但是谢谢你的建议。
【解决方案3】:

根据您的标题,我假设您在标题 shape.h 中有一个形状的抽象基类,您没有向我们展示它,并且 声明 print()。

这算作每个派生类中的声明。现在您需要在每个派生类中定义 print(),以提供实现。它看起来像:

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

无论您在 cpp 文件中的何处提供定义,您还需要在头文件中提供声明。标头是你的类的声明,cpp 只是实现。

【讨论】:

  • 我在定义/cpp文件中为抽象基类定义了打印函数。(之前忘记包含了,抱歉!)如果是,是否需要为每个基类单独定义打算在每个基类中执行相同的功能?
  • 你的问题很混乱。只有基类。见编辑。
  • 我可以把我所有的代码都放在这里,但这会有点多。我在我的基类头文件中声明了 print 。我在我的基类 cpp 文件中定义了它。我在派生类中没有提到它。我从您的回答中假设该函数不能从派生类访问,除非我声明并在每个派生类中定义它?很抱歉造成混乱。
  • 不,我没有,但您的问题似乎仍在变化,我不再知道如何回答。不要发布您的整个代码:您需要创建一个最小、完整、经过测试和可读的示例 [stackoverflow.com/help/mcve]。然后问你真正想要的问题。
猜你喜欢
  • 2021-05-08
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2014-05-30
相关资源
最近更新 更多