A data type whose properties (domain and operations) are specified independently of any particular implementation.
class TimeType
{
public: // Public member functions
void Set( /* in */ int hours,
/* in */ int minutes,
/* in */ int seconds );
void Increment();
void Write();
private: // Private data, default
int hrs;
int mins;
int secs;
};
Class members -- data / functions
TimeType T1, T2; class -- type class instance -- object
| member selection | . | T1.Increment(); |
| assignment | = | T2 = T1; |
The class declaration serves as the public interface to the user as well as the specification describing the behavior of the data type.
Specification file: timetyp1.h
The implementation contains all the function definitions for the class member functions and creates an abstraction barrier by hiding the concrete data representations as well as the code for the operations.
Implementation file: timetyp1.cpp
TimeType::void Increment()
{
...
};
members of a class refer to each other without using dot notation.
TestTime program: testtim1.cpp
Software Tools
The TimeType class depends on the client to invoke the set function before calling any other member function.
void Increment();
// Precondition:
// The Set function has been invoked at least once
A constructor is a member function that is implicitly invoked whenever a class object is created.
Constructors declaration
class TimeType { public: ... TimeType( int, int, int ); TimeType(); private: ... };Constructors implementation
TimeType::TimeType( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) { hrs = initHrs; mins = initMins; secs = initSecs; }
TimeType::TimeType()
{
hrs = 0;
mins = 0;
secs = 0;
}
TimeType lectureTime(14, 10, 0); TimeType startTime;
SomeClass arr[10]; then one of the constructors must be the default constructor. This constructor is invoked for each element of the array.
A destructor is implicitly invoked when a class object is destroyed.
class TimeType
{
public:
...
TimeType( int, int, int );
TimeType();
~TimeType();
private:
...
};