0% found this document useful (0 votes)
6 views53 pages

Unit-5 Part 2

The document provides a comprehensive overview of Object-Oriented System Design (OOSD) concepts, focusing on inheritance, polymorphism, and pointers in C++. It explains different types of inheritance, their advantages, and the significance of virtual functions and pointers in C++. Additionally, it covers function overloading and overriding, emphasizing their roles in achieving code reusability and runtime polymorphism.

Uploaded by

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

Unit-5 Part 2

The document provides a comprehensive overview of Object-Oriented System Design (OOSD) concepts, focusing on inheritance, polymorphism, and pointers in C++. It explains different types of inheritance, their advantages, and the significance of virtual functions and pointers in C++. Additionally, it covers function overloading and overriding, emphasizing their roles in achieving code reusability and runtime polymorphism.

Uploaded by

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

Technical Training

Mission 2023
OBJECT ORIENTED SYSTEM
DESIGN(OOSD)
BATCH 2019-2023
INDEX

Inheritance

Types of Inheritance

Polymorphism

Function Overloading

Function Overriding
INHERITANCE

 In C++, inheritance is a process in which one object


acquires all the properties and behaviors of its parent
object automatically.
 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.
 Sub Class: The class that inherits properties from another
class is called Sub class or Derived Class.
 Super Class: The class whose properties are inherited by
sub class is called Base Class or Super class.
WHY AND WHEN TO USE INHERITANCE

 Consider a group of vehicles. You need to create


classes for Bus, Car and Truck. The methods
fuelAmount(), capacity(), applyBrakes() will be
same for all of the three classes. If we create these
classes avoiding inheritance then we have to write
all of these functions in each of the three classes as
shown in figure:

 If we create a class Vehicle and write these three


functions in it and inherit the rest of the classes
from the vehicle class, then we can simply avoid the
duplication of data and increase re-usability.
ADVANTAGES OF INHERITANCE

 Code reusability: We can reuse the members of your parent class.


 Through inheritance a lot of time and efforts are being saved.
 It improves the program structure which can be readable.
 The program structure is short and concise which is more reliable.
 The codes are easy to debug.
INHERITANCE

Derived Classes
 A Derived class is defined as the class derived from the base class.
 The Syntax of Derived class:

 In C++, the default mode of visibility is private.


 The private members of the base class are never inherited.
VISIBILITY MODE

Visibility modes can be classified into three categories:


• Public: When the member is declared as public, it is accessible to all the functions of the program.
• Private: When the member is declared as private, it is accessible within the class only.
• Protected: When the member is declared as protected, it is accessible within its own class as well
as the class immediately derived from it.
VISIBILITY OF INHERITED MEMBERS
PROTECTED MEMBERS

 A protected member variable or


function is very similar to a private
member but it provides one
additional benefit that they can be
accessed in child classes which are
called derived classes.
 Private members cant be accessed
by derived class.
 For example:-
TYPES OF INHERITANCE

 C++ supports five types of inheritance:


SINGLE INHERITANCE
#include <iostream>
using namespace std;
// base class
 Single inheritance In single
class Vehicle {
inheritance, a class is allowed to public:
inherit from only one class. i.e. one Vehicle() {
sub class is inherited by one base cout << "This is a Vehicle" << endl;
class only } };

// sub class derived from two base classes


class Car: public Vehicle{ };

