Object-Oriented Software Design
OOP vs C++
| OOP |
C++ |
| Object |
Class object or instance |
| Instance variable |
Private data member |
| Method |
Public member function |
| Message passing |
Function call (to a public member function) |
// exttime.h
enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT};
class ExtTime : public Time // derived class
{
public:
void Set( int hours, int minutes, int seconds, ZoneType timeZone );
void Write() const;
ExtTime( int initHrs, int initMins, int initSecs, ZoneType initZone );
ExtTime(); // Default constructor
// setting time to 0:0:0 EST
private:
ZoneType zone;
};
Constructor Initiator
ExtTime::ExtTime( int, int, int, ZoneType )
: Time(initHrs, initMins, initSecs) // use base class Time
Implementation file: exttime.cpp
TestTime program
testextt.cpp
Using Constructors and Destructors in Derived Classes
// FIG9_7.CPP
// Demonstrate when base-class and derived-class
// constructors and destructors are called.
#include <iostream.h>
#include "point2.h"
#include "circle2.h"
main()
{
// Show constructor and destructor calls for Point
{
Point p(1.1, 2.2);
}
cout << endl;
Circle circle1(4.5, 7.2, 2.9);
cout << endl;
Circle circle2(10, 5, 5);
cout << endl;
return 0;
}
Case Study
Multiple Inheritance
#include "base1.h"
#include "base2.h"
class Derived : public Base1, public Base2
{
public:
...
private:
...
};