Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QUESTION IN TEXT FORMAT: Lab Exercise -- ADTs Implemented with C++ Classes Creat

ID: 3586602 • Letter: Q

Question

QUESTION IN TEXT FORMAT:

Lab Exercise -- ADTs Implemented with C++ Classes

Create a "Date" class that contains:

three private data members:

month

day

year
(I leave it to you to decide the type)

"setters" and "getters" for each of the data (6 functions in total)

One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive.

one default constructor (no arguments)

one constructor with three arguments: month, day, and year

Add assert statements to enforce reasonable conditions.

a printDate function. This function will have no arguments and return void

a sameMonth function. This function will have one Date argument and a boolean return type

In main (in the following order):

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 (testing the sameMonth function)

Your code should be in three files:

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

Sample Output

If you are having trouble compiling your class, check for these common errors:

You have forgotten the semi-colon(;) after the closing curly bracket (}) for the class definition (in the Date.h file)

You have forgotten the scope resolution as in: void Date::setYear(int y) (in the Date.cpp file)

You forgot the () after the function name. For example, you should write: cout << date1.getYear() (in main.cpp)

For the StudentClass that we go over this week, see StudentClass.cpp

Explanation / Answer

date.h

class date {
private:
    int date;
    int month;
    int year;
public:
    date();
    date(int, int, int);
    int getDate();
    int getMonth();
    int getYear();
    void setDate();
    void setMonth();
    void setYear();
    void printDate();
    bool sameMonth(Date);
};

date.cpp

#include "date.h"
#include<iostream>

Date::Date() {
   day = 1;
   month = 1;
   year = 1990;
}

Date::Date(int day, int month, int year) {
   this->day = day;
   this->month = month;
   this->year = year;
}

int Date::getDay() {
   return day;
}

int Date::getMonth() {
   return month;
}

int Date::getYear() {
   return year;
}

void Date::setDay(int day) {
   if (day < 1 || day > 31)
       return;
   this->day = day;
}

void Date::setMonth(int month) {
   if (month < 1 || month > 12)
       return;
   this->month = month;
}

void Date::setYear(int year) {
   this->year = year;
}

void Date::printDate() {
   std::cout << month << "-" << day << "-" << year << std::endl;
}

bool Date::sameMonth(Date otherDate) {
   return otherDate.getMonth() == month;
}

main.cpp

#include <iostream>

#include "date.h"

using namespace std;

int main() {
    Date d1(17,11,1993);
    Date d2(26,11,2009);

    d1.printDate();

    cout << d1.sameMonth(d2);
    return 0;
}

I kept the main short due to huge pressure in question flow. Please co-operate