// main function
int main() {
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
SINGLE LEVEL INHERITANCE EXAMPLE: INHERITING FIELDS

OUTPUT
C++ SINGLE LEVEL INHERITANCE EXAMPLE: INHERITING
METHODS

Output
:
MULTILEVEL INHERITANCE

 Multilevel inheritance is a process of deriving a class from


another derived class.
 When one class inherits another class which is further
inherited by another class, it is known as multi level
inheritance.
 Inheritance is transitive so the last derived class acquires all
the members of all its base classes.
EXAMPLE OF MULTILEVEL INHERITANCE

Output:
MULTIPLE INHERITANCE

 Multiple inheritance is the process of


deriving a new class that inherits the
attributes from two or more classes.
 Syntax of the Derived class:
EXAMPLE OF MULTIPLE INHERITANCE

Output:
AMBIGUITY IN MULTIPLE INHERITANCE
 Ambiguity can be occurred in
using the multiple inheritance
when a function with the same
name occurs in more than one
base class.
 The most obvious problem with
multiple inheritance occurs
during function overriding.

 This problem can be solved


using the scope resolution
function to specify which
function to class
either base1or base2
HIERARCHICAL INHERITANCE

 Hierarchical inheritance is defined as


the process of deriving more than one
class from a base class.
EXAMPLE OF HIERARCHICAL INHERITANCE
HYBRID INHERITANCE

 Hybrid inheritance is a combination of more than one type of


inheritance.

 There could be situations where we need to apply two or more


types of inheritance to design a program.
 For example processing the student result.
EXAMPLE OF HYBRID INHERITANCE
EXAMPLE
VIRTUAL BASE CLASS
 The virtual base class is used when a
derived class has multiple copies of the Example: All the members of
base class. grandparent are inherited in child twice
 When all the three kinds of from two different paths
inheritance, namely multi-level,
multiple and hierarchical are involved.
This means that a child class could have
duplicate sets of members inherited from a
single base class.
 The duplication of the inherited
members can be avoided by making
common base class as the virtual base
class.
VIRTUAL BASE CLASS

 The duplication of inherited


members through multiple
paths can be avoided by
making common base class
as virtual base class.

Note: The keyword virtual and public can be used in


either order.
VIRTUAL BASE CLASS- EXAMPLE
POINTERS

 The pointer is a variable, it is also known as locator or indicator that points to an address of a value.

Advantage of pointer
 1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc.
and used with arrays, structures and functions.
 2) We can return multiple values from function using pointer.
 3) It allows to access any memory location in the computer's memory.

Symbols used in Pointer


POINTERS IN C++
 Pointer is a variable in C++ that holds the address of another variable.
 Pointers have data type just like variables, for example an integer type pointer can hold
the address of an integer variable and an character type pointer can hold the address of
char variable.
 Syntax of Pointer:

 Declaration of Pointer: ,
,

 Assigning Addresses to Pointers:


POINTERS IN C++

 To get the value pointed by a pointer, we use the


(*) operator.
 For example: The address of var is assigned
to pointVar. We have used the *pointVar to get the value
stored in that address.
 When (*) is used with pointers, it's called
the dereference operator. It operates on a pointer
and gives the value pointed by the address stored in
the pointer. That is, *pointVar=&var;
WORKING OF C++ POINTERS
Output:
POINTERS TO OBJECTS

 The pointers pointing to objects are referred to as Object Pointers.


 To access an element of an object when using the actual object itself, use the dot operator.
 To access a specific element of an object when using a pointer to the object, you must use
the arrow operator.
POINTERS TO OBJECTS

C++ Declaration and Use of Object Pointers

Example:
class employee {
int code;
char name [20] ;
public:
inline void getdata ( )= 0 ;
inline void display ( )= 0 ;
};
THIS POINTER

 unique keyword called "this" to represent an object that invokes a member function.
 'this' is a pointer that points to the object for which this function was called.
 This unique pointer is called and it passes to the member function automatically.
 The pointer this acts as an implicit argument to all the member function, for e.g.

class ABC {
int a ;
-----
-----
};
The private variable ‘a’ can be used directly inside a member function, like a=123;
We can also use the following statement to do the same job.
this → a = 123
THIS POINTER

 One important application of pointer this is to return the object it points to.
 For example: return *this. Inside member function it
will return an object that
 invoked the function
This statement is also important while comparing two or more objects inside a member
function and return invoking object as a result.
 Example:
