To create a class named Date with four data members (year, month, day, day_of_week) and member functions to initialize data members and display today's date in standard format, we can follow these steps:
- Define a class named Date with the four data members - year, month, day, and day_of_week.
- Define a constructor function that initializes the year, month, day, and day_of_week data members to the current date.
- Define a member function named displayDate() that displays the current date in standard format - "YYYY/MM/DD (Day of Week)".
Here's the C++ code for the above program:
#include <iostream>
#include <ctime>
using namespace std;
class Date {
private:
int year, month, day, day_of_week;
public:
Date() {
// Get current date and time
time_t now = time(0);
tm *ltm = localtime(&now);
// Initialize year, month, day, and day_of_week data members
year = 1900 + ltm->tm_year;
month = 1 + ltm->tm_mon;
day = ltm->tm_mday;
day_of_week = ltm->tm_wday;
}
void displayDate() {
cout << year << "/" << month << "/" << day << " (";
switch(day_of_week) {
case 0:
cout << "Sunday";
break;
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
}
cout << ")" << endl;
}
};
int main() {
Date d;
d.displayDate();
return 0;
}
#include <ctime>
using namespace std;
class Date {
private:
int year, month, day, day_of_week;
public:
Date() {
// Get current date and time
time_t now = time(0);
tm *ltm = localtime(&now);
// Initialize year, month, day, and day_of_week data members
year = 1900 + ltm->tm_year;
month = 1 + ltm->tm_mon;
day = ltm->tm_mday;
day_of_week = ltm->tm_wday;
}
void displayDate() {
cout << year << "/" << month << "/" << day << " (";
switch(day_of_week) {
case 0:
cout << "Sunday";
break;
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
}
cout << ")" << endl;
}
};
int main() {
Date d;
d.displayDate();
return 0;
}
Explanation:
- We define a class named Date with the four data members - year, month, day, and day_of_week.
- We define a constructor function that initializes the year, month, day, and day_of_week data members to the current date. We use the C++ time function to get the current date and time, and then extract the year, month, day, and day_of_week data members from the returned tm struct.
- We define a member function named displayDate() that displays the current date in standard format - "YYYY/MM/DD (Day of Week)". We use the C++ cout function to output the year, month, day, and day_of_week data members, and then use a switch statement to output the corresponding day of the week string.
- In the main function, we create an instance of the Date class and call its displayDate() member function to display the current date in standard format.
No comments:
Post a Comment
If you have any doubts, please let me know