05 and 6 - Inheritance-A
05 and 6 - Inheritance-A
OBJECT ORIENTED
PROGRAMMING
Inheritance
2
Initializer list
• Initializer list is used to initialize data member(s) of the
class
• The list of members to be initialized is indicated with
constructor as a comma-separated list followed by a colon
Organization
Employee
7
Person
Inheritance
• Inheritance is an “is-a” relationship Employee
• Example: “every employee is a person”
• Person’s attributes: First Name, Last Name and CNIC No.
• Employee’s attributes: First Name, Last Name, CNIC No.,
Employee ID, Salary, Joining Date etc.
• Inheritance lets us create new classes from existing
classes
• New classes are called the derived classes/child classes
• Existing classes are called the base classes / parent classes
• Derived classes inherit the properties of the base classes
8
Examples
Base class Derived classes
• Circle
• Shape
• Rectangle
• Faculty Member
• Employee • Staff member
9
Inheritance (continued)
• Inheritance can be viewed as a tree-like, or hierarchical,
structure wherein a base class is shown with its derived
classes
10
Inheritance (continued)
• Single inheritance: derived class has a single base class
• Ex. Circle class from Shape class
• Mutlilevel Inheritance: where one can inherit from a derived class,
thereby making this derived class the base class for the new class.
• Ex. Son->Father->Grand Father
• Multiple inheritance: derived class has more than one base class
• Ex. Son class from Mother class and Father class
11
The left diagram illustrates a “single inheritance”, and the right one a
“multiple inheritance” or “multi-inheritance”.
We can also write about super classes as base classes, and subclasses
as derived classes.
12
101
13
Inheritance (continued)
• General syntax of a derived class:
class A : X, Y, Z
{
member list;
};
Let’s start with the simplest possible case.
15
Base
derived
22
Accessibility
• Accessibility:
• Private < Protected < Public
27
Inheritance (continued)
• public members of base class can be inherited as
public or private members