Monday, March 26, 2012
Virtual functions in C++!!!
A Virtual function is a special type of function, which resolve to most-derived function with the same prototype. To make a function virtual, just use virtual keyword before the function. This is also called function overriding or runtime polymorphism.
Lets see the use of virtual functions. Assume that there is a base class with one member function. And one derived class with another member function with same name as in base class. If you try to call that member function as shown in the below sample code.
Output:
this is base class
From the above sample code, the expected result should be from derived function as base object is referring to the derived object. But we got the base function. Even though , base object is referring to derived object, it wont resolves to derived class. To avoid this we need to use the virtual keyword. See the same sample code with virtual keyword.
Example With Virtual keyword:
Output:
this is derived class
Now we got the expected output, this is because of the virtual keyword. If you use virtual keyword, it will check for resolving function for the derived classes, starting from the base class to referred derived class.
Another Example with more derived classes:
Output:
this is derived1 class
In the above sample code, base object is refering to derived1 object, so resolving process starts from the base class, it checks for the virtual keyword, if it finds the keyword, it goes to the derived class, this process goes on til it reaches the refered object, here it is derived1.
Use of Virtual keyword: To implement virtula functions, we need to use the virtual keyword in base class. And its optional to use in derived class. But its better to use in derived class also, so that you are informing that that function is a virtual function. Generally most base class function need to be used virtual.
Lets see the use of virtual functions. Assume that there is a base class with one member function. And one derived class with another member function with same name as in base class. If you try to call that member function as shown in the below sample code.
#include <iostream> using namespace std; struct base { public: void display() { cout<<"this is base class\n"; } }; class derived: public base { public: void display() // same function there in base class also { cout<<"this is derived class\n"; } }; int main() { derived d; base &b = d; // refering base object to derived object b.display(); }
Output:
this is base class
From the above sample code, the expected result should be from derived function as base object is referring to the derived object. But we got the base function. Even though , base object is referring to derived object, it wont resolves to derived class. To avoid this we need to use the virtual keyword. See the same sample code with virtual keyword.
Example With Virtual keyword:
#include <iostream> using namespace std; struct base { public: virtual void display() // using virtual keyword { cout<<"this is base class\n"; } }; class derived: public base { public: void display() //same function is there in base class { cout<<"this is derived class\n"; } }; int main() { derived d; base &b = d; // refering base object to derived object b.display(); }
Output:
this is derived class
Now we got the expected output, this is because of the virtual keyword. If you use virtual keyword, it will check for resolving function for the derived classes, starting from the base class to referred derived class.
Another Example with more derived classes:
#include <iostream> using namespace std; struct base { public: virtual void display() { cout<<"this is base class\n"; } }; class derived: public base { public: void display() { cout<<"this is derived class\n"; } }; class derived1: public derived { public: void display() { cout<<"this is derived1 class\n"; } }; class derived2: public derived1 { public: void display() { cout<<"this is derived2 class\n"; } }; int main() { base b1; derived1 d; base &b = d; // base object refering to direved1 object b.display(); }
Output:
this is derived1 class
In the above sample code, base object is refering to derived1 object, so resolving process starts from the base class, it checks for the virtual keyword, if it finds the keyword, it goes to the derived class, this process goes on til it reaches the refered object, here it is derived1.
Use of Virtual keyword: To implement virtula functions, we need to use the virtual keyword in base class. And its optional to use in derived class. But its better to use in derived class also, so that you are informing that that function is a virtual function. Generally most base class function need to be used virtual.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment