I have a question about virtual heritage. I am interested in this code 1000 (class "two") and not 3 ( Class "A") prints
Here is the code:
#include & lt; Iostream & gt; using namespace std; Class A {protected: int number; Public: A (int a = 0) {number = a;}}; Class A: Virtual public A {Public: A (int A = 3) {number = a;} Zero print () {cout & lt; & Lt; Number & lt; & Lt; Endl;}}; Class two: Virtual public A {Public: Two (Int a = 1000) {number = a;} Zero print () {cout & lt; & Lt; Number & lt; & Lt; Endl; }}; Class B: Public One, Public Two {Public: Zero Print () {cout & lt; & Lt; Number & lt; & Lt; Endl; }}; Int main () {BA; a print(); }
The base classes are started in that order: one < / Code> then two . Virtual heritage means that they share the same frequency to A , so there is only one variable number . returns one to 3 to number , then two is started, it is allocated to 1000 Does. Therefore, after starting the entire object, this value ends with 1000.
No comments:
Post a Comment