How do I get the output of this code segment to print "Function of Child Class"?
I'm having trouble understanding why writing
BaseClass obj = DerivedClass();
causes a similar output
#include <iostream>
using namespace std;
class BaseClass {
public:
void disp(){
cout<<"Function of Parent Class";
}
void dispOther(BaseClass& other) {
other.disp();
}
};
class DerivedClass: public BaseClass{
public:
void disp() {
cout<<"Function of Child Class";
}
};
BaseClass getInstance() {
BaseClass obj = DerivedClass();
return obj;
}
int main() {
auto obj = getInstance();
auto obj2 = DerivedClass();
obj2.dispOther(obj);
return 0;
}
output:
Function of Parent Class