Inheritance Example in C++


Description:

C++ Inheritance. In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. ... In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class.

Example Program:

Here is the simple program using inheritance in C++.

Source Code

#include <iostream>
using namespace std;
class Base
{
public:
    Base()
    {
        cout<<"Non-param Base"<<endl;
    }
    Base(int x)
    {
        cout<<"Param of Base "<<x<<endl;
    }
};
class Derived:public Base
{
public:
    Derived()
    {
        cout<<"Non-Param Derived"<<endl;
    }
    Derived(int y)
    {
        cout<<"Param of Derived "<<y<<endl;
    }
    Derived(int x,int y):Base(x)
    {
        cout<<"Param of Derived "<<y<<endl;
    }
};
int main()
{
    Derived d1,d2(5),d3(10,50);
}
To download raw file Click Here

Output

Non-param BaseNon-Param DerivedNon-param BaseParam of Derived 5Param of Base 10Param of Derived 50

Basic Programs


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



Learn All in Tamil © Designed & Developed By Tutor Joes