Q4) Date Class) Create a class called Date that includes three pieces of informa
ID: 3741567 • Letter: Q
Question
Q4) Date Class) Create a class called Date that includes three pieces of information as data members-a month (type int), a day (type int) and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For thepurpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 1-12; if it isn't, set the month to 1. Provide a set and a get function for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date's capabilities. (2 marks)Explanation / Answer
#include <iostream>
using namespace std;
class Date {
private:
int month, year, day;
public:
Date(int d, int m, int y) {
year = y;
day = d;
if(m<1 || m > 12) {
month = 1;
}
else {
month = m;
}
}
void displayDate() {
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
void setDay(int d) {
day = d;
}
void setMonth(int m){
month = m;
}
void setYear(int y) {
year = y;
}
int getYear() {
return year;
}
int getMonth() {
return month;
}
int getDay() {
return day;
}
};
int main()
{
Date d1(2,12,1990);
d1.displayDate();
Date d2(5,15,1910);
d2.displayDate();
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.