Inheritance in C++
Inheritance in C++
Inheritance in C++
OOP Feature - New classes are created from the existing classes
Dr. K. Veningston
Department of Computer Science and Engineering
National Institute of Technology Srinagar
veningstonk@nitsri.ac.in
Inheritance
• When we create a class D, deriving the features of the class B
in class D is called inheritance.
PERSON
EMPLOYEE
MANAGER
Properties: name,
Properties: empID,
UID, DoB, height,
General Properties
specialized class
Example 2 BIKE
Behaviours: start(),
accelerate(), decelerate()
Derived Classes
• A derived class is defined as the class derived from the base class.
– Exploit the properties of the base class
• Syntax: How to make a derived class?
class derived_class_name : visibility-mode base_class_name {
//body of the derived class
}
– Visibility mode: The visibility mode specifies whether the features of the base
class are publicly inherited or privately inherited. It can be public or private or
protected.
– We cannot access all the attributes and methods present in the base class.
– Visibility mode decides what attributes and methods are inherited and what are
not.
Visibility-modes
public, protected, and private inheritance
• Public inheritance makes public members of the base class
public in the derived class, and the protected members of the
base class remain protected in the derived class.
– NOTE: Private attributes and methods cannot be inherited as they
cannot be accessed outside the class.
• Protected inheritance makes the public and protected
members of the base class protected in the derived class.
• Private inheritance makes the public and protected members
of the base class private in the derived class.
Access specifiers - Summary
✓
❌
protected (acts like private)
Visibility modes - Summary
public (least constraint) < protected < private (more constraint)
Access specifier of Derived class
Access specifier of Base Visibility members
class members mode Security(derived class members) = max(access
specifier, visibility mode)
private ❌ (inaccessible)
public (least constraint) public public
protected protected
private ❌ (inaccessible)
public (least constraint) protected protected
protected protected
private ❌ (inaccessible)
public (least constraint) private private
protected private
Visibility of Inherited members
class Base
class Derived
(i) Single Inheritance
• We can inherit methods as well.
class Base
class Derived
class Derived2:Derived1
(ii) Multi-level Inheritance
class Base
class Derived2:Derived1
(iii) Multiple Inheritance
• Multiple inheritance is the process of deriving a new class that
inherits the attributes from two or more classes.
• Syntax: class Base1 class Base2
class derived : visibility base1, visibility base2, …{
//body of the derived class
} class Derived: Base1, Base 2
class E : B class F : B
(v) Hybrid Inheritance
• Hybrid inheritance is a class A
combination of more than one
type of inheritance.
class B : A class C : A
• Dirty Inheritance
class D : B class E : B, C
class F : D, E
Topics to be Covered
• Compile-time Polymorphism
– Operator overloading
• Virtual class, virtual methods, Abstract classes
• Run-time Polymorphism
• Real-life Project using OOP principles.