I am having trouble understanding the purpose of a keyword virtualin C ++. I know C and Java very well, but I'm new to C ++
From Wikipedia
In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be redefined as a legacy class using a function with the same signature.
However, I can override the method as shown below without using the keyword virtual
#include <iostream>
using namespace std;
class A {
public:
int a();
};
int A::a() {
return 1;
}
class B : A {
public:
int a();
};
int B::a() {
return 2;
}
int main() {
B b;
cout << b.a() << endl;
return 0;
}
As you can see below, the function A :: a is successfully redefined by B :: a, without requiring virtual
The combination of my confusion is a statement about virtual destructors, also from wikipedia
, ++ .
, virtual ? , -, virtual " "