Nothing Special   »   [go: up one dir, main page]

Inheritance in C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

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.

class B Base class/Parent class/Super class

class D Derived class/Child class/Sub class

• This feature is supported by all the OOP languages.


Inheritance in C++
• In C++, inheritance is a process in which one object acquires all the
properties and behaviours of its parent object automatically.
• In such a way, you can reuse, extend or modify the attributes and
behaviours which are defined in other class.
– We need not inherit all the attributes and behaviours. We can suppress
some of them.
• In C++, the class which inherits the members of another class is
called derived class and the class whose members are inherited is
called base class.
– The derived class (child class) is the specialized class for the base class
(parent class).
Example 1

PERSON
EMPLOYEE
MANAGER
Properties: name,
Properties: empID,
UID, DoB, height,
General Properties

department, Properties: DeptID


weight,
designation,
bloodGroup,BP,
off_email, salary Behaviours:
bodyTemp,
attendanceReport(),
qualification,
Behaviours: performanceAppraisal(),
parentage, 10th
getSalary(),
mark, 12th mark,
#projects()
email, address, etc.

specialized class
Example 2 BIKE

specialized versions of Vehicle


Properties: # gears,
• Any object of class price,

CAR can access VEHICLE


Behaviours: start(),
maxSpeed()
attributes and
Properties: # tyres,
methods inside seatingCapacity,
color, weight, CAR
class CAR, as well as mileage
the attributes and Behaviours:
Properties: # gears, price,
legRoom,
methods in class travel() groundClearance,
bootSpace, # airBags,
VEHICLE. type

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

Access specifier of Accessed


Class Inherited
Class members outside the class
private ❌ ❌

In Base class public ✓ ✓



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

Base class Derived class visibility


access
modifier public protected private

public public protected private

protected protected protected private

private ❌(not inherited) ❌ (not inherited) ❌ (not inherited)


Example (Access specifier vs. Visibility mode)
class A { class B : protected A {
public: //a becomes protected here.
int a; …
… }
}

(ii) Multi-level inheritance

class D : private/protected/public C { class C : private B {


//a becomes inaccessible here. //a becomes private here.
//methods here cannot see any attribute named a …
} }
Objects created of class C outside class C
cannot access the attribute a.
Types of Inheritance
• Single inheritance
• Multi-level inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
(i) Single Inheritance
• Single inheritance is defined as the inheritance in which a
derived class is inherited from the only one base class.

class Base

class Derived
(i) Single Inheritance
• We can inherit methods as well.

class Base

class Derived

d1.mul(4,5); will result in Error.


(ii) Multi-level Inheritance
• Multilevel inheritance is a process of deriving a class from
another derived class.
• Inheritance is transitive, so the last derived class acquires all
the members of all its base classes.
class Base

class Derived1: Base

class Derived2:Derived1
(ii) Multi-level Inheritance

class Base

class Derived1: 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

• What if both Base1 and Base2 has a member int a?


– Both int a will be inherited in the derived class. Besides this, derived
class can have its own int a.
– We have 3 copies of int a, and we can access all 3 of them.
(iii) Multiple Inheritance
• Ambiguity in Multiple Inheritance
– Ambiguity can be occurred in using the multiple inheritance when a
variable/function with the same name occurs in more than one base
classes.
(iii) Multiple Inheritance
• Compiler does not know
which display() to invoke
(iii) Multiple Inheritance
• Ambiguity
Resolution in
Multiple
Inheritance
– Compiler cannot
resolve this
ambiguity.
– When there is
ambiguity, we have
to resolve the scope
for the compiler.
(iii) Multiple Inheritance
• Ambiguity
Resolution in
Multiple
Inheritance
– Compiler cannot
resolve this
ambiguity.
– When there is
ambiguity, we have
to resolve the
scope for the
compiler.
(iv) Hierarchical Inheritance
• More than one sub-class inherits the property of a single base
class.
class A

class B : A class C : A class D : A

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.

You might also like