Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Inheritance syntax

Change example so it doesn’t use the words “base” and “derived.” Too confusing. The syntax for composition is obvious, but to perform inheritance there’s a new and different form.

When you inherit, you are saying, “This new class is like that old class.” You state this in code by giving the name of the class, as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or classes, for multiple inheritance). When you do this, you automatically get all the data members and member functions in the base class. Here’s an example:

//: C14:Inherit.cpp
// Simple inheritance
#include "Useful.h"
#include <iostream>
using namespace std;

class Y : public X {
  int i; // Different from X's i
public:
  Y() { i = 0; }
  int change() {
    i = permute(); // Different name call
    return i;
  }
  void set(int ii) {
    i = ii;
    X::set(ii); // Same-name function call
  }
};

int main() {
  cout << "sizeof(X) = " << sizeof(X) << endl;
  cout << "sizeof(Y) = "
       << sizeof(Y) << endl;
  Y D;
  D.change();
  // X function interface comes through:
  D.read();
  D.permute();
  // Redefined functions hide base versions:
  D.set(12);
} ///:~

In Y you can see inheritance going on, which means that Y will contain all the data elements in X and all the member functions in X. In fact, Y contains a subobject of X just as if you had created a member object of X inside Y instead of inheriting from X. Both member objects and base class storage are referred to as subobjects.

In main( ) you can see that the data elements are added because the sizeof(Y) is twice as big as sizeof(X).

You’ll notice that the base class is preceded by public. During inheritance, everything defaults to private, which means all the public members of the base class are private in the derived class. This is almost never what you want; the desired result is to keep all the public members of the base class public in the derived class. You do this by using the public keyword during inheritance.

In change( ), the base-class permute( ) function is called. The derived class has direct access to all the public base-class functions.

The set( ) function in the derived class redefines the set( ) function in the base class. That is, if you call the functions read( ) and permute( ) for an object of type Y, you’ll get the base-class versions of those functions (you can see this happen inside main( )), but if you call set( ) for a Y object, you get the redefined version. This means that if you don’t like the version of a function you get during inheritance, you can change what it does. (You can also add completely new functions like change( ).)

However, when you’re redefining a function, you may still want to call the base-class version. If, inside set( ), you simply call set( ) you’ll get the local version of the function – a recursive function call. To call the base-class version, you must explicitly name it, using the base-class name and the scope resolution operator.

Contents | Prev | Next


Contact: webmaster@codeguru.com
CodeGuru - the website for developers.
[an error occurred while processing this directive]