相关疑难解决方法(0)

如何从派生类函数调用父类函数?

如何使用C++从派生类调用父函数?例如,我有一个名为的类parent,以及一个child从父派生的类.每个班级都有一个print功能.在孩子的打印功能的定义中,我想调用父母的打印功能.我该怎么做呢?

c++ oop inheritance

563
推荐指数
6
解决办法
59万
查看次数

如何通过指向派生类的基类指针调用Base类方法

class Base
{
  public:
    virtual void foo()
    {}
};

class Derived: public Base
{
  public:
    virtual void foo()
    {}
};

int main()
{
    Base *pBase = NULL;
    Base objBase;
    Derived objDerived;

    pBase = &objDerived;
    pBase->foo();

    /*Here Derived class foo will be called, but i want this to call 
    a base class foo. Is there any way for this to happen? i.e. through 
    casting or something? */
}
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism

13
推荐指数
3
解决办法
7805
查看次数

标签 统计

c++ ×2

inheritance ×1

oop ×1

polymorphism ×1