I am not able to understand virtual and func overriding in classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I am not able to understand virtual and func overriding in classes

what do we mean by virtual int X()=0;? if X () is a function of a class A and how does it effect how the function work?

30th Jun 2016, 5:38 PM
Mukul Kumar
Mukul Kumar - avatar
4 Answers
+ 3
Virtual functions are functions that are dynamically polymorphic. But before we dig into that maybe it's a good idea to set your mind and define polymorphism and it's two flavors first. Polymorphism is Greek and means "many shapes"(poly=many, morphos = shape). Therefore polymorphism enables you to give a function "many shapes". Many shapes in this context means that you can have a function named "func" (or whatever you prefer) to have the same name but different implementations. These different implementations can be distinguished by the compiler by a different list of parameter types in the function definition. An example: void func() { cout << "func()\n"; } void func(int) { cout << "func(int)\n"; } void func(float) { cout << "func(float)\n"; } void func(int, float) { cout << "func(int,float)\n"; } This is *static* polymorphism as the function to call is determined at compile time. Using the keyword "virtual" enables *dynamic* polymorphism, i.e. the function to call is determined at runtime. As you may have noticed, the "virtual" keyword is only valid for class methods. Making a function "virtual" enables you to use a pointer to a superclass and call a function that is defined in one of the subclasses. Here's an example: class A { virtual void func(){ cout << "We're in class A\n"; }; }; class B { virtual void func(){ cout << "We're in class B\n"; }; }; int main() { A* aPtr = new B(); a->func(); delete aPtr; } Even though the pointer aPtr is of type "pointer to A" it refers to an instance of B. If "func" was not "virtual" the implementation in A would be called but with "virtual" the implementation of B is called. Thus, dynamic polymorphism enables you to (re)define a function of a superclass by overriding it with the same signature in subclasses (-> "many shapes" of the function). In summary, dynamic polymorphism is a technique to handle detailed implementations in a uniform way (always the same signature of the function) using abstraction (pointer to a superclass).
30th Jun 2016, 6:20 PM
Stefan
Stefan - avatar
+ 1
Apart from the wonderful answer of stefan, creating a function that is pure virtual ( void f() = 0 ( it can have any return type ) ) means that you must implement the same function in the derived classes. The virtual keyword is used to tell the compiler that that function may be defined in derived classes, and to take that into account when calling the function from a base pointer. The override identifier(not keyword) is used just to tell the compiler that the function is overriding a virtual function coming from a base class. And you can only put that identifier after the function signature, anywhere else it is just a normal identifier, you could use it as a variable name. Note that it is not required, but makes reading code easier. Example: #include <iostream> using namespace std; class A { public : virtual void fun(int) = 0; // Pure virtual. Throws compile-time error when not defined in derived classes. } class B : public A { public: void fun(int i) override { cout << i; } // Overrides the function A::fun(int). Override identifier is optional, but if it has it the function throws an compile-time error when it doesnt override. } int main() { A* test = new B; test->fun(6); // Calls B::fun(int) return 0; }
1st Jul 2016, 12:06 PM
Garme Kain
Garme Kain - avatar
1st Jul 2016, 12:52 PM
Garme Kain
Garme Kain - avatar
0
thanks for your answer both kain and stefen I know its real hard work to write such big answers.+1
1st Jul 2016, 5:28 PM
Mukul Kumar
Mukul Kumar - avatar