Question #174862

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)


1
Expert's answer
2021-03-23T18:43:12-0400
#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 1return "January";
	case 2return "February";
	case 3return "March";
	case 4return "April";
	case 5return "May";
	case 6return "June";
	case 7return "July";
	case 8return "August";
	case 9return "September";
	case 10return "October";
	case 11return "November";
	case 12return "December";
	defaultreturn "Error";
	}
}
 
void Date::Show()
{
	cout << day << " " << Month()<< " " << year;
}
 
int main()
{
	Date Hill;
	Hill.SetDate();
	Hill.Show();
 
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS