Hello guys ! Could you let me know about this in C++ language ??! Thank you so m
ID: 3832638 • Letter: H
Question
Hello guys ! Could you let me know about this in C++ language ??! Thank you so much have a good day !!
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 .
Explanation / Answer
#include <iostream>
using namespace std;
class date{
private:
int month, day, year;
public:
date(int m, int d, int y){
month = m;
day = d;
year = y;
}
void printMMDDYYYY() {
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
void printMonthDDYYYY() {
cout<<getMonth(month)<<" "<<day<<", "<<year<<endl;
}
void printDDMonthYYYY() {
cout<<day<<" "<<getMonth(month)<<" "<<year<<endl;
}
string getMonth(int month){
if(month == 1){
return "January";
}
else if(month == 2){
return "February";
}
else if(month == 3){
return "March";
}
else if(month == 4){
return "April";
}
else if(month == 5){
return "May";
}
else if(month == 6){
return "June";
}
else if(month == 7){
return "July";
}
else if(month == 8){
return "August";
}
else if(month == 9){
return "September";
}
else if(month == 10){
return "October";
}
else if(month == 11){
return "November";
}
else if(month == 12){
return "December";
}
return "Invalid month";
}
};
int main()
{
int m,d,y;
cout << "Enter the day: ";
cin >> d;
while(d <1 || d > 31){
cout << "Invalid day. Enter the day: ";
cin >> d;
}
cout << "Enter the month: ";
cin >> m;
while(m <1 || m > 12){
cout << "Invalid month. Enter the day: ";
cin >> m;
}
cout<<"Enter year: ";
cin >> y;
date obj(m,d,y);
obj.printMMDDYYYY();
obj.printMonthDDYYYY();
obj.printDDMonthYYYY();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the day: 25
Enter the month: 12
Enter year: 2014
12/25/2014
December 25, 2014
25 December 2014
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.