Single Inheritance Example in C++


This code defines two classes Base and Derived where Derived is derived from Base class publicly. The Base class has an integer data member a and a member function display() which displays the value of a. The Derived class has a member function show() which displays the message "Show of Derived".

In the main function, an object of Derived class is created named d. The integer data member a of Base class is assigned a value of 100. The display() function of Base class is called using the d object which displays the value of a. The show() function of Derived class is called using the d object which displays the message "Show of Derived".

Source Code

#include <iostream>
using namespace std;
class Base
{
public:
    int a;
    void display()
    {
        cout<<"Display of Base "<<a<<endl;
    }
};
class Derived:public Base
{
public:
    void show()
    {
        cout<<"Show of Derived"<<endl;
    }
};
int main()
{
    Derived d;
    d.a=100;
    d.display();
    d.show();
}
/*
Reuse
Polymorphism
*/
To download raw file Click Here

Output

Display of Base 100Show of Derived

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples