Question

In: Computer Science

Create a "Date" class that contains: three private data members: month day year (I leave it...

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 (Two Runs)

>./main
Testing the default constructor and the getters
The initialized date is (M-D-Y):1-1-1

Please enter a date:(Month Day Year): 11 03 1976
Please enter a second date:(Month Day Year): 03 03 1999

Printing the two days:
The date is (M-D-Y): 11-3-1976
The date is (M-D-Y): 3-3-1999
The months are different
>./main
Testing the default constructor and the getters
The initialized date is (M-D-Y): 1-1-1

Please enter a date:(Month Day Year): 12 15 2009
Please enter a second date:(Month Day Year): 12 25 2020

Printing the two days:
The date is (M-D-Y): 12-15-2009
The date is (M-D-Y): 12-25-2020
The months are the same

Solutions

Expert Solution

// Date.h

#ifndef DATE_H
#define DATE_H

class Date
{
public:
// Function declarations
Date();
Date(int month, int day, int year);

int getMonth();
void setMonth(int month);
int getDay();
void setDay(int day);
int getYear();
void setYear(int year);
void printDate();
bool sameMonth(Date d);

private:
// Declaring variables
int month;
int day;
int year;
};
#endif

/****************************************************/

/****************************************************/

// Date.cpp

#include <assert.h>
#include <iostream>
using namespace std;
#include "Date.h"

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

int Date::getMonth()
{
return month;
}
void Date::setMonth(int month)
{
assert(month >= 1 && month <= 12);
this->month = month;
}
int Date::getDay()
{
return day;
}
void Date::setDay(int day)
{
assert(day >= 1 && day <= 31);
this->day = day;
}
int Date::getYear()
{
return year;
}
void Date::setYear(int year)
{
this->year = year;
}
void Date::printDate()
{
cout << "he date is (M-D-Y): " << month << "-" << day << "-" << year << endl;
}
bool Date::sameMonth(Date d)
{
if (this->month == d.getMonth())
return true;
else
return false;
}

/****************************************************/

/****************************************************/

// main.cpp

#include <iostream>
using namespace std;
#include "Date.h"

int main()
{
int month,day,year;
Date date1;
cout<<"The initialized date is (M-D-Y):"<<date1.getMonth()<<"-"<<date1.getDay()<<"-"<<date1.getYear()<<endl;
cout<<"Please enter a date : (Month Day Year):";
cin>>month>>day>>year;
date1.setDay(day);
date1.setMonth(month);
date1.setYear(year);
cout<<"Please enter a second date : (Month Day Year):";
cin>>month>>day>>year;
Date date2(month,day,year);

cout<<"\nPrinting the two days:"<<endl;
date1.printDate();
date2.printDate();
bool b=date1.sameMonth(date2);
if(b)
{
cout<<"The months are the same"<<endl;
}
else{
cout<<"The months are not same"<<endl;
}

return 0;
}


/***********************************************/

/***********************************************/

Output:

/***********************************************/


Related Solutions

C++ program: Create a Date class that contains three members: the month, the day of the...
C++ program: Create a Date class that contains three members: the month, the day of the month, and the year, all of type int. The user should enter a date in the format 12/31/2001, store it in an object of type Date, then retrieve the object and print it out in the same format. Next create an employee class. The member data should comprise an employee number (type int) and the employee’s compensation (in dollars; type float). Extend the employee...
C++ CLASSES 1. Define a class date that has day, month and year data members. 2....
C++ CLASSES 1. Define a class date that has day, month and year data members. 2. Define a class reservation with following data members: - An integer counter that generates reservation numbers. - An integer as a reservation number. The counter is incremented each time a reservation object is created. This value will be assigned as the reservation number. - Number of beds. - Reservation date from class date. 3. Define a class rooms with data members: - Room number...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class for working with complex numbers. Only 2 private float data members are needed,...
Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class:a. A default constructor that uses default arguments in case no initializers are included in the main.b. Add two complex numbers and store the sum.c. Subtract two complex numbers and store the difference.d. Multiply two complex numbers and store the product.e. Print...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT