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: –
- It must be a member of a class.
- It cannot be a static member.
- These functions are used by pointers of object.
- Constructors function cannot be vf.
- Destructors function can be virtual.
- 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.”
Nic Feature of virtual function
Very nice nice nice nice🙌🙌🙌🙌🙌🙌🙌🙌🙌
Normally I don’t learn post on blogs, however I wish to say that this write-up very compelled me to check out and do it!
Your writing style has been amazed me. Thanks, quite nice post.
It’s very straightforward to find out any topic on web as
compared to textbooks, as I found this paragraph at this website.
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!
Thank you for sharing with us, I think this website truly stands out :D. Corissa Keefe Narcissus
You can definitely see your enthusiasm within the work you write. Neile Brody Sabsay
Hi there, its good piece of writing concerning media print, we all understand media is a great source of facts. Evvie Bastien Tawsha
What a beautiful area David. Enjoyed looking at your photos, I especially like the bluebell woods. Donielle Glynn Kirchner
I am sure this article has touched all the internet people, its really really good article on building up new webpage. Doro Emmit Utta
Amateur Porn, Hd Porn, Asian Girl, Blowjob, Handjob live watch. Marie-Ann Dougy Maegan
Might work on mine tomorrow. Might work on the hat. Might just goof off and rewatch The Mandalorian. Martguerita Georgi Westbrooke
Here are some hyperlinks to web-sites that we link to due to the fact we feel they are really worth visiting. Michal Bartholemy Kriss
Great, thanks for sharing this blog post. Really thank you! Awesome. Loralee Kip Lustig
Very neat blog. Really looking forward to read more. Fantastic. Lenka Ludvig Daniell
Some really superb content on this website , thankyou for contribution. Edin Arv Demmer
I loved your blog article. Really looking forward to read more. Awesome. Evvy Briano Gibe
Thank you for your post. Much thanks again. Really Cool. Maxie Caldwell Maxma
I am genuinely thankful to the holder of this web site who has shared this impressive article at here. Anastasie Ulric Leifeste
Way cool! Some extremely valid points! I appreciate you penning this write-up and also the rest of the site is also really good. Kania Tom Roxana
I was able to find good information from your blog articles. Fernande Iggie Krantz
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
Very good post. I definitely appreciate this site. Continue the good work! Lorianne Bradley Amoritta
Hello, its good piece of writing about media print, we all be aware of media is a great source of data. Ketti Gunther Schonfield
I like the efforts you have put in this, appreciate it for all the great blog posts. Xaviera Gaspar Pitchford Malory Reynolds Debbra
I do like your review. Thanks. Can you tell me please if the book covers legal referencing. Marla Horton Obie
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
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
Capulla de capullo, es como llama Raquel a Duarte por la amista que los une. Estella Roderich Rosa
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
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
Well I definitely liked studying it. This tip procured by you is very practical for good planning. Penni Billie Swithbert
This piece was a lifejacket that saved me from drowning. Hadria Jabez Russon
I visited multiple web pages however the audio feature for audio songs present at this web site is genuinely wonderful.| Elset Bartholomew Gennaro
Cute idea to make our craft rooms even more special. Nonah Chris Heise
Thanks for sharing such a good idea, article is fastidious, thats why i have read it completely Monah Mar Florri
Great post. I will be experiencing a few of these issues as well.. Ilka Eddie Pan
I really like reading an article that can make people think. Also, thanks for allowing for me to comment. Shellie Cash Tasha
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
Hello! Do you know if they make any plugins to protect against hackers? Marian Chan Josepha
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
Greetings! Very useful advice in this particular article! It is the little changes which will make the greatest changes. Many thanks for sharing! Oona Valle Chung
Why visitors still make use of to read news papers when in this technological globe everything is available on net? Leta Baillie Ruvolo
Serge, now that Trump has Covid how in your view does this change the probability that he will win? Bobina Aldin Christis
I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Hedvig Jarrod Koblick
5693 A really Interesting article. nakliyat 6566 Cherish Kingsley Wendy
Very good write-up. I absolutely love this website. Thanks! Ilse Gustave Faubert
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
XyEeDqVfSZudKG