Question: When your program runs, the user should be prompted to enter values for the month, day and year, storing them in local integer variables. These
When your program runs, the user should be prompted to enter values for the month, day and year, storing them in local integer variables. These values are to be sent as arguments to the MakeDate function. The MakeDate function will create a dynamic CDate object from the heap and attempt to assign the month, day and year values to the object. If all three assignments are successful, a pointer to the CDate object is returned to the caller, otherwise a NULL pointer is returned. This will let the caller know if a valid CDate object was actually created or not.
(NOTE: The NULL pointer has a value of zero, and when a pointer has a value of NULL it means that the pointer isn't pointing to anything legitimate. All you need to do is assign NULL, which is a predefined constant, to a pointer to indicate it's not pointing to anything, and then at a later point in your code you can write a boolean expression to test is the pointer has a NULL value. If you try to dereference a pointer with a value of NULL, you'll produce a segmentation fault! You can read more about NULL pointers on p.746 in the textbook.)
If the caller determines that a valid CDate object wasn't created, an error message should be written to alert the user and the program should end; otherwise, the pointer to the CDate object should be passed to the ShowDate function, which writes the date information to stdout in MM/DD/YYYY format by calling the CDate::DispDate member function.
Once ShowDate returns to the caller, the dynamic CDate object is displayed one more time from the main function by using the overloaded insertion operator (you'll have to write this function, the prototype is already in the cdate.h header file). After that occurs, the date object should be released from the heap before the program ends through the destructor.
As a reminder, youll need 3 total files to turn in: main.cpp, cdate.h, and cdate.cpp
// ============================================================================ // File: cdate.h // ============================================================================
#ifndef CDATE_H #define CDATE_H
#include
class CDate { public: // constructors CDate(); CDate(const CDate &other); CDate(int year);
// member functions void DispDate(ostream &outStream = cout) const; int GetDay() const; int GetMonth() const; int GetYear() const; bool SetDay(int day); bool SetMonth(int month); bool SetYear(int year);
private: int m_month; int m_day; int m_year; };
// overloaded insertion operator ostream& operator<<(ostream &outStream, const CDate &rhs);
#endif // CDATE_H
// ============================================================================ // File: main.cpp // ============================================================================
#include
// function prototypes CDate* MakeDate(int mVal, int dVal, int yVal); void ShowDate(const CDate *datePtr);
// ==== main ================================================================== // // ============================================================================
int main() { CDate *myDatePtr; int monthVal; int dayVal; int yearVal;
// get the month, day and year from the user cout << "Please enter a month, day and year: "; cin >> monthVal >> dayVal >> yearVal;
// call MakeDate to create a date and get a pointer to it; if it's NULL // display an error message and terminate the program
// call ShowDate to display the heap date object's date //???
// write the heap date to stdout using the overloaded insertion operator //???
// release the date object back to the heap //???
return 0;
} // end of "main"
// ==== MakeDate ============================================================== // // This function recieves month, day and year arguments from the caller and // uses them to create a CDate object from the heap. The object is allocated // from the heap, and if all of three of the integer parameters are // successfully assigned to the CDate object, a pointer to the object is // returned to the caller. But if any of the assignments fail, a NULL pointer // is returned to the caller. // // Input: // mVal [IN] -- the integer month value // // dVal [IN] -- the integer day value // // yVal [IN] -- the integer year value // // Output: // If all three parameter values are successfully assigned to the heap // CDate object, a pointer to that object is returned, else a NULL pointer // is returned. // // ============================================================================
CDate* MakeDate(int mVal, int dVal, int yVal) { //???
} // end of "MakeDate"
// ==== ShowDate ============================================================== // // This function receives a pointer to a CDate object and uses it to write its // date to the standard output stream using the CDate::DispDate member function. // // Input: // datePtr [IN] -- a pointer to a CDate object // // Output: // Nothing. // // ============================================================================
void ShowDate(const CDate *datePtr) { //???
} // end of "ShowDate"
Step by Step Solution
There are 3 Steps involved in it
The question is complete Heres a detailed stepbystep solution on how you can implement the program a... View full answer
Get step-by-step solutions from verified subject matter experts
