Create a \"date\" class that contains 3 private data of 1. month 2.day 3.year se
ID: 3857088 • Letter: C
Question
Create a "date" class that contains
3 private data of 1. month 2.day 3.year
setters and getters for each of the data
one default constructor
one constructer with three arguments of month day year, and a assert statement in the constructor.
a printdate function and a samemonth function
instantiate one date object (date1) using the default constructor
use the getters to display the month, day, and year of date1 (should print the default values)
read keyboard input from the user for a month, day and year
use the setters to set the values of date1 to the values that came from the user
read keyboard input from the user for a second date
use the constructor with three arguments to instantiate date2 to the second date input from the user
print both objects using printDate
print a message to say if the two months are the same using the same month function
Date.h
contains the class definition
Date.cpp
includes "Date.h"
contains the functions for the class
main.cpp
includes "Date.h"
tests the class
Explanation / Answer
#include <iostream>
using namespace std;
// define the classes
class date
{
// 1. A class definition for a Date class that contains three integer data members: month, day, and year.
private:
int month, day, year;
public:
void print() const;
date();
date(int, int);
date(int, int, int);
//5. Each constructor should also output a message to the user stating which constructor is currently being used.
};
//2. One constructor that assigns the date 1/1/2000 to any new object that does not receive any arguments.
date::date()
{
month = 1;
day = 1;
year = 2000;
cout << "Using no-argument constructor, assigning date 1/1/2000";
}
//3. One constructor that accepts month and day arguments and uses a default year of 2004.
date::date(int m, int d)
{
month = m;
day = d;
year = 2004;
cout << "Using 2-argument constructor, assigning year 2004";
}
//4. One constructor that accepts month, day, and year arguments.
date::date(int m, int d, int y)
{
month = m;
day = d;
year = y;
cout << "Using 3-argument constructor, for mo/da/yr";
}
//6. A method that displays the values in the Date object.
void date::print() const
{
cout << " The date in this object is " << month << "/" << day << "/" << year;
}
/*
7. A Main() program in which you:
Instantiate three Date objects - create one for each type of constructor (no argument, 2-argument, 3-argument).
For the 2-argument and 3-argument constructor prompt the user for console input of the month, day or month, day, year.
Display output for each of the three constructor types.
*/
int main()
{
cout << "Implementing overloaded constructors program";
cout << " ************************************ ";
date d1;
d1.print();
cout << " ************************************ ";
int mo, da, yr;
cout << "Enter a month eg: 10 for October: ";
cin >> mo;
cout << "Enter a day eg: 24: ";
cin >> da;
date d2(mo, da);
d2.print();
cout << " ************************************ ";
cout << "Enter a month eg: 10 for October: ";
cin >> mo;
cout << "Enter a day eg: 24: ";
cin >> da;
cout << "Enter a year eg: 1950: ";
cin >> yr;
date d3(mo, da, yr);
d3.print();
cout << " ************************************" << endl;
system("pause");
return 0;
}