Question: A basic class dateType is designed to implement the date in a program, but the member function setDate does not check whether the date is
A basic class dateType is designed to implement the date in a program, but the member function setDate does not check whether the date is in valid format. a. Rewrite the definition of the function setDate so that the values for the month (1 to 12), day (1 to 31), and year (4 digits) are checked before storing the date into the member variables. b. Add a member function, isLeapYear, to check whether a year is a leap year.
there should be three files, dateType.h dateTypeImp.cpp main.cpp
dateType.h
#ifndef dateType_H #define dateType_H class dateType { public: // check the format of month, day and year. // if the format is incorrect, displays a message and returns false // if the format is correct, returns true bool setDate(int month, int day, int year); //determines whether its a leap year or not bool isLeapYear() const; // returns current day int getDay() const; // returns current month int getMonth() const; // returns current year int getYear() const; // prints the current date void printDate() const; dateType(); private: int dMonth; int dDay; int dYear; }; #endif
dateTypeImp.cpp
//Implementation file date #include#include "dateType.h" using namespace std; void dateType::setDate(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } int dateType::getDay() const { return dDay; } int dateType::getMonth() const { return dMonth; } int dateType::getYear() const { return dYear; } void dateType::printDate() const { cout << dMonth << "-" << dDay << "-" << dYear; } dateType::dateType() { dMonth = 1; dDay = 1; dYear = 1990; }
main.cpp
#include#include "dateType.h" using namespace std; int main() { int Month, Day, Year; while (true) { Month = 0; Day = 0; Year = 0; cout << "Month: "; cin >> Month; cout << "Day: " ; cin >> Day; cout << "Year: "; cin >> Year; dateType dt; if (dt.setDate(Month, Day, Year)){ dt.printDate(); cout << endl << "Leap year = " << dt.isLeapYear() << endl; break; } else cout << endl<<"Please enter proper date format"<
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
