- C++

C++ program to demonstrate simple inheritance

This program will demonstrate the simple inheritance in C++. Inheritance is the capability of a class to derive properties and characteristics from another class. Inheritance is an important concept in OOPS. More about inheritance its modes and types can be found here https://www.geeksforgeeks.org/inheritance-in-c/ .

In the below code we will create a base class A and we derive class A in another class B. In main function we will create object of class B and access the functions of the base class A.

Code:


/*C++ program to demonstrate simple inheritance.*/

#include 
using namespace std;

/*Base Class*/
class A
{
    public:
        // function definition
        void functionA(void)
        {
           cout << "Hello I am functionA of Class A" << endl;
        }
};

/*Derived Class*/
class B:public A
{
    public:
        // function definition
        void functionB(void)
        {
           cout << "Hello I am functionB of Class B" << endl;
        }
};
int main()
{
    //create object of Berived class B
    B objectB;
    // Now, we can access the function of class A (Base class)
    objectB.functionA();
    objectB.functionB();
    return 0;
}

Output:

Leave a Reply