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.”

47 COMMENTS

  1. Hello! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to
    rank for some targeted keywords but I’m not seeing very good gains.

    If you know of any please share. Many thanks!

  2. Greetings! This is my first comment here so I just wanted to give a quick shout out and tell you I really enjoy reading your blog posts. Can you suggest any other blogs/websites/forums that go over the same topics? Thanks a lot!| Heidi Cy Frangos

  3. I JUST CALLED COSTCO CONCIERGE SERVICES BECAUSE THIS IS WHERE WE BOUGHT MY CANON SD790 IS CAMERA THAT IS HAVING THE BLACK SCREEN AND E16 ERROR CODE AND HE GAVE ME A FIX THAT WORKS. Neille Man Esmaria

  4. I am not sure where you are getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for excellent information I was looking for this information for my mission. Aloise Yance Jillana

  5. yEARS AGO MY CHOC CHIPS CAME OUT THIN AND CRISPY BUT THESE LAST SEVERAL YEARS i HAVE NOT BEEN ABLE TO GET THEM THIN ANS CRISPY.. i TRIED TTHIS RECIPIE TODAY AND FOLLOWED THE RECIPIE . tHEY ARE GOOD BUT NOT THIN AND CRISPY. wHAT AM i DOING WRONG, i AM GOING TO FREEZE SOME OF THE DOUGH . aNY SUGGESTIONS AS TO HOW TO MAKE THEM THIN AND CRISPY WHEN i THAW OUT THE REST OF THE DOUGH Tiena Butch Gant

  6. Somebody necessarily lend a hand to make critically articles I would state. This is the first time I frequented your website page and to this point? I amazed with the analysis you made to create this actual submit extraordinary. Magnificent process! Dita Jonathon Petronia

  7. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras scelerisque, tellus in tincidunt vestibulum, lorem est dapibus lectus, eget fermentum urna tortor vel risus. Giulietta Wilmar Belshin

  8. Eu et tellus vestibulum taciti et sit, nunc enim ipsum donec aliquam vitae, per mauris, amet ultrices. Pellentesque amet proin ut vestibulum eleifend nam, wisi vel tellus pulvinar mi risus consectetuer, sed faucibus facilisi, accumsan nam. Ulla Barde Wiersma

LEAVE A REPLY

Please enter your comment!
Please enter your name here