Questions: 1. State the rules for virtual functions. Write a C++ program to declare a virtual function and demonstrate it. 2. Explain run time polymorphism with example program in C++. 3. Explain about implementation of runtime polymorphism in C++ with an example.
Virtual
Functions
Definition : "A virtual function is a
member function that is declared within a base class and redefined by a derived
class."
•
The virtual keyword is preceded to
the function name. The virtual function can be redefined in the derived class.
Thus using virtual functions we can have one
interface, multiple functions performing different task. This feature is called polymorphism. The virtual function within the base class defines
the form of the interface to that function. Each redefinition of the virtual
function by a derived class indicate some different task related to derived
class.
•
When a virtual function is redefined by a derived class, the keyword virtual is
not needed. The virtual function written in base class acts as interface and the function defined in
derived classes act as different forms of the same function.
This
property of virtual function brings the runtime
polymorphism. The following program shows this property ‒
#include <iostream>
using namespace std;
class base
{
public:
int i;
base(int
x)//constructor
{
i = x;
}
virtual
void function() // The function is with keyword virtual
{
cout
<< "Using base version of function():";
cout
<< i << "\n";
}
};
class derived1: public base
{
public:
//constructor
for object creation
derived1
(int x):base(x){}
void
function()
{
cout<<"Using
derived1's version of function(): ";
cout<i+i
<< "\n";
}
};
class derived2: public base
{
public:
derived2(int
x):base(x){ }//constructor for object creation
void
function()
{
cout
<< "Using derived2's version of function():
cout
<< i*i << "\n";
}
}:
int main()
{
base *P;
int num
cout<<"\n
Enter Some number "
cin>>num;
base
obj(num);
derived1
d1_obj(num);
derived2
d2_ obj(num);
p=&obj;://base
object's address
p‒>function();
//base class function
p =
&d1_obj://derived1 object's address
p‒>function();
//derived1 class function()
p =
&d2_obj;//derived2 object's address.
p‒>function();
//derived2 class function().
return 0;
}
Output
Enter Some number 5
Using base version of
function(): 5
Using derived1's
version of function(): 10
Using derived2's
version of function(): 25
•
In above program we have written one interface function in base class preceded
by the keyword virtual. Note that the
derived class derived1 is also having
function who is performing addition
operation. This time this function is not preceded by keyword virtual. Similarly we have written
another derived class derived2 in
which function is performing the task of multiplication.
•
Using the object pointer we can access different forms of the functions.
Write a C++ program to
create a base class house. There are two classes called door and window
available. The house class has members which provide information related to the
area of construction, doors and windows detail. It delegates the responsibility
of computing cost of doors and window construction to door and window classes
respectively. Write a C++ program to model the above relationship and find the
cost of constructing the house.
Solution :
#include<iostream>
using namespace std;
class House
{
public:
int area;
double rate;
int
noofdoors;
int
noofwindows;
void
GetVal()
{
cout<<"\n
Enter the area for construction(in sq.feet): ";
cin>>
area;
cout<<"\n
Enter the rate per sq.feet: ";
cin>>rate;
cout<<"\n
Enter the number of dooors: ";
cin>>noofdoors;
cout<<"\n
Enter the number of windows: ";
cin>>noofwindows;
}
double AreaCost()
{
return
(area*rate);
}
};
class door:virtual public House.
{
public:
double
costofdoor;
void
GetDoorVal()
{
cout<<"\n
Enter the cost of each door: ";
cin>>costofdoor;
}
double
DoorCost()
{
return
(costofdoor*noofdoors);
}
};
class window:virtual public House
{
public:
double costofwindow;
void GetWindowVal()
{
cout<<"\n
Enter the cost of each window: ";
cin>>costofwindow;
}
double WindowCost()
{
return
(costofwindow*noofwindows);
}
};
class derived : public door,public window
{
public:
double ConstructionCost()
{
double
total;
double
c1=WindowCost();
double
c2=DoorCost();
double c3=
AreaCost();
total=c1+c2+c3;
return
total;
}
};
int main()
{
derived
obj;
obj.GetVal();
obj.GetDoorVal();
obj.GetWindowVal();
cout<<"\n
The construction cost is: "<<obj.ConstructionCost();
return 0;
}
Enter the area for
construction(in sq feet): 500
Enter the rate per sq
feet: 100
Enter the number of
dooors: 4
Enter the number of
windows: 2
Enter the cost of each
door: 40
Enter the cost of each
window: 20
The construction cost
is: 50200
Consider an example of
a book shop which sells book and video tapes. These two classes are inherited
from the base class called media. The media class has command data members such
as tile and publication. The book class has data members for storing number of
pages in book and tape class has the playing time in a tape. Each class will
have member function such as read() and show() in the base class, these members
have to be defined as virtual functions, write a program which models the class
hierarchy for the book shop and process the objects of these classes using
pointers to the base class.

Solution :
#include <iostream>
using namespace std;
class Media
{
public:
char
Title[10],publication[20];
virtual void Read()
{
cout<<"\n
Enter the title: ";
cin>>Title;
cout<<"\n
Enter the publication: ";
cin>>publication;
}
virtual void show()
{
cout<<"Title:
"<<Title<<endl;
cout<<"Publication:
"<<publication<<endl;
}
};
class Book: public Media
{
public:
int pages;
virtual void Read(int num)
{
pages
num;
}
virtual void show()
{
cout
<< "Number of pages of book: ";
cout
<< pages << "\n";
}
};
class Tape: public Media
{
public:
int playtime;
virtual void Read(int num)
{
playtime
num;
}
virtual void show()
{
cout
<< "Playing time in tape: "
cout
<< playtime << "\n";
}
};
int main()
{
Media *p;
Media
obj;
int num;
Book b;
Tape t;
obj.Read();
obj.show();
cout<<"\nEnter
number of pages: ";
cin>>num;
b.Read(num);
p=&b;
p‒>show();
cout<<"\nEnter
Time in minutes: ";
cin>>num;
t.Read(num);
p=&t;
p‒>show();
return 0;
}
Enter the title: AAA
Title: AAA
Enter the publication:
BBB
Publication: BBB
Enter number of pages:
100
Number of pages of
book: 100
Enter Time in minutes:
3
Playing time in tape:
3
Review Questions
1. State the rules for
virtual functions. Write a C++ program to declare a virtual function and demonstrate it.
2. Explain run time
polymorphism with example program in C++.
3. Explain about
implementation of runtime polymorphism in C++ with an example.
Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - Virtual Functions
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation