🔍
With suitable example, describe use of virtual function in polymorphism.
0 like 0 dislike

1 Answer

In order to achieve polymorphism, objects belonging to different classes should be able to respond to the same message at different instances which initiates the use of single pointer variable to refer to objects of different classes.  In case of inheritance, base class pointer is used to refer to all derived objects but even when it contains an address of derived class always executes base class function.  The Compiler simply ignores content of pointer & selects member function that matches type of pointer. In order to achieve polymorphism, the concept of virtual functions is used.  When we use same function name in both base & derived classes, function in base class is declared as virtual using keyword virtual preceding its normal declaration.  When function is made virtual C++ determines which function to be executed at runtime based on type of object pointed to by base pointer rather than type of pointer.  Thus by making use of single pointer variable (base pointer) to point to different objects so that different version of virtual function can be executed and hence polymorphism is achieved known as Run Time Polymorphism.

Example:

#include<conio.h>

#include<iostream.h>

class student

{

int roll;

char name[20];

public:

virtual void accept()

{

cout<<"\n Enter Roll id:";

cin>>roll;

cout<<"\n Enter name:";

cin>>name;

}

virtual void display()

{

cout<<"\n Roll id:"<<roll<<endl;

cout<<"\n Name:"<<name<<endl;

}

};

class Test: public student

{

int marks1,marks2;

public:

void accept()

{

student::accept();

cout<<"\n Enter marks 1:";

cin>>marks1;

cout<<"\n Enter marks 2:";

cin>>marks2;

}

void display()

{

student:: display();

cout<<"Marks1="<<marks1<<endl;

cout<<"Marks2="<<marks2<<endl;

}

};

void main()

{

student *p;

Test b;

p=&b;

clrscr();

p->accept();

p->display();

getch();

}
0 like 0 dislike

Please log in or register to answer this question.

Question ⇨⇦ Question

Related questions

Polymorphism is implemented using function overloading. Justify the statement.
Last Answer : Polymorphism is a mechanism that allows a developer to have more than one function with same name but different signature. In function overloading, one can make use of more than one function with different signature as well. Hence ... main()  { int a[10],i,isum; float b[5],fsum; clrscr(); cout...

Show Answer
0 like 0 dislike
1 answer

What is polymorphism? Enlist different types of polymorphism. What are the differences between them?
Last Answer : Polymorphism: It is a feature of object oriented programming which allows a programmer to have a more than one function having same name but different /same parameters but performs different/ ... Compile Time polymorphism o Virtual Function Difference between Types of Polymorphism: ...

Show Answer
0 like 0 dislike
1 answer

Compare run-time and compile-time polymorphism.
Last Answer : Compare run-time and compile-time polymorphism. ...

Show Answer
0 like 0 dislike
1 answer

How to achieve compile time polymorphism explain in detail.
Last Answer : The process of linking of function call to function definition at compile time is called as Compile Time Polymorphism. It can be through Function and operator overloading. Function overloading: The process of defining the ... \n Concatenated String is:"; str1.display(); getch(); }   ...

Show Answer
0 like 0 dislike
1 answer

Define polymorphism. Enlist its types.
Last Answer : Definition: Polymorphism means ability to take more than one form that means a program can have more than one function with same name but different behavior. Types of polymorphism: 1) Compile time polymorphism 2) Runtime polymorphism ...

Show Answer
0 like 0 dislike
1 answer

Write any four differences between compile time and run time polymorphism.
Last Answer : Sr. No. Compile-time Polymorphism Run-time Polymorphism 1 Compile time polymorphism means that an object is bound to its function call at compile time. Run time polymorphism means that selection ... binding. 7 E.g. overloaded function call. E.g. virtual function. ...

Show Answer
0 like 0 dislike
1 answer

Define the following terms: (i) Data abstraction (ii) Class (iii) Dynamic binding (iv) Polymorphism
Last Answer : (i) Data abstraction: Abstraction refers to the act of representing essential features without including the background details or explanation. Data abstraction is the process of defining a data type, often ... more than one form at different instances depending on the type or number of arguments. ...

Show Answer
0 like 0 dislike
1 answer

Describe with example importance of virtual base class.
Last Answer : A virtual base class (Grandparent class) is a class that avoids duplication of inherited data in derived class (child class) derived from parent classes (parent1 and parent2) which in turn derived ... duplications class L is declare as virtual base class while defining base classes B1 and B.  ...

Show Answer
0 like 0 dislike
1 answer

How to define virtual function? Give example.
Last Answer : A virtual function is a member function that is declared within a base class and redefined by a derived class. To define virtual function following syntax is used: Class baseclass { virtual function_name() { } ... } }; Example: #include class Base { public: virtual void show( ) { cout...

Show Answer
0 like 0 dislike
1 answer

What is the need of virtual function? Explain with example.
Last Answer : When base class and its derived class both contain same name and prototype member function then derived class function overrides base class function. Base class pointer is used to refer member functions of its ... Program/Example: #include class Base { public: virtual void show( ) { cout...

Show Answer
0 like 0 dislike
1 answer
Welcome to Ask Public, where you can ask questions and receive answers from other members of the community.

708k questions

669k answers

287 comments

10.7k users

...