

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to call a parent class function from derived class function in C++?
The following is an example to call parent class function from 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
The derived class d1 function is called. The parent class p1 function is called.
In the above program, a parent class p1 is created and a function first() is defined in it.
class p1 { public: void first() { cout << "\nThe parent class p1 function is called."; } };
A derived class is created, which is inheriting parent class p1 and overloading the parent class function first().
class d1 : public p1 { public: void first() { cout << "The derived class d1 function is called."; p1::first(); } };
The function of d1 class is calling the function of p1 class.
p1::first();
- Related Questions & Answers
- How to call the constructor of a parent class in JavaScript?
- How to call parent constructor in child class in PHP?
- How to call a parent window function from an iframe using JavaScript?
- How to explicitly call base class constructor from child class in C#?
- How to call a JavaScript function from C++?
- How to Call a Lua function from C?
- Accessing protected members in a C++ derived class
- How to call a JavaScript Function from Chrome Console?
- First class function in JavaScript
- How to call a Java function inside JavaScript Function?
- How to get the return value from a function in a class in Python?
- How to call a function in JavaScript?
- What all is inherited from parent class in C++?
- How to call a JavaScript function from an onClick event?
- How to call a JavaScript function from an onsubmit event?
Advertisements