English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to Use extern in C ++How to Call Parent Class Function from Derived Class Function in C?

Below is an example of calling the parent class function from the derived class function.

Example

#include <bits/stdc++.h>
using namespace std;
class p1 {
   public:
   void first() {
      cout << "\nThe parent class p1 function is called.";
   }
};
class d1 : public p1 {
   public:
   void first() {
      cout << "The derived class d1 function is called.";
      p1::first();
   }
};
int main() {
   d1 d;
   d.first();
   return 0;
}

Output Result

The derived class d1 function is called.
The parent class p1 function is called.

In the above program, the parent class p is created1and defines the function first() within it.

class p1 {
   public:
   void first() {
      cout << "\nThe parent class p1 function is called.";
   }
};

Create a derived class that inherits from the parent class p1and overload the parent class function first().

class d1 : public p1 {
   public:
   void first() {
      cout << "The derived class d1 function is called.";
      p1::first();
   }
};

d1The function of the class is calling p1The function of the class.

p1::first();