Inheritance

Inheritance between classes

An important feature of classes is inheritance. This allows us to create an object derived from another one, so that it may include some of the other's members plus its own. For example, we are going to suppose that we want to declare a series of classes that describe polygons like our CRectangle, or CTriangle. They have certain common features, such as both can be described by means of only two sides: height and base.

This could be represented in the world of classes with a class CPolygon from which we would derive the two referred ones, CRectangle and CTriangle.

The class CPolygon would contain members that are common for all polygons. In our case: width and height. And CRectangle and CTriangle would be its derived classes.

Classes derived from others inherit all the visible members of the base class. That means that if a base class includes a member A and we derive it to another class with another member called B, the derived class will contain both A and B.

In order to derive a class from another, we must use the operator : (colon) in the declaration of the derived class in the following way:

class derived_class_name: public base_class_name;

where derived_class_name is the name of the derived class and base_class_name is the name of the class on which it is based. public may be replaced by any of the other access specifiers protected or private, and describes the access for the inherited members.

// derived classes Output #include <iostream.h> 20 class CPolygon { 10 protected: int width, height; public: void set_values (int a, int b)

{ width=a; height=b;} }; class CRectangle: public CPolygon { public: int area (void) { return (width * height); } }; class CTriangle: public CPolygon { public: int area (void) { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }

As you may see, objects of classes CRectangle and CTriangle each contain members of CPolygon,

that are: width, height and set_values(). The protected specifier is similar to private, its only

difference occurs when deriving classes. When we derive a class, protected members of the base class

can be used by other members of the derived class, nevertheless private member cannot. Since we

wanted width and height to have the ability to be manipulated by members of the derived classes

CRectangle and CTriangle and not only by members of CPolygon, we have used protected access

instead of private.

We can summarize the different access types according to whom can access them in the following way:

where "not-members" represent any reference from outside the class, such as from main(), from another class or from any function, either global or local.

In our example, the members inherited by CRectangle and CTriangle follow with the same access permission as in the base class CPolygon:

CPolygon::width // protected accessCRectangle::width // protected accessCPolygon::set_values() // public accessCRectangle::set_values() // public accessThis is because we have derived a class from the other as public, remember:

class CRectangle: public CPolygon;

this public keyword represents the minimum level of protection that the inherited members of the

base class (CPolygon) must acquire in the new class (CRectangle). The minimum access level for

the inherited members can be changed by specifying protected or private instead of public.

For example, daughter is a class derived from mother that we defined thus:

class daughter: protected mother;

this would establish protected as the minimum access level for the members of daughter that it i

nherited from mother. That is, all members that were public in mother would become protected

in daughter, that would be the minimum level at which they can be inherited. Of course, this would

not restrict that daughter could have its own public members. The minimum level would only be

established for the inherited members of mother.

The most common use of an inheritance level different from public is private that serves to

completely encapsulate the base class, since, in that case, nobody except its own class will be able

to access the members of the base class from which it is derived. Anyway, in most cases classes

are derived as public.

If no access level is explicitly written private is assumed for classes created with the class

keyword and public for those created with struct.

HOME LEARN C++ PREV NEXT