I came across this in one of the posts on SO. I am having difficulty understanding the following code.
class A
{
public:
virtual void foo() = 0;
private:
virtual void bar() = 0;
};
class B : public A
{
private:
virtual void foo()
{
std::cout << "B in Foo \n";
}
public:
virtual void bar()
{
std::cout << "bar in Foo \n";
}
};
void f(A& a, B& b)
{
a.foo(); // ok --->Line A
//b.foo(); // error: B::foo is private
//a.bar(); // error: A::bar is private
b.bar(); // ok (B::bar is public, even though A::bar is private) --> Line B
}
int main()
{
B b;
f(b, b);
}
Now I wanted to know how Line A is possible ? Normally classA
would be an interface and you wouldnt be able to instantiate it. You would only be able to instantiate its implementation. I would appreciate it if someone could explain to me whats happening here.
Update :
is it possible to consider a
in LineA as an implementation of b ?