0% found this document useful (0 votes)
37 views11 pages

Virtual Function

The document discusses virtual functions in C++. It defines virtual functions as functions in a base class that can be overridden in derived classes, allowing for late binding. It provides examples of how to declare virtual functions, how they work, and their usage with polymorphism.

Uploaded by

Ethan Samson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views11 pages

Virtual Function

The document discusses virtual functions in C++. It defines virtual functions as functions in a base class that can be overridden in derived classes, allowing for late binding. It provides examples of how to declare virtual functions, how they work, and their usage with polymorphism.

Uploaded by

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

VIRTUAL FUNCTION

Presented By:
Name: Farhan Ramzan
Roll No: 40901
Class: BSCS(Evening)
Submitted To: Mam Farwa
C++ VIRTUAL FUNCTION
1. Virtual function is a function in base class which is overrided in the
derived class, and which tells the compiler to perform late binding on this
function.
2. Virtual keyword is used to make a member function of the base class
virtual
HOW TO CREATE VIRTUAL FUNCTION?

1. A virtual function is a member function that is declared as virtual


within a base class and redefined by a deriver class.
2. To create virtual function, precede the base version of function's
declare with the keyword virtual.
3. The method name and type signature should be same for both base and
derived version of function.
HOW VIRTUAL FUNCTION WORKS?
1. Base class pointer can point to derived class object.
2. In this case, using base class pointer if we call some function which is in both classes,
then base class function is invoked.
3. But if we want to invoke derived class function using function base class pointer, it
can be achieved by defining the function as virtual in base class, this is how virtual
function supports runtime polymorphism.
USING VIRTUAL KEYWORD

1. Using virtual keyword with base class version of show function; late
binding takes place and derived of the function will be called,
because base pointer pointes an derived type of object.
2. We know that in runtime polymorphism the call to a function is
resolved at runtime depending upon the type of object.
SYNTAX
Virtual return_type function_name()
{
body
}
Ex: virtual void print()
GENDRAL FORMAT
Class class _name
{
Public:
virtual return_type function _ name (arguments)
{
Body
}
};
Class A {
};

EXAMPLE
• #include<iostream>
• using namespace std; • class B: public A
• class A • { private:
• { private:
• int b;
• int a;
• public:
• public:
• B()
• A()
• {
• {
• b = 2;
• a = 1;

• }
}
• virtual void show() • void show()
• { • {
• cout <<a; • cout <<b;
• } • }
int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}
EXAMPLE 2(with parameters)
#include<iostream>
using namespace std;
class A
class B:public A
{
{
public:
public:
virtual void show(int a,int b)
void show(int c,int d)
{
{
cout<<"the sum of"<< a << " and " << b <<
cout<<"The Multiple of"<<
" is:"<<a+b<<endl; c << " and " << d << " is:"<<c*d<<endl;

} }
}; };
int main()
{
A obj1;
B obj2;
A *ptr;
int g,h;
cout<<"enter the value of g:";
cin>>g;
cout<<"enter the value of h:";
cin>>h;

ptr=&obj1;
ptr->show( g,h);
ptr=&obj2;
ptr->show(g,h);
return 0;

You might also like