Virtual Function And Pure Virtual Function

virtual function
virtual function

Virtual Function: –

It is a member function of a class. By using a keyword virtual, we can declare it in the base class. To declare a virtual function, we precede its declaration with virtual keyword.

When we want to implement sum time polymorphism by using vf then this function must be called through the base class pointer which will contain the address of objectives of different derived classes. By doing this, the compiler relets the appropriate function of divide class at sum time depending upon the matter of base class pointer. It does not see the type of pointer.

Thus by making the base class pointer, pointing to objects of different classes, the different virtual functions can be invoked at run time life. This we can implement sum time polymorphism.

These types of function are known as virtual function since time bas class vf is not called, derived class function of same name called.Function type is redefined by same function name by its derived classes.

Feature of Virtual Function: –

  1. It must be a member of a class.
  2. It cannot be a static member.
  3. These functions are used by pointers of object.
  4. Constructors function cannot be vf.
  5. Destructors function can be virtual.
  6. If we didn’t redefine the vf is the derived classes then base class vf will be invoked.

 

Example,

#include<iostream.h>

#include<conio.h>

class baseclass

{

public:

virtual void show ()

{ cout<<” baseclass show is called\n”;}

};

class derivedclass1: public baseclass

{

public:

void show()

{ cout<<” derivedclass1 show called\n”;}

};

class derivedclass2: public baseclass

{

public:

void show()

{ cout<<” derivedclass2 show called\n”;}

};

void main ()

{

clrscr ();

baseclass * ptr;

derivedclass1 d1;

derivedclass2 d2;

ptr= &d1;

ptr-> show ();

ptr= &d2;

ptr-> show ();

getch();

}

Pure Virtual Function:-

If a virtual function is initialized by 0 then it is not providing any implementation, this type of virtual function is known as pure virtual function.

Since it has no body, so derive class must override the base class pure virtual function. It derives class doesn’t do then compiler will produce an error message to us.

We cannot create the object of a base class in which pure virtual function is available since it is an abstract class.

Example,

#include<iostream.h>

#include<conio.h>

class base

{

protected:

double a, b;

public:

void read ()

{ cin>>a>>b;}

virtual void getarea ()= 0;

};

class rect: public base

{

public:

void getarea ()

{

double area= a* b;

cout<<”\nArea of rectangle= “<<area<<”\n”;

}

};

class triangle: public base

{

public:

void getarea ()

{

double area= (a*b)/2;

cout<<” \nAarea of triangle= “<<area<<”\n;

}

};

int main ()

{

clrscr();

base *ptr[2];

rect r1;

cout<<”\n Enter lengh and breath of rectangle:”;

r1.read ();

triangle t1;

cout<<”\n Enter base and  perpendicular of triangle:”;

t1.read ();

ptr[0] =&r1;

ptr[1] = &t1;

for (int i=0; i<2; i++)

ptr[i]-> getarea ();

getch();

}

Output: –

Enter length and breath of rectangle: 10        20

Enter base and perpendicular of triangle: 5    20

Area of rectangle = 200

Area of tringle = 50

You Also Can Visit For More Knowledge In C++:-

Visit Your YouTube  Channel:-  Think , About It

Visit Your Website :- www.atozlives.com

Thanks,

“Play With All Computer Science , Mathematics  And Technology.”

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here