M
msalters
Nivvy schreef:
The reason for this is that Derived implements not only its own
interface, but also the Base interface. In the Base interface,
f1 is public. That means anybody who use the Base interface can
get f1 - independent of the class implementing that interface.
In your case, Derived implements the Base interface for f1 and
main uses that interface. Both sides follow proper OO rules, so
the compiler doesn't stop you.
Now, the Derived interface doesn't offer f1. That may seem strange
but there is no OO requirement for that. A derived class may offer
additional interfaces which are not supersets of the Base interface.
(This is also what allows Multiple Inheritance - those interfaces
are also incompatible with Base ).
HTH,
Michiel Salters
HI all,
I just wondered by seeing the o/p of the program. Let me know wt is the
reason.
class Base
{
public :
virtual void f1()
{
cout << "Base::f1() "<<endl;
}
};
class Derived : public Base
{
private:
void f1()
{
cout << "Derived::f1() "<<endl;
}
};
int main()
{
Base *bp = new Derived;
bp->f1();
return 0;
}
It ouputs the Derived private function.
What is the reason, whether this is like breaking OOPS.
The reason for this is that Derived implements not only its own
interface, but also the Base interface. In the Base interface,
f1 is public. That means anybody who use the Base interface can
get f1 - independent of the class implementing that interface.
In your case, Derived implements the Base interface for f1 and
main uses that interface. Both sides follow proper OO rules, so
the compiler doesn't stop you.
Now, the Derived interface doesn't offer f1. That may seem strange
but there is no OO requirement for that. A derived class may offer
additional interfaces which are not supersets of the Base interface.
(This is also what allows Multiple Inheritance - those interfaces
are also incompatible with Base ).
HTH,
Michiel Salters