CSE 202: Computer Science II, Winter 2018
Derived Classes
Review of classes

Before covering derived classes, it is important that you remember what a class is.

A class is a user-defined type. When designing a class, what you can put inside of it are called members. There are three kinds of members you can have in a class:

class A { public: int b; void c() {} double d(int x) { return x / 2.0; } class E {}; typedef bool F; };

In the example above, this arbitrary class A has: 1 data member, b; 2 member functions: c and d; and 2 nested types, E and F.

When using this class, the operator :: is used for accessing nested types and the operator . is used for accessing data members and member functions:

A o; // Variable o has type A A::F p = true; // Variable p has type A::F which is an alias for bool o.b = 4; // Accessing the member b from o and assigning 4 to it. o.c(); // Accessing the member function c from o and calling it.

The access modes define the accessibility of certain class members:

A class can contain multiple access mode specifiers, allowing some members to be private, some to be protected, and others to be public.

Derived classes and Base classes

When designing a class, you can "combine" it with an existing class so that every object of your class-type will also contain all the members of that existing class.

struct A { int a, b; void c() {} }; struct B : A { int d, e; };

In the above example, the B class contains the public members a, b, c, d, and e. The : A part after struct B is known as the base clause, it is an optional clause for class declarations and it is used to specify the base classes for this class.

A class that has base classes is itself a derived class of those base classes and every object of this derived class contains one subobject for each base class.

Since struct is used instead of class, the members are public by default and the members of the base classes, which are accessible from the derived class, will be publically accessible through objects of this class.

The act of specifying one or more base classes within a class declaration is known as "inheritance". In our inheritance example, we say that B "inherits" from A. There are different types of inheritance with regard to access mode: public inheritance, private, inheritance, and protected inheritance.

By default, a class declared using struct uses public inheritance on it's specified base classes and a class declared using class uses private inheritance on it's specified base classes.

Types of inheritance

The syntax of a C++ class declaration is:

class-key name base-clause { member-specification }

class-key can be "class" or "struct", name is the name of the class, base-clause is an optional clause beginning with ':' and followed by a comma separated list of base specifiers where each base specifier is the name of a class to be used as a base class optionally preceeded with an access specifier of "public", "private", or "protected".

In the base clause, when a base class is specified with the access specifier public, all inherited members (which are the public and protected members of the base class) will have their access modes unchanged in the derived class, this is known as public inheritance. Public inheritance is the default inheritance mode when the current class is declared with class-key struct.

A base class specifier of private means that the inherited members will have private access mode in the current class; this is the default inheritance mode for derived classes declared with a class key of class.

A base class specifier of protected means that the inherited members will have protected access mode in the current class.

Remember that a class can control what members it allows a potential derived class to inherit. Private members cannot be inherited, public or protected members can be inherited.

Examples of inheritance
/* * Class A has protected data members a, b, and c */ class A { protected: int a, b, c; }; /* * Class B has private data members x, y, and z together with private data * members a, b, and c inherited from class A */ class B : A { int x, y, z; }; /* * Class A has private data member y, public data member y, and protected * data member z */ class A { int x; public: int y; protected: int z; }; /* * Class B has public data members i and y and protected data member z. */ class B : public A { public: int i; }; /* * Class A has public data member a and private data member b */ class A { public: int a; private: int b; }; /* * Class B has public data member x, protected data member a which was * inherited from class A, and private data member y. */ class B : protected A { public: int x; private: int y; }; /* * Class C has public data members i and j together with the private data * members x and a which were inherited from class B */ struct C : private B { int i, j; };
Inheritance used within the standard libraries
Summary
Access mode Meaning
public The class member(s) can be accessed from code existing inside or outside the class
protected The class member(s) can be accessed from code existing inside the class or inside any derived classes
private The class member(s) can only be accessed from code existing inside the class
Inheritance mode Meaning
public The public and protected inherited members from the base class have the same access modes in the derived class
protected The public and protected inherited members from the base class have protected access in the derived class
private The public and protected inherited members from the base class have private access in the derived class
Class key Default member access mode Default inheritance mode (when used as a derived class)
struct public public
class private private