Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 24, 2015

How to call a parent class function from derived class function?

How to call a parent class function from derived class function?


How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within each class there is a print function. In the definition of the child's print function I would like to make a call to the parents print function. How would I go about doing this?

Answer by Greg Hewgill for How to call a parent class function from derived class function?


Do something like this:

void child::print(int x)  {      parent::print(x);  }  

Answer by Andrew Rollings for How to call a parent class function from derived class function?


If your base class is called Base, and your function is called FooBar() you can call it directly using Base::FooBar()

void Base::FooBar()  {     printf("in Base\n");  }    void ChildOfBase::FooBar()  {    Base::FooBar();  }  

Answer by Motti for How to call a parent class function from derived class function?


I'll take the risk of stating the obvious, you call the function, if it's defined in the base class it's automatically available in the derived class (unless it's private).

If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...). You should note that unlike Java and C#, C++ does not have a keyword for "the base class" (super or base) since C++ supports multiple inheritance which may lead to ambiguity.

class left {  public:      void foo();  };    class right {  public:      void foo();  };    class bottom : public left, public right {  public:      void foo()      {          //base::foo();// ambiguous          left::foo();          right::foo();      }  };  

Incidentally, you can't derive directly from the same class twice since there will be no way to refer to one of the base classes over the other.

class bottom : public left, public left { // Illegal  };  

Answer by Andrey for How to call a parent class function from derived class function?


In MSVC there is a Microsoft specific keyword for that: __super


MSDN: Allows you to explicitly state that you are calling a base-class implementation for a function that you are overriding.

// deriv_super.cpp  // compile with: /c  struct B1 {     void mf(int) {}  };    struct B2 {     void mf(short) {}       void mf(char) {}  };    struct D : B1, B2 {     void mf(short) {        __super::mf(1);   // Calls B1::mf(int)        __super::mf('s');   // Calls B2::mf(char)     }  };  


Answer by user2170324 for How to call a parent class function from derived class function?


struct a{   int x;     struct son{    a* _parent;    void test(){     _parent->x=1; //success    }   }_son;     }_a;    int main(){   _a._son._parent=&_a;   _a._son.test();  }  

Reference example.

Answer by Ajay yadav for How to call a parent class function from derived class function?


If access modifier of base class member function is protected OR public, you can do call member function of base class from derived class. Please refer the program.

#include  using namespace std;    class Base1  {    protected:      virtual void fun(int i)      {        cout<<"Base1::fun functionality write here"<


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.