Single Inheritance in C++ Programming


Inheritance is a basic object oriented feature in which one class acquires and inherit the properties of another class. All the properties of the Base Class ( also known as the Parent Class or Super class ) are present in the Derived Class ( also known as the Child Class or Sub class )

Types of Inheritance :

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

Single Inheritance
      Single inheritance is one base class and one derived class. One in which the derived class inherits the one base class either publicly, privately or protected

In this program, there are two classes father and son. The son class is derived from the father class using the public keyword. This indicates that the public members of the father class are accessible from the son class.

The father class has a public member function house() that prints the message "Have own 2BHK House." Similarly, the son class has a public member function car() that prints the message "Have own Audi Car."

In the main() function, an object o of the son class is created. Since the son class is derived from the father class, it can access the house() member function of the father class using the dot (.) operator. Hence, the house() function is called for the object o. Next, the car() member function of the son class is called using the same object o.

Source Code

#include<iostream>
using namespace std;
//Single Inheritance in C++ Programming
class father
{
   public:
    void house()
    {
        cout<<"Have own 2BHK House."<<endl;
    }
};
class son:public father
{
    public:
    void car()
    {
        cout<<"Have own Audi Car."<<endl;
    }
};
int main()
{
    son o;
    o.house();
    o.car();
    return 0;
}
 

Output

Have own 2BHK House.
Have own Audi Car.
To download raw file Click Here

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