0% found this document useful (0 votes)
3 views23 pages

Inheritance

The document covers Object-Oriented Programming (OOP) fundamentals, focusing on inheritance and access modifiers such as public, private, and protected. It explains the relationship between access modifiers and visibility modes, detailing how they affect accessibility in derived classes. Additionally, it discusses various types of inheritance, function overriding, and the concept of abstract classes in OOP.

Uploaded by

harshvardhanagam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Inheritance

The document covers Object-Oriented Programming (OOP) fundamentals, focusing on inheritance and access modifiers such as public, private, and protected. It explains the relationship between access modifiers and visibility modes, detailing how they affect accessibility in derived classes. Additionally, it discusses various types of inheritance, function overriding, and the concept of abstract classes in OOP.

Uploaded by

harshvardhanagam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

FY- OOP-Programming for Problem Solving

Prof. Sarang Joshi


OOP Fundamentals: Inheritance(F015)
Module II
Relationship between access modifiers and
Visibility modes
⚫ Access Modifiers in OOP allow Data hiding
⚫ Three Access Modifiers: Public, Private, Protected
⚫ Public: The Data and function members declared
under Public access modifier can be assessed by the
class itself, Other classes and even functions from
anywhere in the program using Direct Access
Operator(or “.”) using class Objects.
⚫ class_Object.class_public_member_Data(or member
function)

Relationship between access modifiers and
Visibility modes
⚫ Private: These access specifiers allow access of
data/function members within the class. These are
not accessed by objects or functions outside the class
except friend classes or friend functions. But these
members can be accessed using public function
members of the class using class objects outside the
class.
⚫ Protected: These access specifiers allow access of
data/functions members within the class. These are
not accessed by objects or functions outside the class
except friend classes and derived classes.
Introduction to Inheritance

⚫ Its a key feature of Object Oriented Programming


⚫ Let A and B be two classes such that A is called a parent class or
base class and B be the child of A.
⚫ Then, The class A is also referred as being inherited from and a child
class is one which inherits from other or parent or base class and
also called as derived class.
⚫ Properties or members of base class A are accessible to child class
or derived class B. Syntax of Inheritance as following:
⚫ class derived_class_name : access_specifier base_class_name{
− body_of_the_class

⚫ }
⚫ example
Introduction to Inheritance
⚫ Inheritance is relation
between two classes
which has relation or
commonness.
⚫ The commonness can
have limited
variations or extreem
differences.
⚫ Example
Access Modifiers
⚫ Access Modifiers Keywords:
− private, protected and public
⚫ Private and Protected members are inaccessible
outside a class, BUT, can be accessible using
derived classes and friend classes/functions.
⚫ Protected members help in hiding the data but can
be accessed using derived classes.
⚫ Public are accessible from classes or from outside
of the class using class objects.
Relation between Access Modifiers and
Visibility Modes
⚫ Rules of Visibility in
inheritance
⚫ MyEagle: private MyBirds
⚫ Inherited class MyEagle
⚫ Base Class MyBirds
⚫ Access Specifier: private
⚫ Default mode means when no
accessibility mode is specified
⚫ Default mode is private
⚫ e.g. MyEagle: MyBirds
Rules of accessibility: visibility
⚫ Rule 1: Derived Class: (Default visibility ) Private Base Class
− Private variables are accessible only to the member functions of the given
base class and not accessible to the members of a derived class.
− Private and public members of the base class override as a private
members and can only be accessible to the members of the derived
class only.
− Such private visibility members are accessible within the class only, even if
an object created outside the class is not permitted to access these private
visibility members.
− Even When an object of a derived class accesses these private members
either error occurs or no changes of function operations are reflected.

⚫ Example (Students to give example)


Rules of accessibility: visibility
⚫ Rule 2: Derived Class: (visibility ) Public Base Class
The Public, Private and Protected members of the base class remains Public,
Private and Protected members of the derived class in Public visibility mode. All
members of the parent class remains accessible in the Public Visibility Mode.
The Child class and other classes can access Parents Public members Only, But
only the derived classes and its inherited classes can access the protected
members of the Base class.

Example Parent class Derived class


Private Not Accessible
Protected Accessible as Protected
Public Accessible as Public
Rules of accessibility: visibility
⚫ Rule 3: Derived class: (Visibility) Protected Base Class

⚫ All the members of base class become protected


members of the derived class, hence the members of the
derived class and its inheritance can access these
visibility members using protected access.
⚫ Example Base Class Derived Class
⚫ Private Not Accessible
⚫ Protected Protected
⚫ Public Protected

