CS333 - Problem Solving and Object Oriented Programming in C++
[ Home | Schedule | Personal Progress | Search | Glossary | Help ]


 Simple Explanation

 Intro to Inheritance

 Advanced Explanation

  Test your Understanding

  DESCRIPTION
  • One of the significant advantages of adopting an object-oriented programming langauge is the ability to reuse previously defined objects.
  • Problem: what if previously defined class is close to, but not exactly, what you need.
    You could of course cut and paste the source code (assuming if you had access to it)
    Or you could use class inheritance.
  • Inheritance is a mechanism whereby you can define a new class (called the derived class) from an existing class (called the base class).
  • With inheritance, you can reuse (inherit) any and all base class member functions and data members.
    You can also add new data and function members.
    And you can modify any base class functions as required.
  • The base class is sometimes referred to as the parent class and the derived class is then referred to as the child class
    (this motivates the term inheritance of child from parent).
  • Because the derived class usually changes the base class to specialize its use, the base class is sometimes referred to as the general class and the derived class as the specialized class.
  • The relationship between a derived class and a base class is an "Is-A" relationship since the derived class "is a " kind of base class.
EXAMPLES 
Click here for more examples 
//This examples adds an alarm feature to the "Time" class in figure 7.1
// Here is the header file
#include "time5.h" // definition of base class

class AlarmClock : public Time { // AlarmClock inherits basic clock features from Time base class
public:
		// constructors
		// next constructor just sets time not alarm
		// we will pass these parameters through to base class
	AlarmClock(int hour = 0, int minute = 0, int second = 0);
		// next constructor just sets time and alarm
		// first three parameters passed to base class
		// and last three used here
	AlarmClock(int hour, int minute, int second,
		int alarmHour, int alarmMinute, bool alarmSet);
		// sets alarm time and switches alarm on
	void setAlarmTime(int hour, int minute);
		// return the hour that alarm is set to
	int getAlarmHour( ) const; // don't forget to distinguish "const" functions
		// return the minute the alarm is set to
	int getAlarmMinute( ) const;
		// state of alarm is set to true for on or false for off
	void setAlarm(bool state);
	bool getAlarm( ) const;

private: // already have three data members from base class
	// add new ones to handle new features
	int alarmHour; 
	int alarmMinute;
	bool alarmState; // if true, alarm set, else not set
};
// just show unusual code in implementation - click c++.gif (1085 bytes) to get full source
AlarmClock::AlarmClock(int hour, int minute, int second)
: Time(hour, minute, second) // construct base class object hptrd_left.gif (955 bytes) 1
{
	alarmHour = 0;
	alarmMinute = 0;
	alarmState = false; // alarm is not set
}

// next constructor just sets time and alarm
AlarmClock::AlarmClock(int hour, int minute, int second,
		int aHour, int aMinute, bool aState)
		: Time(hour, minute, second),
		alarmHour(aHour), alarmMinute(aMinute), alarmState(aState)
{ } // all work done in constructor initializer hptrd_left.gif (955 bytes) 2
rainbow.gif (2251 bytes)
  1. Notice use of constructor initializer. Since the private members of a base class are not accessible, even from a derived class, you must either use this method OR use the setTime function.
  2. In this constructor, all data members can be set upon construction. This example uses the constructor initializer for all data members.
    This is not necessary for the derived classes data members. You could have used assignment statements as in the previous constructor.
    Just to show another way to initialize variables
    • Uses the alternate syntax for initializing integers. the following two statements are equivalent
      int i = 3;
      int i(3);
SYNTAX  
class derived-class-name : public base-class-name
 
TIPS
  • There are many object libraries available (Microsoft Foundation Classes (MFC), Object Windows Library (OWL), Standard Template Library (STL)) which support reuse of complex base classes through inheritance and templates.
    These libraries are in wide spread use and dramtically speed up development of software projects.

 


Copyright chris wild 1999.
For problems or questions regarding this website contact [Chris Wild (e-mail:cs333@cs.odu.edu].
Last updated: November 27, 1999.