0% found this document useful (0 votes)
0 views

Chapter_5_Polymorphism

This document discusses the concept of the 'this' pointer in C++ and its applications, such as returning objects and method chaining. It also covers runtime polymorphism, function overriding, and virtual functions, explaining their significance in achieving dynamic binding. Additionally, it introduces pure virtual functions and abstract classes, emphasizing their role in defining interfaces for derived classes.

Uploaded by

pandeynisan222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter_5_Polymorphism

This document discusses the concept of the 'this' pointer in C++ and its applications, such as returning objects and method chaining. It also covers runtime polymorphism, function overriding, and virtual functions, explaining their significance in achieving dynamic binding. Additionally, it introduces pure virtual functions and abstract classes, emphasizing their role in defining interfaces for derived classes.

Uploaded by

pandeynisan222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming

in C++
(CMP 115)
Chapter-5: Polymorphism
Er. Nirmal Thapa
Email: [email protected]
Lumbini Engineering College
Pokhara University
This pointer
• To understand ‘this’ pointer, it is important to know how objects look at functions and data
members of a class.
• Each object gets its own copy of the data member.
• All-access the same function definition as present in the code segment

• The ‘this’ pointer is passed as a hidden argument to all member function calls and is available as
a local variable within the body of all non-static functions.

• this pointer is used to represent the address of an object inside a member function.

• this is a keyword that refers to the current instance of the class.


This pointer
• For example, consider an object obj calling one of its member function say method() as
obj.method().
• Then, this pointer will hold the address of object obj inside the member function method().
• The this pointer acts as an implicit argument to all the member functions
class ClassName {
private:
int dataMember;
public:
method(int a) {
// this pointer stores the address of object obj and access dataMember
this->dataMember = a;
... .. ...
}
}
int main() {
ClassName obj;
obj.method(5);
... .. ...
}
This pointer
Applications of this pointer:
1. Return Object
 One of the important applications of using this pointer is to return the object it points.
For example, the statement
return *this;
inside a member function will return the object that calls the function.
2. Method Chaining
 After returning the object from a function, a very useful application would be to chain the methods for ease and
a cleaner code.
For example, positionObj->setX(15)->setY(15)->setZ(15);
 Here, the methods setX, setY, setZ are chained to the object, positionObj.
 This is possible because each method return *this pointer.
 This is equivalent to
positionObj->setX(15);
positionObj->setY(15);
positionObj->setZ(15);
Runtime Polymorphism
Runtime Polymorphism
 Run time polymorphism is achieved when the object's
method is invoked at the run time instead of compile time.

 It is achieved by method overriding.

 which is also known as dynamic binding or late binding.


Function Overriding
 Function Overriding occurs when a derived class has a definition for one of the
member functions of the base class.
 That base function is said to be overridden.
Virtual Function
 A virtual function is a member function that is declared in the base class using
the keyword virtual and is re-defined (Overridden) in the derived class.

Some Key Points About Virtual Functions:


 Virtual functions are Dynamic in nature.
 They are defined by inserting the keyword “virtual” inside a base class
 They are always declared with a base class and overridden in a child class
 A virtual function is called during Runtime
Virtual Function

 Defining in a base class a virtual function, with another version in a derived class,
signals to the compiler that we don't want static linkage for this function.

 The function to be called at any given point in the program to be based on the kind of
object for which it is called.

 This sort of operation is referred to as dynamic linkage, or late binding.


Virtual Function
class A Class A
{ {
int x=5; public:
public: virtual void display()
void display() {
{ cout << "Base class is invoked"<<endl;
cout << "Value of x is : " << x;
} }
}; };
class B: public A class B:public A
{ {
int y = 10; public:
public: void display()
void display() {
{ cout << "Derived Class is invoked"<<endl;
cout << "Value of y is : " <<y; }
} };
};
int main() int main()
{
A *a; {
B b; A* a; //pointer of base class
a = &b; B b; //object of derived class
a->display(); a = &b;
return 0; a->display(); //Late Binding occurs
} return 0;
}
Output: Value of x is : 5 Output: Derived Class is invoked
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.
Pure Virtual Function
class Shape {
protected:
int width, height;
public:
Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
 The = 0 tells the compiler that the function has no body and above virtual function will
be called pure virtual function.
Pure Virtual Function
Abstract Class
 A class having at least one pure virtual function is called an abstract class
 No objects can be created for the abstract class
 But pointers to abstract class can be created for selecting the proper virtual
function

 The main objective of an abstract base class is


 To derive specific classes of the same kind and traits
 To create a base pointer required for achieving runtime polymorphism
Deferred Methods
 A deferred method (sometimes called abstract method and in C++ called a
pure virtual method) is a virtual function without a body
 Very often the virtual functions of the base classes are not used
 Here, the behavior described by the method in a parent class is NOT
modified by the child class as the behavior of a parent class is NULL,
 only just a place holder and all useful activity is defined as part of the code
provided by the child classes
Questions ?
Thank you !

You might also like