THIS POINTER
class stud {
int a;
public: Output:
void set (int a) { 5
this → a = a; //here this point is used to
assign a class level ‘a’ with the argument
‘a’
}
void show ( ) {
cout << a;
}
};
main ( ) {
stud S1, S2;
S1.bet (5) ;
S2.show ( );
POINTERS TO DERIVED CLASSES
Example:

 Pointers can be used to objects of


derived classes.
 Pointers to objects of base class are
type compatible with pointers to
objects of derived class.
POINTERS TO DERIVED CLASSES

After Explicit type


casting:

 The pointer ptr points to an object of the


derived class obj. But, a pointer to a
derived class object may not point to a
base class object without explicit casting.
POLYMORPHISM
POLYMORPHISM

 The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms.
 Types of polymorphism:
DIFFERENCES B/W COMPILE TIME AND RUN TIME
POLYMORPHISM.
OVERLOADING (FUNCTION AND OPERATOR)

 If we create two or more members having the same name but different in number or type
of parameter, it is known as overloading. In C++, we can overload:
• methods,
• constructors, and
• indexed properties
Types of overloading:
• Function overloading
• Operator overloading
FUNCTION OVERLOADING

 Function Overloading is defined as the


process of having two or more function
with the same name, but different in
parameters is known as function
overloading.
 In function overloading, the function is
redefined by using either different types
of arguments or a different number of
arguments.
 It is only through these differences
compiler can differentiate between the
functions.
 The advantage of Function overloading
is that it increases the readability of the
program because you don't need to use
different names for the same action.
FUNCTION OVERLOADING WITH DIFFERENT TYPES OF
ARGUMENT

Output:
FUNCTION OVERRIDING

 If derived class defines same function


as defined in its base class, it is
known as function overriding.
 It is used to achieve runtime
polymorphism.
 It enables to provide specific
implementation of the function which
is already provided by its base class.

Output:
ACCESS OVERRIDDEN FUNCTION IN C++

 To access the overridden function


of the base class, we use the
scope resolution operator .
 Overridden function can also be
called by using a pointer of the
base class to point to an object of
the derived class and then calling
the function from that pointer.

 Example 1: Using scope


resolution operator
ACCESS OVERRIDDEN FUNCTION IN C++

 Example 2: Accessing
overridden function from
base class.
ACCESS OVERRIDDEN FUNCTION IN C++
Example 3: Calling overridden function using pointer

 Pointers to object of base class are


type compatible to pointers to
object of derived class.
 Single pointer variable can point to
objects of different classes.
 Base class pointer can access
members which are inherited from
base class and not members that
originally belong to derived class.
 If derived class and base class has
members of same name then any
refrence to base class pointer will
always access base class member.
VIRTUAL FUNCTION
 A virtual function is a member function in the base class that is redefined in a derived
class.
 It is declared using the virtual keyword.
 It is used to tell the compiler to perform dynamic linkage or late binding on the function.
 When base class pointer contains the address of the derived class object, it always
executes the base class function. This issue can only be resolved by using the 'virtual'
function.
• A 'virtual' is a keyword preceding the normal declaration of a function.
• When the function is made virtual, C++ determines which function is to be invoked at the
runtime based on the type of the object pointed by the base class pointer.
RULES OF VIRTUAL FUNCTION

• Virtual functions must be members of some class.


• Virtual functions cannot be static members.
• They are accessed through object pointers.
• They can be a friend of another class.
• A virtual function must be defined in the base class, even though it is not used.
• The prototypes of a virtual function of the base class and all the derived classes must be
identical. If the two functions with the same name but different prototypes, C++ will consider
them as the overloaded functions.
• We cannot have a virtual constructor, but we can have a virtual destructor
• If a virtual function is defined in base class, it need not be necessarily redefined in the derived
class. In such case calls will invoke the base function.
VIRTUAL FUNCTION
Output:
VIRTUAL FUNCTION
PURE VIRTUAL FUNCTION

• A virtual function is not used for performing any task. It only serves as a placeholder.
• When the function has no definition, such function is known as "do-nothing" function.
• The "do-nothing" function is known as a pure virtual function. A pure virtual function is
a function declared in the base class that has no definition relative to the base class.
• A class containing the pure virtual function cannot be used to declare the objects of its
own, such classes are known as abstract base classes.
• The main objective of the base class is to provide the traits to the derived classes and to
create the base pointer used for achieving the runtime polymorphism.
Syntax:
PURE VIRTUAL FUNCTION

Output:

You might also like