0% found this document useful (0 votes)
43 views5 pages

Atul 777

Virtual functions allow polymorphism in C++ by allowing derived classes to override functions defined in a base class. When a base class pointer points to a derived class object, the version of the virtual function that is called at runtime is determined by the actual object type, not the pointer type. Pure virtual functions act as placeholders that must be implemented in derived classes, making a class abstract and unable to be instantiated on its own.

Uploaded by

atuklumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Atul 777

Virtual functions allow polymorphism in C++ by allowing derived classes to override functions defined in a base class. When a base class pointer points to a derived class object, the version of the virtual function that is called at runtime is determined by the actual object type, not the pointer type. Pure virtual functions act as placeholders that must be implemented in derived classes, making a class abstract and unable to be instantiated on its own.

Uploaded by

atuklumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Virtual function

Polymorphism in biology means the ability of an organization to assume a


variety of forms. In C++, it indicates the form of a member function that can
be changed at runtime. Such member functions are called virtual function
and the corresponding class is called polymorphic class. The object the class
polymorphic class, addresses by pointers, change at runtime and respond
differently for the same message.
In C++, run time polymorphism is achieved using
virtual functions. Virtual functions facilitated dynamic binding of functions
to appropriate objects. They are the means by which function of the base
class can be overridden by function of the derived class. Virtual function
allows derived class to redefine member functions inherited from a base
class. Virtual functions allow programmers to declare function in base class
that can be redefined in each derived class. When a pointer to the base class
is used with a base or derived class object, the object to which it points
determines the activation of an appropriate member function call. That is,
when a base class pointer points to the object of a derived class, the derived
class’s member function is selected and when it points to the object of the
base class’s member function is selected at runtime.
When we use the same function name in both
the base and derived classes, the function in base class is declared as
virtual using the keyword virtual preceding its normal declaration. When a
function is made virtual, C++ determines which function to use at run time
based on the type of object pointed to by the base pointer, rather than the
type of the pointer. Virtual functions should be defined in the public section
of a class to realize its full potential benefits .

Syntax:
Class student
{

Public:
……
…....
Virtual return type function name (argument)

{
……….
…………
}
……….
};
Program

#include<iostream.h>
#include<conio.h>
Class base
{
Public:
void display()
{
Cout<< “\n display base”;
}
Class derived : public base
{
Public:
Void display()
{
Cout<<”\n display derived”;
}
Void show()
{
Cout<<”\n show derived”;
};
Int main()
{
Base b;
Derived d;
Base *bptr;
Cout<<”\n bptr points to base \n”;
}
bptr = &b;
bptr -> display(); //calls bqase version
bptr -> show(); // calls base version
cout<< “\n\n bptr points to derived\n”;
bptr =&d;
bptr -> display(); // calls base version
bptr -> show(); // calls derived version
return 0;
getch();
}

Rules for virtual function:


1. The virtual function must be member of class.
2. They cannot be static members.
3. They are be accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived
class version must be identical. If two function with the same name have different
prototypes, C++ consider them as overloaded functions, and the virtual function
mechanism is ignored.
7. We cannot have virtual constructors, but we can have virtual destructors.

Abstract class
Abstract classes (classes with at least one virtual function) can be used as a framework
upon which new classes can be built to provide new functionality. A framework is a
combination of class libraries (set of cooperative classes) with predefined flow of control.
Abstract classes with virtual functions can be used as an aid to debugging. Suppose, it is
required to build a project consisting of a number of classes, possibly using a large
number of programmers.

PURE VIRTUAL FUNCTIONS


It is normal practice to declare a function virtual inside the base class and redefine it in
the derived classes. Pure virtual function normally serves as a framework for future
design of the class hierarchy; these functions can be overridden by the methods in the
derived classes. In most of the cases, virtual functions are defined with a null-body; it has
no definition. such functions in the base class are similar to do-nothing or dummy
functions and in C++, they are called pure virtual functions. the syntax of defining pure
virtual function is shown in down. Pure virtual function is declared as a vitual function
with its declaration by=0.

Syntax:
Class student
{
Public:
…….
………
Virtual return type function name(argument)=0;
……….
…………
};

A pure virtual function declared in a base class has no implementation as far as the base
class is concerned.
The following are the properties of pure virtual functions:
1. A pure virtual function has no implementation in the base class hence, a class
with pure virtual function cannot be instantiated.
2. It acts as an empty bucket (virtual function is a partially filled bucket) that the
derived class is supposed to fill.
3. A pure virtual member function can be invoked by its derived class.

You might also like