Inheritance: The is-a relation

Steven Zeil

Last modified: Feb 19, 2014

Contents:
1. Generalization & Specialization
1.1 Inheritance
1.2 Subtyping
2. Inheritance & Subtyping in C++
2.1 Inheritance Example - Values in a Spreadsheet
3. Overriding Functions
4. Example: Inheritance and Expressions

1. Generalization & Specialization

In an earlier lesson, we introduced the idea of a generalization/specialization relationship between classes.


Specialization Example

For example, a check and a deposit are actually specializations of the more general concept of a “transaction”.

If I were to assert, therefore, that every transaction has a date and an amount, we would understand that the same is true for Checks and Deposits.

Or, from the interface point of view, if I were to assert that every Transaction has a function member apply that takes a Balance as its only parameter, then Checks and Deposits must support the same operation.

1.1 Inheritance

In programming languages, generalization is denoted by inheritance and/or subtyping.

A class C inherits from class D if C has all the data members and messages of D.

D is called a base class of C.


Inheritance Example

This example suggests that Teachers and Students will inherit from University Personnel.

Inheritance Example


Multiple Inheritance

Inheriting from multiple base classes is called multiple inheritance.

It’s reasonably common in domain and analysis models, but designers often try to remove it before they get to the stage of coding. That’s because multiple inheritance can lead to complications.

It’s pretty obvious that the TA’s name is not changed depending on whether they are doing teacher stuff or student stuff at the time. But it’s not hard to imagine that some Universities might use distinct ranges of numbers for teachers than for students, requiring a TA to actually have two different numbers. And, how would you indicate your preference for inheriting one or two copies of a data member in a programming language.

