Inheritance questions

B

Busin

When a child class inherits from a base class, will the child class inherits
everything of the base class, including all member variables and functions?
Or is such inheritance "selective", like not inheriting all constructors,
assignment operators, destructor, etc.? Thanks!
 
A

Alf P. Steinbach

* Busin:
When a child class inherits from a base class, will the child class inherits
everything of the base class, including all member variables and functions?
Or is such inheritance "selective", like not inheriting all constructors,
assignment operators, destructor, etc.? Thanks!

First, terminology: what you call a child class is in C++ called a
derived class, and more generally a subclass.

Yes, it inherits everything except those things you mention, namely
assignment operators, constructors and (with a high-level interpretation
of "inherit") destructors, but not everything is accessible
(accessibility depends on public/protected/private).

The reason constructors cannot be said to be inherited is that they're
not inherited. :)

The reason destructors cannot generally be said to be inherited is that
every class either defines its own destructor, or has one automatically
defined, or declares a destructor with no definition. But this is less
of a "no inheritance" than is the case with constructors. Depending on
what you mean by inheritance it's possible to argue that destructors are
in some cases inherited; for example, if class Base has a virtual
destructor, and class Derived does not define its own destructor, then
the implementation may choose to just use Base::~Base also for Derived
instead of actually definining a new destructor for Derived, and when
debugging a program at the source code level you will certainly not see
any Derived destructor being called, only the "inherited" Base one.
Also it's difficult to understand how a virtual destructor can be
overridden unless one thinks of it as inherited. But the terminology
adopted in the standard is one where a destructor is by definition not
inherited, but can be overridden -- I think it's just a mess... :)

The reason assignment operators are not inherited is that that would
play havoc with most classes. After all, most classes add data members.
Instead an assignment operator is automatically defined (roughly) if you
do not define or declare one yourself. But as with a virtual destructor
it's possible to argue -- with a more low-level view of "inherit" --
that virtual assignment operators can indeed be inherited. In
particular, without checking the standard on this, I think you can
override a virtual assignment operator, although such beasts are Evil.
 
A

Andrey Tarasevich

Busin said:
When a child class inherits from a base class, will the child class inherits
everything of the base class, including all member variables and functions?
Or is such inheritance "selective", like not inheriting all constructors,
assignment operators, destructor, etc.? Thanks!

In C++ terminology, constructors and destructors are not inherited,
while all other members are inherited.
 
B

Busin

Andrey Tarasevich said:
In C++ terminology, constructors and destructors are not inherited,
while all other members are inherited.
"all other members" includes assignment operator, static members, const
members, virtual members?
 
A

Andrey Tarasevich

Busin said:
"all other members" includes assignment operator, static members, const
members, virtual members?
...

Well, yes! Keep in mind though, that there are certain technicalities
involved in the meaning of the word "inherited". You might want also to
look into such thing as "name hiding", to understand the difference
between such situations as "inherited, but hidden" and "not inherited at
all".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top