0% found this document useful (0 votes)
34 views19 pages

CH - 9-Runtime Polymorphism

Virtual functions allow for runtime polymorphism in C++. When a base class pointer points to a derived class object, calling a virtual function will execute the overridden version of that function in the derived class. Virtual functions are resolved at runtime based on the actual object type, rather than the pointer type. Pointers are used to achieve dynamic binding with virtual functions, allowing a base class pointer to call different implementations based on which derived class it is actually pointing to.

Uploaded by

Dhruv Rawal
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)
34 views19 pages

CH - 9-Runtime Polymorphism

Virtual functions allow for runtime polymorphism in C++. When a base class pointer points to a derived class object, calling a virtual function will execute the overridden version of that function in the derived class. Virtual functions are resolved at runtime based on the actual object type, rather than the pointer type. Pointers are used to achieve dynamic binding with virtual functions, allowing a base class pointer to call different implementations based on which derived class it is actually pointing to.

Uploaded by

Dhruv Rawal
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/ 19

Runtime Polymorphism by

Virtual Functions
Introduction
• Polymorphism : It is one of the crucial features of
OOP. It simply means ‘one name, multiple forms’.
• The overloaded member functions are selected for
invoking by matching the arguments, both type
and number.
• The information is known to the compiler at
compile time.
• This is called early binding or static binding or
static linking and polymorphism.
• When two functions with the same name are used in
two different classes, they can be defined using class
resolution operator.
• Here the function is not overloaded and so static
binding does not apply.
• If we want a member function could be selected while
the program is running , then we have to use virtual
function.
• This is known as run time polymorphism.
• // Virtual Functions and // Run-time Polymorphism
• #include <iostream.h>
▫ // base class
class base
{ public: int a; };
▫ // derived class
class derived:public base
{ public: int b; };
▫ // main
void main()
{ base b; derived d;
▫ // base class pointer
base *bptr;
// pointer pointing to base's object
bptr=&b;
bptr->a=10;
// pointer pointing to derived's object
bptr=&d; // still is able to access the members of the base class
bptr->a=100; }
• // Using Virtual functions to achieve run-time Polymorphism
• #include <iostream.h>
// base class
class base
{
public:
virtual void func()
{ cout<<"Base's func()\n"; }
};
// derived class
class derived:public base
{
public:
void func()
{
cout<<"Derived's func()\n"; }
};
• // main
void main()
{
int ch=0;
base b;
derived d;
// base class pointer
base *bptr;
while(ch!=3)
{
cout<<"1> Call Base's func\n";
cout<<"2> Call Derived's func\n";
cout<<"3> Quit\n"; cin>>ch;
switch(ch)
{
case 1: // point to base's object
bptr=&b; break;
case 2: // point tp derived's object
bptr=&d; break;
default: bptr=&b;
} // call whichever function // user has chosen to call
bptr->func(); } }
Pointers
• To achieve the dynamic binding, it is required to use the
pointer to objects and virtual functions.
• Pointer is a derived data type that refers to another data
variable by storing the variable’s memory address rather
than the data.
• Like C, a pointer variable can also be used to refer another
pointer in c++.
• Pointers provide an alternative approach to access other
data objects.
Pointer to Object
• Normal Objects cannot help in achieving runtime
polymorphism. It is possible to achieve it only by setting
or defining pointer to objects,
• Pointer to object is a variable containing an address of an
object.
• It is similar to a pointer to any other variable. We can use
normal address-of operator to get the address of an
object.
• We can define a pointer to an object and can assign it the
address of an object.
Declaring Pointers
• The declaration of a pointer is based on the data type of
the variables it points to.
• Syntax : - datat-type *pointerVariable;
• A pointer is able to point to only one data type at a specific
time.
• Eg :- int *ptr; // declare
ptr = &a; // initialize

Class Demo{ ----}


Demo objDemo;
Demo *ptrObjDemo;
ptrObjDemo = &objDemo;
this pointer
• The this pointer is a pointer that represent an object that
invokes a member function.
• This is a pointer that points to an object for which this
function was called.
• This pointer is automatically passed to a member
function when it is called.
• This pointer acts as an implicit argument to all the
member functions.
• Eg: objDemo.Display() function call, the value of this
pointer contains the address of objDemo.
• this pointer is poiner to objDemo.
Virtual Functions
• Virtual functions are special.
• Using virtual functions we are able to point to any object
of a derived class using a base pointer and can
manipulate that object.
• The class has an additional storage requirement when at
least one virtual function is defined.
• A table is created additionally to store pointers to all
virtual functions available to all objects of class.
• This is known to a virtual table.
• Syntax Constraints on Virtual functions:
• 1) Function should precede virtual keyword in the base
class
•.
2)The function name in derived class must have same
name as of virtual function defined in base class and
same prototype
3) The function in derived class need not to be preceded by
virtual keyword.
4) If function is not defined with same name then base
class function will be called.
5) The virtual function must be defined in the base class, it
may have an empty body though.
6) Polymorphism is achieved (executing the function of the
object which is pointed to) only using pointers to the
base class. It is not possible using objects.
7) Virtual Constructors are not possible.
Virtual Destructors
• Virtual destructors are needed for proper deletion of
objects of derived class, when pointed to by a base class
pointer.
• If do not define virtual destructors, only the base class
subobject is deleted, and the remaining portion of the
derived class object is not deleted.
• We know destructor name cannot be same in base and
derived class as it should same as class name.
• A class can have only one destructor and the derived class
destructor is the function called when delete is invoked
with a base class pointer.
Pure Virtual Functions
• When a class does not have any object, there is no need to
have functions for it as there is no object to utilize those
functions.
• When we write “=0” in place of the function body after
function header, the function is said to be pure virtual
function.
• In this function need not have any body.
• So instead of defining the function with any empty body,
if we define it as pure virtual function, the function forces
the base class to be abstract and it has to be derived.
• And abstract classes do not have object.
Static Invocation of virtual Functions

• Virtual functions can also be invoked statically.


• When we use a class name :: ptr-> virtual
function mechanism, they are called statically.
Pointers expressions and Arithmetic
• C++ allows pointers to perform the following arithmetic
operations:
▫ A pointer can be incremented (++) or decremented (--).
▫ Any integer can be added to or subtracted from a
pointer
▫ One pointer can be subtracted from another.

Eg: int a[10];


int *aptr;
aptr = &a[0];
We cn do aptr++ or aptr– to increment or decrement a
pointer and it moves to the next memory address.
Pointers with Arrays and Strings
• Accessing an array with pointers is simpler than accessing
the array index.
• Arrays refer to a block of memory space, but pointers do
not refer to any section of memory.
• The memory addresses of arrays cannot be changed,
whereas the content of the pointer variables, such as the
memory addresses that it refer to can be changed.
• Eg :- Pointer to array

int *nptr;
nptr = number[0];
Nptr points to the 1st element of an array.
Array of pointers
• An array of pointers represent the collection of
addresses.
• An array of pointers point to an array of data
items.
• Each element of the pointer array points to an
item of the data array.
• Data items can be accessed either directly or by
dereferencing the elements of pointer array
Pointers to Functions
• The pointer to function is known as callback function. We
can use these function pointers to refer to a function.
• Using function pointers, we can allow a c++ program to
select a function dynamically at run time.
• We can also pass a function as an argument to another
function (as pointer).
• There are two types of function pointers, function that
points to static member functions and function pointers
that point to non-static member functions
• For non-static member function requires hidden
argument.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy