This is incorrect. Members are always inherited, using public
for the inheritance means that they will remain the same access level e.g.
class Base
{
public:
void PublicFoo();
protected:
void ProtectedFoo();
private:
void PrivateFoo();
};
class Derived : public Base
{
// All of these are inherited
// public:
// void PublicFoo();
// protected:
// void ProtectedFoo();
// private:
// void PrivateFoo();
};
Changing Derived to be
class Derived : protected Base
Would inherit PublicFoo()
as a protected member instead and having
class Derived : private Base
// or simply, as private is default for classes
class Derived : Base
Would inherit everything as private members.
The point of virtual functions is for “dynamic dispatch”. Given a base pointer (or reference) you can call a virtual function and it will dispatch to the correct function.
#include <cstdio>
struct Animal
{
virtual void Speak() const
{
puts("Animal");
}
virtual ~Animal() = default;
};
struct Dog : Animal
{
void Speak() const override
{
puts("Woof!");
}
};
struct Cat : Animal
{
void Speak() const override
{
puts("Meow!");
}
};
// this will call the correct function.
void Talk(const Animal& A)
{
A.Speak();
}
int main()
{
Cat C;
Dog D;
C.Speak();
D.Speak();
puts("");
Talk(C);
Talk(D);
}
And if you remove the virtual-ness Talk
no longer calls the correct function for the actual type.
Demo:
Compiler Explorer
Compiler Explorer
The base function doesn’t get called, if the parent class’ BeginPlay function needs to do something then not calling it would mean it doesn’t do the stuff it needs to.