It gets a bit messy, but it can be done in C++. On the other hand, Java disallows multiple inheritance as an unnecessary complication (partly because, as we will see, Java’s interface construct let’s us achieve much of the same flexibility with fewer complications.

1.2 Subtyping

A closely related idea:

D is called a superclass or supertype of C.


What’s the Difference?


Effects of Subtyping and Inheritance

applyToBal.cpp
void applyToCurrentBalance (CheckBook cbook, Transaction trans) {
   Balance b = cbook.getCurrentBalance();
   trans.apply (b);
   cbook.setCurrentBalance(b);
}
   ⋮
CheckBook myCheckBook;
Check check;
Transaction transaction;
Balance bal;
   ⋮
bal = myCheckBook.getCurrentBalance();
transaction.apply (bal);                         ➊
check.apply (bal);                               ➋
applyToCurrentBalance(myCheckBook, transaction); ➌
applyToCurrentBalance(myCheckBook, check);       ➍

For example, the last four statements in the code above are all legal, but the reasons vary:

C++ Combines Inheritance & Subtyping

In most OOPLs, including C++, inheritance and subtyping are combined.

That makes the distinction between inheritance and subtyping moot in C++.

The same does not hold, however, of Java, where only the first two of the four above statements are true.

2. Inheritance & Subtyping in C++

The construct

class C : public Super {

indicates that

2.1 Inheritance Example - Values in a Spreadsheet

cell.h
#ifndef CELL_H
#define CELL_H

#include "cellname.h"
#include "observable.h"
#include "observer.h"
#include "strvalue.h"

class Expression;
class Value;
class SpreadSheet;


// A single cell within a Spreadsheet
class Cell: public Observable, Observer
{
public:
  Cell (SpreadSheet& sheet, CellName name);
  Cell(const Cell&);

  ~Cell();

  CellName getName() const;

  const Expression* getFormula() const;
  void putFormula(Expression*);

  const Value* getValue() const;
  const Value* evaluateFormula();

  bool getValueIsCurrent() const;
  void putValueIsCurrent(bool);


  virtual void notify (Observable* changedCell);

private:
  SpreadSheet& theSheet;
  CellName theName;
  Expression* theFormula;
  Value* theValue;
  bool outOfDate;
  static StringValue defaultValue;
};

#endif

Every cell in the spreadsheet contains

Values

The interface for values is

value.h
#ifndef VALUE_H
#define VALUE_H

#include <string>
#include <typeinfo>

//
// Represents a value that might be obtained for some spreadsheet cell
// when its formula was evaluated.
// 
// Values may come in many forms. At the very least, we can expect that
// our spreadsheet will support numeric and string values, and will
// probably need an "error" or "invalid" value type as well. Later we may 
// want to add addiitonal value kinds, such as currency or dates.
//
class Value
{
public:
  virtual ~Value() {}


  virtual std::string render (unsigned maxWidth) const = 0;
  // Produce a string denoting this value such that the
  // string's length() <= maxWidth (assuming maxWidth > 0)
  // If maxWidth==0, then the output string may be arbitrarily long.
  // This function is intended to supply the text for display in the
  // cells of a spreadsheet.


  virtual Value* clone() const = 0;
  // make a copy of this value

protected:
  virtual bool isEqual (const Value& v) const = 0;
  //pre: typeid(*this) == typeid(v)
  //  Returns true iff this value is equal to v, using a comparison
  //  appropriate to the kind of value.

  friend bool operator== (const Value&, const Value&);
};

inline
bool operator== (const Value& left, const Value& right)
{
  return (typeid(left) == typeid(right))
    && left.isEqual(right);
}

#endif


Numeric Values

Numeric values hold numbers.

numvalue.h
#ifndef NUMVALUE_H
#define NUMVALUE_H

#include "value.h"

//
// Numeric values in the spreadsheet.
//
class NumericValue: public Value
{
  double d;

public:
  NumericValue():d(0.0)  {}
  NumericValue (double x): d(x) {}

  virtual std::string render (unsigned maxWidth) const;
  // Produce a string denoting this value such that the
  // string's length() <= maxWidth (assuming maxWidth > 0)
  // If maxWidth==0, then the output string may be arbitrarily long.
  // This function is intended to supply the text for display in the
  // cells of a spreadsheet.


  virtual Value* clone() const;

  double getNumericValue() const {return d;}

protected:
  virtual bool isEqual (const Value& v) const;
  //pre: typeid() == v.typeid()
  //  Returns true iff this value is equal to v, using a comparison
  //  appropriate to the kind of value.

};

#endif

 void foo (NumericValue nv) {
     cout << nv.render(8) << endl;
 }
 
void foo (Value v; NumericValue nv) {
    if (v == nv) {
       ⋮


String Values

String values hold numbers.

strvalue.h
#ifndef STRVALUE_H
#define STRVALUE_H

#include "value.h"

//
// String values in the spreadsheet.
//
class StringValue: public Value
{
  std::string s;
  static const char* theValueKindName;

public:
  StringValue()  {}
  StringValue (std::string x): s(x) {}


  virtual std::string render (unsigned maxWidth) const;
  // Produce a string denoting this value such that the
  // string's length() <= maxWidth (assuming maxWidth > 0)
  // If maxWidth==0, then the output string may be arbitrarily long.
  // This function is intended to supply the text for display in the
  // cells of a spreadsheet.


  std::string getStringValue() const {return s;}

  virtual Value* clone() const;



protected:
  virtual bool isEqual (const Value& v) const;
  //pre: valueKind() == v.valueKind()
  //  Returns true iff this value is equal to v, using a comparison
  //  appropriate to the kind of value.

};

#endif


Error Values

Error values store no data at all, but are used as placeholders in a cell whose calculations have failed for some reason.

errvalue.h
#ifndef ERRVALUE_H
#define ERRVALUE_H

#include "value.h"

//
// Erroneous/invalid values in the spreadsheet.
//
class ErrorValue: public Value
{
  static const char* theValueKindName;

public:
  ErrorValue()  {}

  virtual std::string render (unsigned maxWidth) const;
  // Produce a string denoting this value such that the
  // string's length() <= maxWidth (assuming maxWidth > 0)
  // If maxWidth==0, then the output string may be arbitrarily long.
  // This function is intended to supply the text for display in the
  // cells of a spreadsheet.


  virtual Value* clone() const;

protected:
  virtual bool isEqual (const Value& v) const;
  //pre: valueKind() == v.valueKind()
  //  Returns true iff this value is equal to v, using a comparison
  //  appropriate to the kind of value.

};

#endif

3. Overriding Functions

When a subclass inherits a function member, it may


Overriding: Declaring Your Intentions


class A {
public:
  void foo();
  void bar();
  void baz();
};

class B: public A {
public:
  void foo();       ➊
  void bar(int k);  ➋
  void bar() const; ➌
};                  ➍



Access to Functions

B b;
b.foo();     // calls B::foo()
b.A::foo();  // calls the original A::foo()

void B::foo()
{
  A::foo();
  doSomethingExtra();
}


Example of Overriding

As an example of overriding, consider these four classes, which form a small inheritance hierarchy.

animalOv.cpp
class Animal {
public:
  String eats() {return "food";}
  String name() {return "Animal";}
};

class Herbivore: public Animal {
public:
   String eats() {return "plants";}
   String name() {return "Herbivore";}
};

class Ruminants: public Herbivore {
public:
   String name() {return "Ruminant";}
};

class Carnivore: public Animal {
public:
   String name() {return "Carnivore";}
};

void show (String s1, String s2) {
    cout << s1 << " " << s2 << endl;
}

Note that several of the inheriting classes override one or both functions in their base class.

Question: Now, suppose we run the following code. What will be printed by each of the show calls?

 Animal a;
 Carnivore c;
 Herbivore h;
 Ruminant r;
 show(a.name(), a.eats());        // AHRC fpgm
 show(c.name(), c.eats());        // AHRC fpgm
 show(h.name(), h.eats());        // AHRC fpgm
 show(r.name(), r.eats());        // AHRC fpgm

(Try to work this out for yourself before looking ahead.)

Answer:


Inheritance and Encapsulation

An inheriting class does not get access to private data members of its base class:

class Counter {
   int count;
public:
   Counter() {count = 0;}
   void increment() {++count;}
   int getC() const {return count;}
};

class HoursCounter: public Counter {
public:
   void increment() {
     counter = (counter + 1) % 24; // Error!
   }
};


Protected Members

Data members marked as protected are accessible to inheriting classes but private to all other classes.

class Counter {
protected:
   int count;
public:
   Counter() {count = 0;}
   void increment() {++count;}
   int getC() const {return count;}
};

class HoursCounter: public Counter {
public:
   void increment() {
     counter = (counter + 1) % 24; // OK
   }
};

4. Example: Inheritance and Expressions

Inheritance also plays a part in the spreadsheet in taking care of Expressions


Expression Trees


Expression Inheritance Hierarchy

We would expect the lower-level classes like PlusNode and TimesNode to override the evaluate function to do addition, multiplication, etc., or whatever it is that distinctively identifies that particular “kind” of expression from all the other possibilities.