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

Virtual Function and Polymorphism

Here is a C++ program demonstrating the use of a pure virtual function with base and derived classes: #include <iostream> using namespace std; class Shape { public: virtual void draw() = 0; }; class Circle: public Shape { public: void draw() override { cout << "Drawing Circle" << endl; } }; class Rectangle: public Shape { public: void draw() override { cout << "Drawing Rectangle" << endl; } }; int main() { Circle c; Rectangle r; c.draw(); r.draw(); return 0; } The need

Uploaded by

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

Virtual Function and Polymorphism

Here is a C++ program demonstrating the use of a pure virtual function with base and derived classes: #include <iostream> using namespace std; class Shape { public: virtual void draw() = 0; }; class Circle: public Shape { public: void draw() override { cout << "Drawing Circle" << endl; } }; class Rectangle: public Shape { public: void draw() override { cout << "Drawing Rectangle" << endl; } }; int main() { Circle c; Rectangle r; c.draw(); r.draw(); return 0; } The need

Uploaded by

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

-------------------------------------------------------

Virtual Functions and Polymorphism


------------------------------------------------------

Polymorphism means one entity behave different way in different circumstance and it changes its
behavior depends on different situation.

There are two levels at which polymorphism can be achieved. They are

1. Compile time polymorphism

2. Run time polymorphism

1. Compile-time polymorphism

Compile time polymorphism is achieved through “Function overloading” and “Operator


overloading”. In function overloading, the member functions with the same name but having
different arguments helps the compiler to bind or link the appropriate function definition to a
specific function call during compile time itself. This type of binding is called static binding or
early binding. Hence polymorphism achieved through “function overloading” is called
compile time polymorphism.

2. Run-time polymorphism

Run time polymorphism is achieved through “virtual functions”. Here binding or linking of a
function definition to a specific function call is done during run time. Hence polymorphism
achieved through “virtual functions” is known as run-time polymorphism. Dynamic
binding requires the use of pointers to object.

Polymorphism

Compile-time Polymorphism Run-time Polymorphism

Function Overloading Operator Overloading Virtual Function


Pointers to derived class

a. Pointer of type derived class pointing to derived class objects:

class A
{
public:
void display() // overridden function
{
cout<<"Manjeet loves India";
}
};

class B: public A
{
public:
void display() // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};

main()
{
B b; //derived class object

B *bptr; //derived class pointer

bptr=&b; // Pointer of type derived class pointing to derived class objects

bptr->display(); //calls derived class display() function

return 0;
}

b. Pointer of type base class pointing to a derived class object:

class A
{
public:
void display() // overridden function
{
cout<<"Manjeet loves India";
}
};

class B: public A
{
public:
void display() // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};

main()
{
A a; //base class object

B b; //derived class object

A *aptr; //Base class pointer

aptr=&b; // base class Pointer pointing to derived class objects

aptr->display(); //calls base class display() function

return 0;
}

Here we notice that, the “display ()” function of the base class is called irrespective of the type of the
object pointed at by the pointer.

This is because the appropriate function to be called is decided by the compiler at the compile time, by
considering the type of pointer, but not the type of object pointed to by the pointer. Hence this is a
serious problem, which has to be taken care of.

There are two ways to overcome this problem. They are

I. Typecasting the pointers

II. Virtual Function

Example : Typecasting the pointers

#include<iostream>

using namespace std;

class A

public:

void display() // overridden function

cout<<"Manjeet loves India"<<endl;

};
class B: public A

public:

void display() // Overriding function

cout<<"Manjeet loves Massala Dosa";

};

main()

A a; //base class object

B b; //derived class object

A *aptr; //Base class pointer

aptr=&b; // base class Pointer pointing to derived class objects

aptr->display(); // calls base class display() function

((B*)aptr)->display(); //calls derived class display() function

return 0;

}
Virtual Function
● Virtual functions are the C++ language’s feature to achieve the run-time polymorphism.

● When we use the same function name in both the base and the derived classes, the function
in base class is declared as virtual using the keyword “virtual” prefixed with the function
declaration.

● When a function is made virtual, the decision as to which function to be invoked is done at the
run-time based on the type of the object pointed to by the pointer, rather than the type of the
pointer.

● The keyword virtual is required only for the declaration but not for the definition. Once a function
is declared as virtual in the base class, it is virtual in all the derived classes.

Syntax

<return-type> virtual <function-name>( <parameter_list> )

Or

virtual <return-type> <function-name>( <parameter_list> )

Eg:

#include<iostream>

using namespace std;

class A

public:

void virtual display() // virtual function

cout<<"Manjeet loves India"<<endl;

};
class B: public A

public:

void display() // Overriding function

cout<<"Manjeet loves Massala Dosa";

};

main()

B b; //derived class object

A *aptr; //Base class pointer

aptr=&b; // base class Pointer pointing to derived class objects

aptr->display(); // calls derived class “display()” function

return 0;

Rules for Virtual functions

● Virtual functions must be non-static members.

● Virtual functions are accessed by using pointers to objects.

● Constructor cannot be made virtual, but destructors can be made.

● A base class pointer can refer to the derived class objects, but the reverse is not true.

● Virtual functions should be declared in the public section of a class.


Pure Virtual Functions
Pure virtual functions are virtual function with no body/definition. Pure Virtual Function start with
“virtual” keyword and ends with =0.

Or

A virtual function that is declared but not defined in a base class is referred to as a pure virtual
function. The general syntax for declaring a pure virtual function is as follows:

<return-type> virtual <function-name>( )=0;

Or

virtual <return-type> <function-name>( )=0;

Note: here function is initialized to zero. The assignment operator ‘=’ has nothing to do with the
assignment i.e the value zero is not assigned to anything. It is used to simply inform the compiler that,
the function will be pure and does not have any body.

#include<iostream>
using namespace std;
class A
{
public:
void virtual display( )=0; // virtual function

};
class B: public A
{
public:
void display( ) // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};
main()
{
B b; //derived class object
A *aptr; //Base class pointer
aptr=&b; // base class Pointer pointing to derived class objects
aptr->display(); // calls derived class “display()” function
return 0;
}
“A virtual function which gives an interface for the derived class functions to override but must not
be invoked itself through virtual mechanism is called pure virtual function.”

Features of pure virtual Functions

● A pure virtual function is a “do-nothing” function.

● When the base class uses a pure virtual function each derived class must override that
function, failing of which a compile time error is generated.

● A class containing a “pure virtual function” is called Abstract class. We cannot create an
object of an Abstract class. We can however declare pointers to it.

● Abstract class is a design concept only to act as a base class upon which other derived classes
are set up.

● If a virtual function is declared as pure, it must be redefined in each derived class, else the
compilation of the program is unsuccessful.

Q: Write a C++ program demonstrating use of the pure virtual function with the use of base and
derived classes.

Q: What is the need of abstract class in C++?

You might also like