Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Unit 6 Virtual Function, Polymorphism, and Other C++ Features

BIT course

Uploaded by

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

Unit 6 Virtual Function, Polymorphism, and Other C++ Features

BIT course

Uploaded by

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

Unit 6

Virtual Function, Polymorphism, and other C++ Features

Polymorphism
In C++, polymorphism causes a member function to behave differently based on the object
that calls/invokes it. Polymorphism is a Greek word that means to have many forms. It occurs
when you have a hierarchy of classes related through inheritance.

Types of Polymorphism
C++ supports two types of polymorphism:
1. Compile-time polymorphism, and
2. Runtime polymorphism

Fig: Types of polymorphism

Base Class Pointer


- Pointer is a data type that stores the address of other data types.
- The pointer of Base Class can point object of derived class.

1|Page Prepared By: Ramesh Kumar Chaudhary


Example:
// C++ program to illustrate the
// implementation of the base class
// pointer pointing to derived class
#include <iostream>
using namespace std;

// Base Class
class BaseClass {
public:
int var_base;

// Function to display the base


// class members
void display()
{
cout << "Displaying Base class"
<< " variable var_base: " << var_base << endl;
}
};

// Class derived from the Base Class


class DerivedClass : public BaseClass {
public:
int var_derived;

// Function to display the base


// and derived class members
void display()
{
cout << "Displaying Base class"
<< "variable var_base: " << var_base << endl;
cout << "Displaying Derived "
<< " class variable var_derived: "
<< var_derived << endl;
}
};

2|Page Prepared By: Ramesh Kumar Chaudhary


// Driver Code
int main()
{
// Pointer to base class
BaseClass* base_class_pointer;
BaseClass obj_base;
DerivedClass obj_derived;

// Pointing to derived class


base_class_pointer = &obj_derived;

base_class_pointer->var_base = 34;

// Calling base class member function


base_class_pointer->display();

base_class_pointer->var_base = 3400;
base_class_pointer->display();

DerivedClass* derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 9448;
derived_class_pointer->var_derived = 98;
derived_class_pointer->display();

return 0;
}

Output:
Displaying Base class variable var_base: 34
Displaying Base class variable var_base: 3400
Displaying Base classvariable var_base: 9448
Displaying Derived class variable var_derived: 98

Virtual Function
- A C++ virtual function is a member function in the base class that you redefine 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.
- There is a necessity to use the single pointer to refer to all the objects of the different
classes. So, we create the pointer to the base class that refers to all the derived objects.
But, when base class pointer contains the address of the derived class object, 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.

3|Page Prepared By: Ramesh Kumar Chaudhary


Late binding or Dynamic linkage
In late binding function call is resolved during runtime. Therefore compiler determines the
type of object at runtime, and then binds the function call.

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

Example:
#include <iostream>
using namespace std;

class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};

class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};

int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}

4|Page Prepared By: Ramesh Kumar Chaudhary


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 can be defined as:
virtual void display() = 0;

Example:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
std::cout << "Derived class is derived from the base
class." << std::endl;
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}

Output:
Derived class is derived from the base class.

5|Page Prepared By: Ramesh Kumar Chaudhary


Abstract Class
- An abstract class is a class in C++ which have at least one pure virtual function.
- Abstract class can have normal functions and variables along with a pure virtual function.
- Abstract class cannot be instantiated, but pointers and references of Abstract class type
can be created.
- Abstract classes are mainly used for Upcasting, so that its derived classes can use its
interface.
- If an Abstract Class has derived class, they must implement all pure virtual functions, or
else they will become Abstract too.
- Abstract classes cannot be used to instantiate objects and serves only as an interface.
Example:
#include<iostream>
using namespace std;
class Base {
public:
virtual void show() = 0; // Pure Virtual Function
};

class Derived:public Base {


public:
void show() {
cout << "Virtual Function in Derived class\n";
}
};

int main() {
Base *b;
Derived dobj;
b = &dobj;
b->show();
}

Output:
Virtual Function in Derived class

Concrete Classes
Classes that can be used to instantiate objects are called concrete classes.

Virtual Destructor
Deleting a derived class object using a pointer of base class type that has a non-virtual
destructor results in undefined behaviour. To correct this situation, the base class should be
defined with a virtual destructor.
For example, following program results in undefined behaviour.

6|Page Prepared By: Ramesh Kumar Chaudhary


Example:
Without the use of virtual destructor.
// CPP program without virtual destructor
// causing undefined behavior
#include <iostream>

using namespace std;

class base {
public:
base()
{ cout << "Constructing base\n"; }
~base()
{ cout<< "Destructing base\n"; }
};

class derived: public base {


public:
derived()
{ cout << "Constructing derived\n"; }
~derived()
{ cout << "Destructing derived\n"; }
};

int main()
{
derived *d = new derived();
base *b = d;
delete b;
getchar();
return 0;
}

Output:
Constructing base
Constructing derived
Destructing base

Example:
Use of virtual destructor
// A program with virtual destructor
#include <iostream>

using namespace std;

class base {
public:
base()
{ cout << "Constructing base\n"; }

7|Page Prepared By: Ramesh Kumar Chaudhary


virtual ~base()
{ cout << "Destructing base\n"; }
};

class derived : public base {


public:
derived()
{ cout << "Constructing derived\n"; }
virtual ~derived()
{ cout << "Destructing derived\n"; }
};

int main()
{
derived *d = new derived();
base *b = d;
delete b;
getchar();
return 0;
}

Output:
Constructing base
Constructing derived
Destructing derived
Destructing base

Difference between Runtime and compile time polymorphism


Compile-Time Polymorphism Run-Time Polymorphism
Invoking of The function is invoked at the The function is invoked at the run
Function compile time. time.
Common It is known as overloading, early It is known as overriding, late binding,
Terms binding, and static binding. and dynamic binding.
Method Name In Overloading, more than one In Overriding, more than one method
and method has the same name but has the same name, number, and
Parameters with a different number or type of type of parameters.
parameters.
Carriers It is achieved with function and It is achieved with virtual functions
operator overloading. and pointers.
Execution Time It executes faster than run-time It executes slower than compile-time
polymorphism at the compile time. polymorphism at the run time.
Flexibility It is less flexible as everything It is more flexible as everything
executes at the compile time. executes at the run time.

8|Page Prepared By: Ramesh Kumar Chaudhary

You might also like