The following statement is an example of instrumentation [1]. Since nothing seem
ID: 2246490 • Letter: T
Question
The following statement is an example of instrumentation[1]. Since nothing seems to be gained by instrumenting the DATE constructor and destructor, why do you think Dr. Hanna went to the trouble?
cout << "Date construction of "; OutputDate(date); cout << endl;
C++ Code is already completed below:
//-------------------------------------------------
// Dr. Art Hanna
// Date.h
//-------------------------------------------------
#ifndef DATE_H
#define DATE_H
struct DATE
{
int MM;
int DD;
int YYYY;
};
void ConstructDate(DATE *date);
void DestructDate(DATE *date);
void InputDate(DATE *date);
void OutputDate(const DATE *date);
void SetDateMM(DATE *date,int MM);
void SetDateDD(DATE *date,int DD);
void SetDateYYYY(DATE *date,int YYYY);
int GetDateMM(const DATE *date);
int GetDateDD(const DATE *date);
int GetDateYYYY(const DATE *date);
#endif
//-------------------------------------------------
// Dr. Art Hanna
// Date.cpp
//-------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include ".Date.h"
using namespace std;
//-------------------------------------------------
void ConstructDate(DATE *date)
//-------------------------------------------------
{
date->DD = 12; date->MM = 1; date->YYYY = 1993;
cout << "Date construction of "; OutputDate(date); cout << endl;
}
//-------------------------------------------------
void DestructDate(DATE *date)
//-------------------------------------------------
{
cout << "Date destruction of "; OutputDate(date); cout << endl;
}
//-------------------------------------------------
void InputDate(DATE *date)
//-------------------------------------------------
{
cout << " MM? "; cin >> date->MM;
cout << " DD? "; cin >> date->DD;
cout << "YYYY? "; cin >> date->YYYY;
}
//-------------------------------------------------
void OutputDate(const DATE *date)
//-------------------------------------------------
{
// Use the format MM-DD-YYYY
cout << setw(2) << date->MM << "-" << setw(2) << date->DD << "-" << setw(4) << date->YYYY;
}
//-------------------------------------------------
void SetDateMM(DATE *date,int MM)
//-------------------------------------------------
{
date->MM = MM;
}
//-------------------------------------------------
void SetDateDD(DATE *date,int DD)
//-------------------------------------------------
{
date->DD = DD;
}
//-------------------------------------------------
void SetDateYYYY(DATE *date,int YYYY)
//-------------------------------------------------
{
date->YYYY = YYYY;
}
//-------------------------------------------------
int GetDateMM(const DATE *date)
//-------------------------------------------------
{
return( date->MM );
}
//-------------------------------------------------
int GetDateDD(const DATE *date)
//-------------------------------------------------
{
return( date->DD );
}
//-------------------------------------------------
int GetDateYYYY(const DATE *date)
//-------------------------------------------------
{
return( date->YYYY );
}
//-------------------------------------------------
// Dr. Art Hanna
// Chapter #3 Problem, Part 1
// Problem.cpp
//-------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include ".Date.h"
using namespace std;
//-------------------------------------------------
int main()
//-------------------------------------------------
{
DATE date1,date2;
ConstructDate(&date1);
ConstructDate(&date2);
SetDateMM(&date1,1);
SetDateDD(&date1,30);
SetDateYYYY(&date1,1979);
OutputDate(&date1); cout << endl;
cout << "date2? " << endl; InputDate(&date2);
// OMG! An example of the violation of the principle of information hiding!!!
cout << setw(2) << date2.MM << "/"
<< setw(2) << GetDateDD(&date2) << "/"
<< setw(4) << GetDateYYYY(&date2);
cout << endl;
DestructDate(&date2);
DestructDate(&date1);
system("PAUSE");
return( 0 );
}
Explanation / Answer
Since here, DATE is a struct which contains only primitive data such as integers.. So currently the constructors or destructors are not adding much value, but suppose if the structure Date would have contained some dynamically allocated object, then in that case, we would need to create the object from heap using new operator and at the last of the program, we must free that object using delete operator so that memory is released. To do these operations, it is good to have constructors and destructors block.