Access Specifier Summary[Ref:GreeksforGeeks]
Comparative Summary [Ref: javapoint.html]
Types of Inheritance
⚫ Simple or single Inheritance
− One Base class is allowed to be inherited from one derived class only. For example father
and child.
⚫ Multiple Inheritance
− Multiple Base classes and single inherited class. For example, Father class, Mother class
and Child class
⚫ Multi-Level Inheritance
− Class B is derived from class A and Class C is derived from class B, for example,
− Grand Father >> Father >> Child and so on, multi-levels
⚫ Hierarchical Inheritance
− For example, Binary Tree with root as base class, Say, classes A and C are derived from
base class B and classes D and F are derived from a base class, say, E, respectively and
classes C and E are derived from say, class G.
⚫ Hybrid Inheritance
− Combination of Hierarchical inheritance and multiple inheritance results in Hybrid
Passing parameters to Base Class constructor
⚫ Constructor is a special method having name of the
class which is invoked automatically when an
object of a class is created.
⚫ Constructors are usually declared as public bust
can also be private.
⚫ These do not return values hence it has no return
data type
⚫ If programmer do not write a constructor then
compiler adds a default constructor automatically.
Passing parameters to Base Class constructor
Constructor outside the class
⚫ Constructor inside the ⚫

− class <class_name> {
class ⚫ Class members;

− class <class_name> { − class_name(){


parameter initialization;
⚫ Class members; ⚫

⚫ };// End of Construction


− class_name(){ − }; // End of class
⚫ parameter initialization; ⚫ class_name::class_name(list of
⚫ };// End of Construction parameters){
− parameter initialization;
− }; // End of class
⚫ }
⚫ Example
Function Overriding
⚫ Morphism is creating self-similar either identical (mono-morphism)
or overriding with variations or different forms (iso-Morphism, Poly-
morphism). Function Overriding supports Morphism in C++.
⚫ Function overriding in C++ is redefinition of base class
functions(having same function name) in its derived class with
same return type and parameters( It is also called having same
signature).
⚫ When a base class function is called with from outside the class
(i.e. using class object) then base class function executes.
⚫ But when an object of derived class is used to call a function with
same name in derived class, function in derived class is executed.
⚫ Example
Function Overriding Advantages
⚫ Methods are selected at runtime based on object type
⚫ Common interfaces and implementations can be
reused using base class interfaces
⚫ Changes in base classes automatically affect derived
classes
⚫ It facilitates implementation of design patterns using
design pattern designs
⚫ Interaction with base class on client specific overriding
Function Overriding DisAdvantages
⚫ Errors related to overriding(signature specific errors)
can only be detected run-time, stressing testing efforts
⚫ Implementing and maintaining polymorphic code is bit
complex, time consuming as compare to simple non-
polymorphic code
⚫ Virtual tables and virtual pointers add to the memory
overhead in large system with many classes.
⚫ Virtual function calls are generally slower since
requirements of indirect references to virtual tables
Function Overload Vs Function Overriding
⚫ Overloading ⚫ Overriding
− Scope may be same − Scope may be different
− Can be executed − Can not be executed
without Inheritance without Inheritance
− Function can be − Function can not be
executed Multiple times overridden multiple
being resolved during times as being resolved
Compile time at run time
− Uses compile time − Uses compile time or
Polymorphism run time Polymorphism
Friend Class
⚫ Keyword friend
⚫ Private members are not accessible out side the class
as well, protected members are not accessible out
side the class except using derived class objects.
⚫ Friend function breaks this rule and provide access to
the private and protected members outside the class.
⚫ Example friend function between independent classes
⚫ Example A Friend Class
Nested Class
⚫ A class declared inside another class is called
nested class.
⚫ Nested class is just like other members and related
access permissions.

⚫ Example
⚫ Students to take example with Private, Protected
Members
Virtual Base Classes and Abstract Classes
⚫ Sometimes some functions are not provided in base class being unclear of
implementation, for example polygon, polygon can be of different shapes and varying
edges. So class having function polygon is an abstract class.
⚫ An abstract class is specifically designed to be a base class having atleast one pure virtual
function.
⚫ A pure virtual function is one which uses pure specifier (i.e. = 0) in a declaration of a pure
virtual function.

⚫ A pure virtual function can not have both i.e. pure specifier ( = 0) and function definition.
⚫ IMP ***: Its not possible to use an abstract class as a parameter type, a function return
type, or the type of explicit conversion and nor can it be declared as an object of an
abstract class.
⚫ IMP***: Abstract class can also be defined using struct keyword.

⚫ Hence, an abstract class must have atleast one pure virtual function.
⚫ Example
Virtual Base Classes and Abstract Classes
⚫ Advantage of Abstract class
− It allows to specify a set of methods that must
be implemented by any subclass
− It ensures consistent interface
− Re-usability
− Promotion of polymorphism
− Virtualization is important data hiding technique
⚫ Virtual debit card, Aadhar card etc.

You might also like