C++ program to demonstrate an Example of Single Inheritance

By | 01.01.2017

Example of Single Inheritance


Write a C++ program to demonstrate an Example of Single Inheritance. Here’s a Simple C++ program to demonstrate an Example of Single Inheritance in C++ Programming Language.


What are Inheritance in C++ ?


  • Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
  • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
  • The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Types of Inheritance : :

There are different types of inheritance : :

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid (Virtual) Inheritance

Below is the source code for C++ program to demonstrate an Example of Single Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C++ program to demonstrate an Example of Single Inheritance  */

#include<iostream>
using namespace std;
class B
{
    int a;
    public:
    int b;
    void get_ab();
    int get_a();
    void show_a();
};

class D: private B
{
    int c;
    public:
    void mul();
    void display();
};

void B::get_ab()
{
    cout<<"\nEnter Values for a and b :: ";
    cin>>a>>b;
}

int B::get_a()
{
    return a;
}

void B::show_a()
{
    cout<<"\na = "<<a<<"\n";
}

void D::mul()
{
    get_ab();
    c=b*get_a();
}

void D::display()
{
    show_a();
    cout<<"\nb = "<<b<<"\n";
    cout<<"\nc = "<<c<<"\n\n";
}


int main()
{

                        D d;
                        d.mul();
                        d.display();
                        d.mul();
                        d.display();
                        return 0;

}

OUTPUT : :


/* C++ program to demonstrate an Example of Single Inheritance  */

Enter Values for a and b :: 3 4

a = 3

b = 4

c = 12


Enter Values for a and b :: 5 6

a = 5

b = 6

c = 30


Process returned 0

Above is the source code and output for C++ program to demonstrate an Example of Single Inheritance which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

4.5 2 votes
Article Rating
Category: C++ Programming Inheritence Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments