Question: Get all three files (main.cpp, cdate.h and cdate.cpp) together in a multi-module project so it will build and run. (see main.cpp below) *Using Overloaded Operators*
Get all three files (main.cpp, cdate.h and cdate.cpp) together in a multi-module project so it will build and run. (see main.cpp below) *Using Overloaded Operators*
1. The main module is essentially a test driver program to exercise a class called CDate. But where does this class exist? Well, I'm afraid you're going to have to implement it!
2. You'll need to carefully study the main driver program and observe what the class does in the code, and run the sample executable to confirm your observations.
3. Look at every CDate variable and the functions it's calling, and don't forget - those objects are going to be calling constructors when they're allocated in memory too!
*Note that the CDate::SetMonth, CDate::SetDay and CDate::SetYear member functions return boolean values.
- That's because for this assignment we'll set some arbitrary data validation rules:
1) a month must be a positive value between 1 and 12 (inclusive)
2) a day must be a positive value between 1 and 31 (inclusive)
3) and a year must be a positive four-digit value.
- So, each of these member functions should validate the data passed in through the formal parameters.
--IF-- the incoming data is determined to be valid, you can make the assignment to the private data member and return a value of true;
--ELSE-- do not make the assignment and return a value of false.
*Naturally, since you'll have "Set" functions to set the month, day and year, it only makes sense to provide "Get" functions that allow a caller to get those values as well, right?
4. This assignment will also give you a chance to experiment with overloading constructors. Look at the main driver program to see how these constructors are invoked.
5. You might not only wish to review your notes from class, but also look in your book (see Chapter 10).
6. Study the code in main.cpp carefully and compare it with what you see when you run the sample executable. You should be able to observe what each type of constructor is supposed to do.
--------------------- main.cpp -------------------
// ============================================================================ // File: main.cpp // ============================================================================
#include #include #include using namespace std; #include "cdate.h"
const int BUFLEN = 256;
// ==== main ================================================================== // // ============================================================================
int main() { // create a default CDate object CDate date1;
// display the default date object cout << "Here's the default date: "; date1.DispDate(); cout << endl;
// get month, day and year values from the user bool bValidMonth = false; bool bValidDay = false; bool bValidYear = false; int userMonth; int userDay; int userYear; char buf[BUFLEN];
do {
if (bValidMonth == false) { cout << "Please enter a month value: "; cin.getline(buf, BUFLEN);
// NOTE: -- the "atoi" function means "ascii-to-int". It takes as // input a null-terminated cstring that represents an integer, // converts it into an integer value, then returns that integer // to the caller. It's a standard library function so you don't // have to write it, just #include userMonth = atoi(buf); bValidMonth = date1.SetMonth(userMonth);
if (bValidMonth == false) { cout << "Sorry, that's an invalid month; try again... ";
// NOTE: the 'continue' statement just causes program flow // to go immediately to the next loop iteration continue; } }
if (bValidDay == false) { cout << "Please enter a day value: "; cin.getline(buf, BUFLEN); userDay = atoi(buf); bValidDay = date1.SetDay(userDay);
if (bValidDay == false) { cout << "Sorry, that's an invalid day; try again... "; continue; } }
if (bValidYear == false) { cout << "Please enter a year value: "; cin.getline(buf, BUFLEN); userYear = atoi(buf); bValidYear = date1.SetYear(userYear == false);
if (bValidYear == false) { cout << "Sorry, that's an invalid year; try again... "; continue; } }
} while ((bValidMonth == false) || (bValidDay == false) || (bValidYear == false));
// display the date the user entered cout << "Here's the date you entered: "; date1.DispDate(); cout << endl;
// use the copy constructor to make a copy of date1 and display it CDate date2(date1); cout << "Here's a copy of the new date: "; date2.DispDate(); cout << endl;
// use an existing year value to try out the type conversion constructor CDate date3(date2.GetYear()); cout << "Here's the first day of that year: "; date3.DispDate(); cout << endl; return 0;
} // end of "main"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
