In this shot, we'll learn how to call a base
class function from a derived
class function in C++.
We can call a base
class function from a derived
class using the following syntax:
baseClassName :: functionName();
Now, let's see some examples.
base
classLet's first create a base
class.
class base {public:void display () {cout << "Function of base class" << endl;}}
base
.display()
.derived
classLet's create a derived
class by extending the base
class.
class derived: public base {public:void display () {cout << "Function of derived class" << endl;// calling base class functionbase :: display();}}
derived
by extending the class base
.base
class function display()
.base
class function display()
from the derived
class.#include <bits/stdc++.h>using namespace std;class base {public:void display () {cout << "Function of base class" << endl;}};class derived: public base {public:void display () {cout << "Function of derived class" << endl;// calling base class functionbase :: display();}};int main() {// create object of derived classderived d;d.display();return 0;}
bits/stdc++.h
library.base
class and a derived
class.main()
function, we create an object of the derived
class and call its display()
function.display()
function of the derived
class:display()
function of the base
class.