Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

example of inhaertance in c++

+11 votes
asked Sep 6 by Sonam Sharma (220 points)

2 Answers

+2 votes
answered Sep 8 by Peter Minarik (80,300 points)
This site has a good explanation of inheritance in C++, check this out!

0 votes
answered Sep 25 by Gulshan Negi (1,380 points)

Well, in C++, inheritance is a fundamental concept in OOP (object-oriented programming) that allows you to create new classes such as derived or child classes.  

Here is the simple example of inheritance in C++.

#include <iostream>

class Base {
    void sayHello() {
        std::cout << "Hello from Base" << std::endl;

class Derived : public Base {
    void sayHi() {
        std::cout << "Hi from Derived" << std::endl;

int main() {
    Derived derivedObj;
    derivedObj.sayHello(); // Inherited from Base
    derivedObj.sayHi();    // Specific to Derived

    return 0;

There are many kind of inhertance, if you are looking to know about it in detail then you can read about it from here in detail.


Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.