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


Inheritance

 

Answer the following questions about the program shown on the right,

  1. Which is the base class?

  2. Which is the derived class?

  3. How many data members does "myAlarm" have?

  4. Is is necessary to use the constructor initializer in the constructors for "AlarmClock"? (y/n)

  5. Is the following statement legal (y/n)
    cout << myAlarm.getAlarmHour( );

  6. Is the following statement legal (y/n)
    cout << myAlarm.getHour( );

 

Choose one when done
 

 
 
class Time {
public:
   Time( int = 0, int = 0, int = 0 );  // default constructor

   // set functions
   void setTime( int, int, int );  // set time
   void setHour( int );     // set hour
   void setMinute( int );   // set minute
   void setSecond( int );   // set second

   // get functions (normally declared const)
   int getHour() const;     // return hour
   int getMinute() const;   // return minute
   int getSecond() const;   // return second

   // print functions (normally declared const)
   void printMilitary() const;  // print military time
   void printStandard() const;  // print standard time
private:
   int hour;              // 0 - 23
   int minute;            // 0 - 59
   int second;            // 0 - 59
};


//This examples adds an alarm feature to the "Time" class in figure 7.1
// Here is the header file (see also this topic)
class AlarmClock : public Time {
public:
   AlarmClock(int hour = 0, int minute = 0, int second = 0);
   AlarmClock(int hour, int minute, int second,
	int alarmHour, int alarmMinute, bool alarmSet);
   void setAlarmTime(int hour, int minute);
   int getAlarmHour( ) const;
   int getAlarmMinute( ) const;
   void setAlarm(bool state);
   bool getAlarm( ) const;

private:
   int alarmHour; 
   int alarmMinute;
   bool alarmState;
};
AlarmClock myAlarm;



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.