Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Which formal parameter is included in every member function interface? Why? How

ID: 2246487 • Letter: W

Question

Which formal parameter is included in every member function interface? Why? How is and why is the DATE *date formal parameter for an accessor function different from the formal parameter for a mutator function? Hint What does the const mean?

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

The formal parameter common to all the functions is Data *date. it is present in each function prototype as one of the formal parameter.
In the case of accessor function const key word is used with this formal parameter, this just prohibits any change in the structre pointed by this formal parameter inside the accessor function. It is true also becuase in an accessor function we don't need to change the structure rather we just need to return its repective fields depending on the accessor function. But in case of mutator function we can not have const keyword because we need to change the structure in the mutator function as that's what mutatotor function are meant for.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote