Oh boy, you got polymorphism and type safety all confused up there. What you are expecting to happen is wrong, that is not why a type is inherited. Inheritance works from parent, to child. So a function in parent, is guaranteed—
let's ignore the access modifiers in inheritance for a minute—in the child type, but not the other way around. You cannot call a child function from a parent type. Parent type just doesn't know what that function is. So,
class Foo {
int a;
int b;
};
class Bar {
string name;
};
In the code above, you can do this,
Bar b;
cout << b.a;
But you cannot do this,
Foo f;
cout << f.name;
But, in inheritance, polymorphism works just fine. So in your code, if we do this,
class deri : public base
{
public:
int func_1() override {
cout << "Derived func_1";
return 0; }
Now, when you run the same code,
base* base = new deri;
base->func_1();
It would print, "Derived func_1". Why? Because of polymorphism.
I do not want to indulge you in the details of C++ memory model, because all this detail is applied there. Just remember, inheritance works from parent to child, but polymorphism forces that the function calls be done on the actual instance type—in your case
new deri;
.
Please see the links below, to learn a bit more on this concept,
Polymorphism (computer science) - Wikipedia[
^]
Type safety - Wikipedia[
^]
Memory model - cppreference.com[
^]
c++ - How does inheritance actually work? - Stack Overflow[
^]
Friendship and inheritance - C++ Tutorials[
^]