Single inheritance
let us consider a simple example to understand single inheritance in the below program show a base class B and a derived class D. The class B consist 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
example of single inheritance
< iostream >
using namespace std;
class B
{
a; // private its not inherit
:
b; // public its ready for inherit
get_data();
get_A();
show_A();
};
class D: B //public derivation
{
c;
:
mul();
display();
};
B :: get_data()
{
a=8; b=90;
}
B:: get_A()
{
a;
}
B:: show_A()
{
cout << “a=” << a << “\n”;
}
D:: mul()
{
c=b*get_A();
}
D:: display()
{
cout << “a=” << get_A() << “\n”;
cout << “b=”<
cout << “c=”<
}
d.display();
main()
{
D d;
d.get_data();
d.mul();
d.show_A();
d.b=20;
d.mul();
d.display();
}
|
output:
a=8
a=8
b=90
c=720
a=8
b=20
c=160
|
The class D is a public derivation of the base class B. Therefore D inherits all the public members of B and retains their visibility. Thus a public member of the base class B is also
a public member of the derived class D. The private member of B cannot be inherit by D. The class D, in effect, will have more members than what it contains at the time of declaration
in the above example that the object of class D have access to all the public members of B. Let us have a look at the functions show_A() and mul():
so here the data member a is private in B and cannot be inherited, objects of D are able to access it through an inherited member function of B
let us now consider the case of private derivation
class B
{
a;
:
b:
get_data();
get_A();
show_A();
};
class D : B // private derivation
{
C;
:
mul();
display();
};
|
The membership of the derived class D is shown in fig 8.4 in private derivation, the public members of the base class become private members of the derived class. Therefore the objects of D can not have direct access to the public member function of B.
the statement such as
d.get_data(); // get_data() is private
d.get_A(); // so also get_A()
d.show_A(); // and show_A()
|
will not work. however , These functions can be used inside mul() and display like the normal functions as shown below
mul()
{
get_data();
c=b*get_A();
}
display()
{
show_A(); // outputs value of ‘a’
cout << “b=” << b << “\n” << “c=” << c << “\n\n”;
}
|
program 8.2 incorporates these modification for private derivation .compare this with program 8.1
single Inheritance : PRIVATE
< iostream >
using namespace std;
class B
{
a; // private so not inherit
:
b; // public ready to inherit
get_data();
get_A();
show_A();
};
class D: private B // this is private derivation
{
c;
:
mul();
display();
};
B :: get_data()
{
cout << “Enter values for a and b:”;
cin >> a >> b;
}
B :: get_A()
{
a;
}
B :: show_A()
{
cout << “a=” << a << “\n”;
}
D:: mul()
{
get_data();
c=b*get_A(); // ‘a’ cannot used directly
}
D ::display()
{
show_A(); // outputs value of ‘a’
cout << “b=” << b << “\n” << “c=” << c << “\n\n”;
}
main()
{
D d;
// d.get_data(); //will not work
d.mul();
// d.show_A(); //will not work
d.display();
// d.b=20; // will not work because b has become private
d.mul();
d.display();
0;
}
|
Suppose A base class and a derived class define a function of the same name. what will happen when a derived class object invokes the function?. in such cases the derived class function supersedes the base class definition. The base class function. Will be called only if the derived class does not redefine the function.