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

How to use C ++ to call the parent function from the derived class? For example, I have called a parentclass, and called childthe class, which is derived from the parent. Each class has a printfunction. When you define a child's printing capabilities, I would like to call the parents of the print function. How do I do it?


#1st Floor

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.


#2nd Floor

If access modifiers base class member function is protected or public, you can call the base class member function from a derived class. Non-members may be virtual and virtual member functions derived from the base class member function calls. Please refer to the program.

#include<iostream>
using namespace std;

class Parent
{
  protected:
    virtual void fun(int i)
    {
      cout<<"Parent::fun functionality write here"<<endl;
    }
    void fun1(int i)
    {
      cout<<"Parent::fun1 functionality write here"<<endl;
    }
    void fun2()
    {

      cout<<"Parent::fun3 functionality write here"<<endl;
    }

};

class Child:public Parent
{
  public:
    virtual void fun(int i)
    {
      cout<<"Child::fun partial functionality write here"<<endl;
      Parent::fun(++i);
      Parent::fun2();
    }
    void fun1(int i)
    {
      cout<<"Child::fun1 partial functionality write here"<<endl;
      Parent::fun1(++i);
    }

};
int main()
{
   Child d1;
   d1.fun(1);
   d1.fun1(2);
   return 0;
}

Output:

$ g++ base_function_call_from_derived.cpp
$ ./a.out 
Child::fun partial functionality write here
Parent::fun functionality write here
Parent::fun3 functionality write here
Child::fun1 partial functionality write here
Parent::fun1 functionality write here

#3rd floor

Given named Parentparent and called Childthe child class, you can do the following:

class Parent {
public:
    virtual void print(int x);
}

class Child : public Parent {
    void print(int x) override;
}

void Parent::print(int x) {
    // some default behavior
}

void Child::print(int x) {
    // use Parent's print method; implicitly passes 'this' to Parent::print
    Parent::print(x);
}

Please note that Parentthe actual name of the class, rather than keywords.


#4th floor

If your base class called Base, and your function is called FooBar(), you can use Base::FooBar()a direct call it

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

void ChildOfBase::FooBar()
{
  Base::FooBar();
}

#5th Floor

I braved the obvious danger: the function is called, if it is defined in the base class, the function automatically available in a derived class (unless it is private).

If the derived class has a function with the same signature, and then by adding the name of the base class two colons base_class::foo(...)to disambiguate. You should note that, unlike Java and C #, C ++ does not have "basic" (keyword superor basebecause of support for C ++), multiple inheritance , which could 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();

        // and when foo() is not called for 'this':
        bottom b;
        b.left::foo();  // calls b.foo() from 'left'
        b.right::foo();  // call b.foo() from 'right'
    }
};

By the way, you can not step twice from the same class derived directly, it will not be cited as a basis for class on the basis of another class.

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

#6th floor

Use parent scope resolution operator calls the parent method.

Parent :: method ()

class Primate {
public:
    void whatAmI(){
        cout << "I am of Primate order";
    }
};

class Human : public Primate{
public:
    void whatAmI(){
        cout << "I am of Human species";
    }
    void whatIsMyOrder(){
        Primate::whatAmI(); // <-- SCOPE RESOLUTION OPERATOR
    }
};

#7th floor

In MSVC, there is a Microsoft-specific keyword for this are: __super


MSDN: allows you to call the base class is explicitly stated to be implemented as a function of rewrite.

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

Original articles published 0 · won praise 0 · Views 2225

Guess you like

Origin blog.csdn.net/p15097962069/article/details/103907061