Wednesday, April 6, 2016

Single Inheritance – Public Inheritance in C++

Inheritance in which the derived class is derived from only one base class is calledsingle inheritance.
Let us consider a simple example to illustrate the single inheritance. Program given below shows a base class B and a derived class D. The class B contains one private data member, one public data member and three public member functions. The class D contains one private data member and two public member functions.
Given below is the output of the above program:
In the above program, following things require to be noted:
  1. The class D is a public derivation of the base class B. Therefore, class D inherits all the public members of class B an retains their visibility. Thus, a public member of the base class B is also a public member of the derived class D
  2. The private members of the base class B cannot be inherited by the derived class D
  3. The class D, in effect, will have more members than what it contains at the time of declaration
  4. The program illustrates that the objects of class D have access to all the public members of class B
Let us have a look at the function show_a() and mul():
Although the data member a is a private member of base class B and cannot be inherited, objects of derived class D are able to access it through an inherited member function of class B.

0 comments:

Post a Comment