A virtual function a member function which is declared within a base class and is re-defined(or overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
2: Why do we need a virtual function?
We use virtual function when we want to override a certain behavior (like method) for the derived class rather than the one implemented for the base class, and we want to do so at run-time or later binding through a pointer to the base class. This is called polymorphism.
See the following example,
class Shape {//base class public: Shape() {}//constructor; cannot be virtual! virtual void draw() = 0; //virtual method; for "=0" indicating it is a pure virtual function virtual ~Shape() {}//virtual distructor; must be virtual! otherwise it will cause problems... }; class Rectangle : public Shape { //derived class public: void draw() {cout<<"Rectangle";} }; class Circle : public Shape { //derived class public: void draw() {cout<<"Circle";} };
Now if we need to print different kinds of shapes, we just need to create a vector containing pointers:
vector<Shape*> shapes; shapes.push_back(new Rectangle); shapes.push_back(new Circle); for(auto &it : shapes) { it->draw(); }
In this way, we can draw different kinds of shapes with one virtual function-draw(). Proper version of the method of draw() is selected based on the run time information about the type of object behind pointer.
For pure virtual function, we call declare them by just place "= 0" after method protocol. In this case we won't be able to create an instance of object with pure virtual function and it will be called Abstract class.
For virtual destructor, it is necessary when we plan to work with objects through pointers to their base class. So when we call "delete" for base class pointer, all chain of destructors will be called and there won't be memory leak like problems.
The constructor cannot be virtual. The reason is that when constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. Hence the constructor should always be non-virtual. The virtual mechanism only works when you have a based class pointer to a derived class object.
In addition, from Bjarne Stroustrup's C++ Style and Technique FAQ Why don't we have virtual constructors?
A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only any interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual.
3: why do we need a pure virtual function?
"Briefly, it's to make the class abstract, so that it can't be instantiated, but a child class can override the pure virtual methods to form a concrete class. This is a good way to define an interface in C++." Non-abstract classes can contain virtual member functions and be instantiated.
One important thing to note is that, you should override the pure virtual function of the base class in the derived class. If you fail the override it, the derived class will become an abstract class as well.
No comments:
Post a Comment