2.5k views
0 votes
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/2018

December 25, 2018

25 December 2018

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 monnth greater than 12 or less than 1.

*This is in C++ language and needs to be compatible with visual studio

User Dhobbs
by
7.4k points

1 Answer

5 votes

Final answer:

To design the class called Date, you can start by defining three private member variables: month, day, and year. Then you can create constructor functions to initialize these variables and member functions to print the date in different formats. Demonstrating the class can be done by writing a complete program that includes the main function which creates an instance of the Date class and calls the member functions to print the date in various formats.

Step-by-step explanation:

To design the class called Date, you can start by defining three private member variables: month, day, and year. Then you can create constructor functions to initialize these variables and member functions to print the date in different formats. Here is an example implementation:

#include <iostream>
#include <string>

using namespace std;

class Date {
private:
int month;
int day;
int year;

public:
Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}

void printDate1() {
cout << month << "/" << day << "/" << year << endl;
}

void printDate2() {
string monthName;
switch(month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
//more cases for other months
}
cout << monthName << " " << day << ", " << year << endl;
}

void printDate3() {
string monthName;
switch(month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
//more cases for other months
}
cout << day << " " << monthName << " " << year << endl;
}
};

int main() {
Date date(12, 25, 2018);
date.printDate1();
date.printDate2();
date.printDate3();

return 0;
}

User Estn
by
8.6k points

No related questions found