Originally Posted by
Syndacate
A) The view modifier prepended before the class name that you're inheriting from, that sets a MINIMUM level of restriction, in that instance of the class, ie. if C inherits from P, and P has all private members, but it's inherited with private restriction, then the members of P are accessible from C as if they were C's private members? But if they were all public, declaring the inheritance as public won't give C access to P's private members.
Incorrect. In fact, exactly the opposite.
Inheritance (public, protected, private) can not increase accessibility of inherited members. Either the accessibility remains the same or is it reduced.
A derived class therefore can never have direct access to a private member of a base class.
Originally Posted by
Syndacate
B) Even if a function is private, if declared pure virtual (void function), the class is therefore abstract and cannot be instantiated, right? So what would the point of writing a function that's pure virtual without code under a private visbility in a parent class be?? I have some code that I didn't write (but I can't post here
) that has that, and I'm trying to figure out the rationale behind it. There's no way a child could implement that, it's private, right?
A class with a pure virtual function cannot be instantiated. Period. A derived class must override an inherited pure virtual, or the derived class cannot be instantiated either.
The thing to realise is that declaring a function as pure virtual does not disallow implementing it (i.e. providing a body).
So
Code:
class X
{
virtual void func() = 0;
};
void X::func() // this is perfectly legal
{
std::cout << "X::func()\n";
}
The purpose in declaring func() as private (so it is a private pure virtual function) is that a derived class is not allowed to call it.
This prevents things like
Code:
class Y : public X
{
void func(); // yes, this is allowed, it overrides X::func()
};
void Y::func()
{
X::func(); // not allowed as func() is private to X
std::cout << "Y::func()\n";
}
Why would you do this? Because other member functions of class X may need to call X::func(), but we don't want derived classes to call it.