Lecture 15
Lecture 15
Lecture No 15
1
Previous Lecture
• Function overriding
– Example program
– Stack class – stack2 class
• push() and pop()
– Distance class – Distsign class
• getdist() and setdist()
• Class hierarchy
– Employee program
• Employee class
• Scientist class
• Manager class
• Laberor class 2
Today’s Lecture
• Abstract class
• Public and private Inheritance
– Example program
• Level of inheritance
3
Abstract base class
• Classes used only for deriving other classes, as
employee, are sometimes loosely called
abstract classes, meaning that no actual
instances (objects) of this class are created
4
public and private Inheritance
• Class manager : public employee
• The keyword public specifies that objects of
the derived class are able to access public
member functions of the base class
• When this keyword is not used, objects of the
derived class cannot access public member
functions of the base class
• Result is that no member of the base class is
accessible to objects of the derived class
5
Cont.
• public inheritance represents is a relationship
Type of inheritance
Base class : Public Protected Private
access inheritance inheritance inheritance
specifier
int z; z = objC.privdataA;
B objB; z = objC.protdataA;
z = objB.privdataA; z = objC.pubdataA;
z = objB.protdataA;
z = objB.pubdataA; Go to program 8
Cont.
• Derived class
• Functions can access protected and public member
in base class (in case of public, protected and
private inheritance)
• Objects cannot access private or protected
members of the base class (in case of public
inheritance)
• Objects cannot access public, private or protected
member of base class (in case of private or
protected inheritance)
9
Access Specifiers: When to Use What
• In most cases a derived class exists to offer an
improved—or a more specialized—version of the
base class
• We’ve seen examples of such derived classes
CountDn class that adds the decrement operator
to the Counter class and the manager class that is
a more specialized version of the employee class
• In such cases it makes sense for objects of the
derived class to access the public functions of the
base class if they want to perform a basic
operation
10
Cont.
• The derived class is created as a way of
completely modifying the operation of the base
class, hiding or disguising its original interface
• Examples
– Array class that acts like an array but provides
protection against out-of-bounds array indexes
– Objects of Stack2 should always be treated as if
they were stacks, using push() and pop()
• In this situation, private inheritance would
allow you to hide all the Array class functions
from objects of the derived Stack2 class
11
Levels of Inheritance
• A class can be derived from a class that are
themselves derived
• B is derived from A, and C is class A
derived from B. The process can { };
class B : public A
be extended to an arbitrary number { };
of levels—D could be derived from C, class C : public B
{ };
and so on
• As a more concrete example, suppose that we
decided to add a special kind of laborer called a
foreman to the EMPLOY program
12
UML class diagram – EMPLOY program
class A {
};
class B {
};
class C : public A, public B{
};
15
Member Functions in Multiple Inheritance
• Suppose
– we need to record the educational experience of some of the
employees in the EMPLOY program
– In a different project, we’ve already developed a class called
student that models students with different educational
backgrounds
• Instead of modifying the employee class to incorporate
educational data, we will add this data by multiple
inheritance from the student class
• The student class stores the name of the school or
university last attended and the highest degree received
• Educational information is not relevant to every class of
employee
16
17
Miniprogram showing relationships
class student
{ };
class employee
{ };
class manager : private employee, private student
{ };
class scientist : private employee, private student
{ };
class laborer : public employee
{ };
18
19