Question
Design a class called Date. The class should store a date in three integers: month, day, and year. There should be member functions to print the date in the following forms: 12/25/2014 December 25, 2014 25 December 2014 Demonstrate the class by writing a complete program implementing it. Input Validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1.
Solutions
VerifiedSolution A
Solution B
Answered 3 months ago
Step 1
1 of 3//contents of Date.h
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <string>
using namespace std;
class Date
{
private:
int month, day, year;
//array of month names
string monthNames[12] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
public:
//constructor
Date(int m = 1, int d = 1, int y = 1970){
//check invalid input for month
if(m < 1 || m > 12){
cout << "ERROR! Month can only be between 1-12!";
cout << " Now terminating!\n";
exit(EXIT_FAILURE);
}
//check invalid input for day
if(d < 1 || d > 31){
cout << "ERROR! Day can only be between 1-31!";
cout << " Now terminating!\n";
exit(EXIT_FAILURE);
}
month = m;
day = d;
year = y;
}
//getter functions
int getMonth(){
return month;
}
int getDay(){
return day;
}
int getYear(){
return year;
}
//setter functions
void setMonth(int m){
//check for invalid input
if(m < 1 || m > 12){
cout << "ERROR! Month can only be between 1-31!";
cout << " Now terminating!\n";
exit(EXIT_FAILURE);
}
else{
month = m;
}
}
void setDay(int d){
//check for invalid input
if(d < 1 || d > 31){
cout << "ERROR! Day can only be between 1-12!";
cout << " Now terminating!\n";
exit(EXIT_FAILURE);
}
else{
day = d;
}
}
void setYear(int y){
year = y;
}
//printing functions
void format1(){
cout << month << "/" << day << "/" << year;
cout << endl;
}
void format2(){
//don't forget that index of monthNames array
//starts from 0
cout << monthNames[month-1] << " ";
cout << day << ", " << year;
cout << endl;
}
void format3(){
//don't forget that index of monthNames array
//starts from 0
cout << day << " " << monthNames[month-1];
cout << " " << year;
cout << endl;
}
};
#endif
Answered 1 year ago
Step 1
1 of 4Create an account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Create an account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface
5th EditionDavid A. Patterson, John L. Hennessy220 solutions

Starting Out with C++ from Control Structures to Objects
8th EditionGodfrey Muganda, Judy Walters, Tony Gaddis1,294 solutions


Introduction to Algorithms
3rd EditionCharles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen726 solutions