A virtual function is a member function in a base class that can be overridden in a derived class. When a virtual function is called using a pointer or reference to a base class object, the appropriate derived class function is called based on the type of the object pointed to or referenced. Here is an example program showing the use of virtual function: #include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape(int w = 0, int h = 0) { width = w; height = h; } virtual int area() { cout << "Parent class area :" << endl; return 0; } }; class Rectangle: public Shape { public: Rectangle(int w = 0, int h = 0): Shape(w, h) { } ...
This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.