Inheritance is the most powerful feature of object-oriented programming. Inheritance is a form of software reusability in which new classes are created from existing classes by acquiring their attributes (data) and behaviors (functions).

The existing classes are called base/parent classes whereas the new classes are called derived child classes.

The derived class not only inheritance all the capabilities of the base class but also can add new features & refinements of its own.

By adding these refinements the base class remains unchanged.

In the Single Inheritance there will only one base class and only one derived class. The following is the schematic representation Class A is the base class and class B is the derived class.

Only one class is derived from the base class. Have a look at the following example

Here, as you can see in the schematic representation, the father is the base class and son is the derived class.

Single Inheritance

When we have a base class B and derived class D. Class B contains one private data member, one public data member, and three public member functions.

Class D contains one private data member and two public member functions.

Simple Program for Single Inheritance Using C++ Programming program

#include<iostream>
using namespace std;
class B {
int a;
public: int b;
void get_ab() {
cout << "Enter Values for a and b :"; cin >> a >> b;
}
int get_a() {
return a;
}
void show_a() {
cout << "n a = " << a;
}
};
class D: private B { //class B is inherited in class D here
int c;
public: void mul() {
get_ab();
c = b * get_a();
}
void display() {
show_a();
cout << "n b = " << b;
cout << "n c = " << c;
}
};
int main() {
D d;
d.mul();
d.display();
return 0;
}

Output:-Enter Values for a and b : 10 20
a = 10
b = 20
c = 200

Explanation of the program

Here, we have first declared a base class called B and then derived a class called D from it. D inherits all the features of the base class B.

This article is written by our awesome writer
Comments to: Simple Program for Single Inheritance in C++ Programming

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password