Answer to Question #174862 in C++ for Vijay Raj

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 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;
}

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!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS