Since I have overridden the function returnANumber in the derived class, the constructor shall return "overridden" instead of executing the branch in the parent class. But the result is different with what I expect :\
#include <iostream>
#include <string>
using namespace std;
class NewClass {
public:
NewClass(){
returnANumber();
}
virtual void returnANumber(){
cout << "random number blah blah blah";
}
};
class DeriveClass : public NewClass {
public:
void returnANumber() override {
cout << "overridden";
}
};
int main(){
NewClass NewClass;
DeriveClass DeriveClassInst;
DeriveClassInst.returnANumber();
}
Result: "random" "random" "overridden" The member function seems to be overridden at last, but it didn't work with the constructor. Expected output: "random" "overridden" "overridden"