Understanding this code in C ++. Inheritance and Scope Change

I came across this in one of the posts on SO. I find it hard to understand 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? classA will usually be an interface and you cannot create one. You could only implement its implementation. I would appreciate it if someone could explain to me what is going on here.

Update:

can be considered a in LineA as an implementation of b?

+5
source share

Source: https://habr.com/ru/post/1202935/


All Articles