I recently heard one of my coworkers claim that the concept of a "subtype" is not defined in C++. He claims that "subtypes" are rightly called "derived types" in C++ terminology. Is this true? If I have:
class A { };
class B : public A { };
Can I call B a subtype of A? Or is it only valid to call B a "derived type" of A in C++?
According to wikipedia:
This definition means that public inheritance fits for the following classes:
However, C++ also provides
private
andprotected
inheritance, and these do not model the "is-a" relationship thatpublic
inheritance does, and therefore these types of inheritance do not meet the definition of subtype (as provided by wikipedia).Subtype is not a common way in C++ to describe these relationships, even in the cases where the definition does appear to fit (
public
inheritance).A subtype is a specialized version of a type. In C++ a derived class is a subtype (subclass) of a base class type. A variable of the derived type could appear anywhere that a variable of the parent type is requested. A subtype of another type means you can use the subtype in all situations where the parent type could be used.
From MSDN:
Stroustrup seems to prefer "derived class" but will also call it a "subclass": see this link.
In http://www.stroustrup.com/hopl2.pdf he writes,
Subtype is not part of the common jargon in C++. The definition in Wikipedia (thanks Chad) is quite broad and in C++ could represent multiple different things, including but not limited to inheritance. For example, all iterator types from a given category and pointers could be subtypes of the iterator concept as they can be substituted in templates that require that concept (including the standard library algorithms).
I would use derived in general, other alternative words for the same (in different languages) could include extends (type A extends B) or inherits.