Section 1 - Introduction to Inheritance: Base Classes

4A.1.1 Orientation

This week we introduce inheritance, the mechanism that allows us to reuse old, or existing, classes in new applications.  Inheritance is universal to all contemporary object-oriented programming languages, including C++, Java and C#.  The notation is slightly different in each language, but if you understand the concept in one, you will understand it in all.

Most large programming projects rely on inheritance.  Classes defined by one team, or programmers now long gone, have been completely debugged and tested.  You would extend those classes (using inheritance) to create new functionality, without going into the source code of the original base classes.

scenic chessboard

4A.1.2 Reading and Resources

All the resources needed for this week and instructions on textbook reading have already been posted in module A1 of the first three weeks. Refer to those pages and links, regularly.

4A.1.3 A Base Class

A base class is just a class, the same way a parent is just a person. It is the creation of a second entity (daughter or son), that makes the person a parent.

This class is a base class, because I know that it is about to become a parent. We are going to derive a new class from it, which, without modification to the class itself, turns it into a semantic entity known as a base class.

Any class is a base class if you derive a new class (called the derived or sub class) from it.

We create an ordinary class, Phone. It will store an area code and phone number in two separate members, and manage them accordingly. The constructor for Phone will initialize the members so there is no chance of garbage cluttering them up.   Since the class is so straightforward, I'll list it inside the complete program, to make it easy for you to copy.  The most interesting aspect of it is the way the setPhone() method is defined.  I hope you will read it, try it out, and understand it in preparation for the next part.

#include <iostream>
#include <string>
using namespace std;

// Class Phone Prototype -----------------------------
class Phone {
  string areaCode;
  string number;

public:
  Phone(string ac = "000", string num = "0000000");
  bool setPhone(string ac = "000", string num = "0000000");
  string getAreaCode();
  string getNumber();
  string toString();
  void showPhone();
  protected:
  static bool isNumber(string s);
};

// main method ---------------------------------------
int main()
{
  Phone me("800", "1234567"), you;

  you.setPhone("415", "5551234");
  me.showPhone();
  you.showPhone();

  return 0;
}


// Phone method definitions --------------------------
Phone::Phone(string ac, string num)
{
  if ( !setPhone(ac, num) )
    setPhone("000", "0000000");  // use defaults if bad values passed
}

bool Phone::setPhone(string ac, string num)
{
  if (ac.length() == 3 && isNumber(ac)
  && num.length() == 7 && isNumber(num) )
  {
    number = num;
    areaCode = ac;
    return true;
  }
  return false;  // don't change anything if bad values
}

string Phone::getAreaCode()
{
  return areaCode;
}

string Phone::getNumber()
{
  return number;
}

string Phone::toString()
{
  string result;
  result = "(" + areaCode + ")"
  + number.substr(0,3) + "-"
  + number.substr(3,4);
  return result;
}

void Phone::showPhone()
{
  cout << toString() << endl;
}
// static class method for testing whether a string is a number
bool Phone::isNumber(string s)
{
  for(int k = 0; k < s.length(); k++)
    if (!isdigit(s[k]))
      return false;
  return true;
}

// end Phone method definitions ---------------------

Two things to note:

  1. We have a static class method isNumber().  Because this method is a utility that takes a string and there is no specific Phone object to which it belongs, it is natural to make it a static method.  It can be used (and is in the above) to validate a string, testing whether it contains all numeric digits.
  2. isNumber() is neither public nor private, but a new category called protectedProtected is like private from the point of view of the client: the client may not access it.  But derived classes (which we are about to introduce) that are based on Phone can use this method.  So protected means it is accessible from the class and its derived classes, but not from other, external classes.  This can be used for member data or member functions.

Here is a copy of the run:

console shot