class a
{
virtual void foo(void) ;
};
class b : public a
{
public:
virtual void foo(void)
{
cout<< "class b";
}
};
int main ( )
{
class a *b_ptr = new b ;
b_ptr->foo();
}
please guide me why the b_ptr->foo() will not call the foo() function of the class b?
As you've written the code, it won't compile due to access control violations. Since b_ptr
is actually of type a *
and a::foo
is private, the compiler won't allow that.
But make a::foo
public and that will correctly call b::foo
.
There is also the problem that you have not defined a::foo
so your program won't link. You need to either define it or make it pure virtual (i.e virtual void foo(void) = 0;
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With