Virtual functions in C ++ and Java

I read about virtual functions and found

VFs are used in inherited class polymorphism.

So, if a class and a derived class have the same function name, VF associates the corresponding function with the function call.

i.e. If the function in question is denoted as virtual in the base class, then the derived function of the class will be called. If it is not virtual, the base class function will be called.

In Java, by default: all functions are virtual C ++: not virtual and can be made virtual in Java using the final, private access modifier, but in C ++ using the virtual keyword to make the function virtual.

Based on the above theory, I wrote the code:

#include <iostream> class base{ public : virtual void function1(){ std::cout<<"BaseVirtual"<<std::endl; } void function2(){ std::cout<<"Base NonVirtual"<<std::endl; } }; class derieved: public base { public : void function1(){ std::cout<<"Derieved Virtual"<<std::endl; } void function2(){ std::cout<<"Derieved NonVirtual"<<std::endl; } }; int main() { base b1; derieved d1; b1.function1(); b1.function2(); d1.function1(); d1.function2(); } 

Now, based on the fact, if its virtual function is called only by a derived function of the class, my output for the above program should be:

 BaseVirtual Base NonVirtual Derieved Virtual Base NonVirtual 

however, it turned out:

 BaseVirtual Base NonVirtual Derieved Virtual Derieved NonVirtual 

which should be right, of course. So my question is that the result completely violates the operator . If the function in question is assigned virtual in the base class, then the derived function of the class will be called. If it is not virtual, the base class function will be called. to call:

  d1.function2(); 
+4
source share
3 answers

Yes .. the role of the virtual one comes into the picture if and only if you are trying to access an object of a derived class with a pointer to the base class.

With an example: -

 #include <iostream> class base{ public : virtual void function1(){ std::cout<<"BaseVirtual"<<std::endl; } void function2(){ std::cout<<"Base NonVirtual"<<std::endl; } }; class derieved: public base { public : void function1(){ std::cout<<"Derieved Virtual"<<std::endl; } void function2(){ std::cout<<"Derieved NonVirtual"<<std::endl; } }; int main() { base *b1; derieved d1; b1=&d1; b1->function1(); b1->function2(); return 0; } 

Output: -

 Derieved Virtual Base NonVirtual 
+9
source

At the moment, you create one object from base and derived , and then call function1 and function2 directly on these objects. Under these conditions, virtual (or their absence) does not matter.

At least in C ++, for virtual means anything, you need to work with a pointer (or reference) to a base class that references an object, which can be either a base or a derived class:

 base *b2 = &d1; // invoke non-virtual function. Inovkes base::function1, because we're using // pointer to base. b2->function1(); // invoke virtual function. Invokes derived::function2 because the pointee object // is a derived. b2->function2(); 

This is especially useful if you have (for example) a collection of pointers to objects where the objects to which these pointers belong can be any of several different types. One classic example is the base class shape with line , circle , square , etc., Derived from it. When you call each draw member, each draws its own shape. In this particular case, your base class is probably an abstract base class - the draw member is declared "pure virtual", that is, you cannot create an object of the base class shape and create an instance of the derived class that must override the draw member function:

 class shape { public: virtual void draw() = 0; }; class circle : public shape { public: virtual void draw() { /* draw itself */ } }; 

Then in your drawing program you will create a collection (pointers) that the user has created, and to draw them all, you simply look at the collection and tell everyone to draw themselves. Higher-level code does not need to know or care about whether a particular figure is a circle, square, triangle, etc.

 for (int i=0; i<shapes.size(); i++) shapes[i]->draw(); 
+4
source

Polymorphism in C ++ requires the use of pointers. If you change your example to:

 base *b1 = new base(); base *d1 = new derived(); 

It actually uses a virtual mechanism. However, you seem to be confused with the basic ideas of polymorphism. If you define a class as derived, it will, of course, call the functions defined in the derived class, as well as with base .

Edit: To make this more explicit, here is the conclusion and explanation:

 b1->function1(); //Will call base::function1() b1->function2(); //Will call base::function2() d1->function1(); //Will call derived::function1() d2->function2(); //Will call derived::function2() 

All polymorphism / virtual calls allow you to treat a derived pointer as a base type, invoking (correct) derived functions on it. Therefore, if you have another function, for example:

 void foo(base& b) { b.function1(); } 

Then passing b1 will call base::function1() , passing d1 will call derived::function1() .

+1
source

Source: https://habr.com/ru/post/1441436/


All Articles