Declare the class Date, consisting of data members are day, month and year. The member functions are set date() to assign the value for data members, Month() to find the string for the month data member and show() to display the date.(Implement the concept of class and objects)
#include <iostream>
#include <string>
using namespace std;
class Date
{
public:
Date() {};
void SetDate();
string Month();
void Show();
private:
int day;
int month;
int year;
};
void Date::SetDate()
{
while (true)
{
cout << "Enter year: ";
cin >> year;
cout << "Enter month number (1 - January, 2 - February etc.): ";
cin >> month;
if (month > 12 || month < 1)
{
cout << "Incorrect month number!" << endl;
continue;
}
cout << "Enter day number: ";
cin >> day;
if (day < 1 || day > 31)
{
cout << "Incorrect day number!" << endl;
continue;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day > 30)
{
cout << "In this month less than 31 days." << endl;
continue;
}
}
if (month == 2 && year % 4)
{
if (day > 28)
{
cout << "In this month less than 29 days." << endl;
continue;
}
}
if (month == 2 && !(year % 4))
{
if (day > 29)
{
cout << "In this month less than 30 days." << endl;
continue;
}
}
break;
}
}
string Date::Month()
{
switch (month)
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Error";
}
}
void Date::Show()
{
cout << day << " " << Month()<< " " << year;
}
int main()
{
Date Hill;
Hill.SetDate();
Hill.Show();
return 0;
}
Comments
Leave a comment