FUNCTONS

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

VIRTUAL FUNCTONS

175113346
SISHYA.S
I YEAR BCA – ‘C’
What will you learn..,
Virual Functions
Basics

Rules

Syntax

Example
Basics of Virtual Function
 It is a function in base class, which is overrided in the
derived class, and which the compiler to perform Late
Binding on this function.
 ‘Virtual’ keyword is used to make a member function
of the base class Virtual.
Continued..,
 LATE BINDING :
 In this function call is resolved at runime.
 Hence, now compiler determines the type of object
at runtime, and then binds the function call.
 Late Binding is also called,
• Dynamic Binding

• Runtime Binding
Rules For Virual Funcions
 It shouldn’t be static and must be member of a class.
 It may be declared as friend for another class. Object
pointer case access virtual function.
 Constructors cannot be declared as virtual, but
destructors can be declared as virtual.
 The Virtual Function must be defined in public section of
the class.
 It is also possible to define the virtual function outside
the class.In such a case, the declaration is done inside the
class and definition is done outide the class.
Continued..,
 The virtual keyword is used in the declaration and not in
function declarator.
 It is also possible to return a value from virtual function
like other functions.
 The prototype of virtual functions in base and derived
classes should be exactly the same.In case of mismatch,
the compiler neglects the virtual function mechanism
and them as overloaded functions.
 Arithmatic performance cannot be used with base class
pointer.
Continued..,
 If base class contains virtual function and if the same
function is not redefined in the derived classes in that case
the base class function is invoked.
 The operator keyword used for operator overloading also
supports virtual mechanism.
Syntax For Virual Funcions
To declare virtual function and execute the same function defined in
base and derived classes.
#include <iostream.h>
#include <conio.h>
class first
{
int b;
public :
first() {b=10 ;}
virtual void diplay() { count <<“\n b=”<<b ;} };
class second : public first
{
int d ;
public:
second() {d=20;}
void diplay() { count <<“\n d=”<<d ;} };
Continued..,
int main()
{
clrscr();
first f,*p;
second s;
p=&f;
p->display();
p=&s;
p->display();
Return 0;
}
Example For Virual Funcions
#include <iostream.h>
#include <conio.h>
class shape
{
public :
virtual void area()=0;
};
class circle: public shape
{
public:
int b,h;
float arcircle;
void area()
{
int r;
cout<<”\n\n area of the circle”;
cout<<”\n enter the radius:” ;
cin>>r;
acircle =3.14*r*r;
cout<<”\n area of circle =”<<acircle;
}
};
Continued..,
class rect: public shape
{
public:
int w,h;
int arrec;
void area()
{
cout<<”\n\n area of the rectangle”;
cout<<”\n enter the width and height”;
cin>>w>>h;
arec =w*h;
cout<< “\n area of the rectangle:”<<arec;
}
};
class tria: public shape
{
public:
int b,h;
float artri;
void area()
{
Continued..,
cout<<”\n\n area of the triangle”;
cout<<”\n enter the base and height”;
cin>>b>>h;
atri=0.5*b*h;
cout<< “\n area of the triangle:”<<artri;
} };
class square: public shape
{
public:
int a;
float arsquare;
void area()
{
cout<<”\n\n area of the square”;
cout<<”\n enter the value of a”;
cin>>a;
arsquare =a*a;
cout<< “\n area of the triangle:”<<arsquare;
}
};
Continued..,
void main()
{
clrscr();
shape*ptr;
circle 0b1;
rect 0b2;
tri a 0b3;
square 0b4;
ptr = &0b1;
ptr ->area();
ptr = &0b2;
ptr ->area();
ptr = &0b3;
ptr ->area();
ptr = &0b4;
ptr ->area();
getch();
}

You might also like