class Class1
{
public:
void print()
{
cout << "test" << endl;
}
void printl()
{
print();
}
};
class Class2 : public Class1
{
public:
void print()
{
cout << "test2" << endl;
}
};
Why does print() not get overridden in Class2, is there any way a function can be overridden like this? (Without virtual functions). Thanks
Class2 t;
t.printl();
No. This is the entire reason for virtual functions.
Without a virtual method here, when printl() calls print(), it’s calling
Class1.print(), which prints “test”. If you flag the method as virtual, then it will handle it as you were expecting.