In: Computer Science
C++ Program
Create Semester class (.h and .cpp file) with the
following three member variables:
▪ a semester name (Ex: Fall 2020)
▪ a Date instance for the start date of the semester
▪ a Date instance for the end date of the semester
The Semester class should have the following
member functions
1. Create a constructor that accepts three arguments: the semester
name, the start Date, and the end Date. Use default values for all
the parameters in the constructor
2. Overload the << operator for this
class
▪ Have it output the semester name and dates in a manner similar to
this:
Semester: Fall 2020 (08/31/2020 - 12/10/2020)
3. Overload the >> operator for this
class
▪ Have the >> operator accept input for the
Semester name, start date and end date.
4. Create the get and set functions for each variable - Do NOT
recreate the Date class - reuse code from Date Class.
Below is the Date .h and .cpp file
Date.h
========================================
#define DATE_H
#include <iostream>
using namespace std;
class Date
{
friend ostream& operator << (ostream&,
const Date&);
friend istream& operator >> (istream&,
Date&);
private:
int month;
int day;
int year;
void checkDate();
public:
Date(int = 1, int = 1, int = 1990);
~Date();
Date& setDate(int, int, int);
Date& setMonth(int);
Date& setDay(int);
Date& setYear(int);
int getMonth() const;
int getDay() const;
int getYear() const;
bool operator< (const Date&);
bool operator> (const Date&);
bool operator<= (const Date&);
bool operator>= (const Date&);
bool operator== (const Date&);
bool operator!= (const Date&);
};
Date.cpp
========================================
#include <iostream>
#include <iomanip>
#include "Date.h"
using namespace std;
Date::Date(int m, int d, int y)
{
setDate(m, d, y);
}
Date::~Date()
{
}
Date& Date::setDate(int m, int d, int y)
{
setMonth(m);
setDay(d);
setYear(y);
return *this;
}
void Date::checkDate()
{
static const int daysPerMonth[13] = { 0, 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month <= 0 || month >= 12)
{
month = 1;
}
if (!(month == 2 && day == 29 &&
(year % 400 == 0 || (year % 4 == 0 && year % 100 !=
0)))
&& (day <= 0 || day >
daysPerMonth[month]))
{
day = 1;
}
if (year <= 1989)
{
year = 1990;
}
}
Date& Date::setMonth(int m)
{
month = m;
checkDate();
return *this;
}
Date& Date::setDay(int d)
{
day = d;
checkDate();
return *this;
}
Date& Date::setYear(int y)
{
year = y;
checkDate();
return *this;
}
int Date::getMonth() const
{
return month;
}
int Date::getDay() const
{
return day;
}
int Date::getYear() const
{
return year;
}
bool Date::operator<(const Date& right)
{
bool status;
if (year < right.year)
status = true;
else if ((year == right.year) && (month <
right.month))
status = true;
else if ((year == right.year) && (month <
right.month) && (day < right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator>(const Date& right)
{
bool status;
if (year > right.year)
status = true;
else if ((year == right.year) && (month >
right.month))
status = true;
else if ((year == right.year) && (month >
right.month) && (day > right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator<=(const Date& right)
{
bool status;
if (year <= right.year)
status = true;
else if ((year == right.year) && (month <=
right.month))
status = true;
else if ((year == right.year) && (month <=
right.month) && (day <= right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator>=(const Date& right)
{
bool status;
if (year < right.year)
status = true;
else if ((year == right.year) && (month <
right.month))
status = true;
else if ((year == right.year) && (month <
right.month) && (day < right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator==(const Date& right)
{
bool status;
if (year == right.year && month == right.month
&& day == right.day)
status = true;
else
status = false;
return status;
}
bool Date::operator!=(const Date& right)
{
bool status;
if (year != right.year && month != right.month
&& day != right.day)
status = true;
else
status = false;
return status;
}
ostream& operator<<(ostream& output, const
Date& obj)
{
output << setfill('0') << setw(2) <<
obj.month << '/' << setfill('0') << setw(2)
<< obj.day << '/' << obj.year;
return output;
}
istream& operator>>(istream& input, Date&
obj)
{
input >> obj.month;
input.ignore();
input >> obj.day;
input.ignore();
input >> obj.year;
obj.checkDate();
return input;
}
Code for Date.h:
#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std;
class Date
{
friend ostream &operator<<(ostream &, const Date &);
friend istream &operator>>(istream &, Date &);
private:
int month;
int day;
int year;
void checkDate();
public:
Date(int = 1, int = 1, int = 1990);
~Date();
Date &setDate(int, int, int);
Date &setMonth(int);
Date &setDay(int);
Date &setYear(int);
int getMonth() const;
int getDay() const;
int getYear() const;
bool operator<(const Date &);
bool operator>(const Date &);
bool operator<=(const Date &);
bool operator>=(const Date &);
bool operator==(const Date &);
bool operator!=(const Date &);
};
#endif
Code for Date.cpp:
#include <iostream>
#include <iomanip>
#include "Date.h"
using namespace std;
Date::Date(int m, int d, int y)
{
setDate(m, d, y);
}
Date::~Date()
{
}
Date &Date::setDate(int m, int d, int y)
{
setMonth(m);
setDay(d);
setYear(y);
return *this;
}
void Date::checkDate()
{
static const int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month < 0 || month > 12)
{
month = 1;
}
if (!(month == 2 && day == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) && (day <= 0 || day > daysPerMonth[month]))
{
day = 1;
}
if (year <= 1989)
{
year = 1990;
}
}
Date &Date::setMonth(int m)
{
month = m;
checkDate();
return *this;
}
Date &Date::setDay(int d)
{
day = d;
checkDate();
return *this;
}
Date &Date::setYear(int y)
{
year = y;
checkDate();
return *this;
}
int Date::getMonth() const
{
return month;
}
int Date::getDay() const
{
return day;
}
int Date::getYear() const
{
return year;
}
bool Date::operator<(const Date &right)
{
bool status;
if (year < right.year)
status = true;
else if ((year == right.year) && (month < right.month))
status = true;
else if ((year == right.year) && (month < right.month) && (day < right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator>(const Date &right)
{
bool status;
if (year > right.year)
status = true;
else if ((year == right.year) && (month > right.month))
status = true;
else if ((year == right.year) && (month > right.month) && (day > right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator<=(const Date &right)
{
bool status;
if (year <= right.year)
status = true;
else if ((year == right.year) && (month <= right.month))
status = true;
else if ((year == right.year) && (month <= right.month) && (day <= right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator>=(const Date &right)
{
bool status;
if (year < right.year)
status = true;
else if ((year == right.year) && (month < right.month))
status = true;
else if ((year == right.year) && (month < right.month) && (day < right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator==(const Date &right)
{
bool status;
if (year == right.year && month == right.month && day == right.day)
status = true;
else
status = false;
return status;
}
bool Date::operator!=(const Date &right)
{
bool status;
if (year != right.year && month != right.month && day != right.day)
status = true;
else
status = false;
return status;
}
ostream &operator<<(ostream &output, const Date &obj)
{
output << setfill('0') << setw(2) << obj.month << '/' << setfill('0') << setw(2) << obj.day << '/' << obj.year;
return output;
}
istream &operator>>(istream &input, Date &obj)
{
input >> obj.month;
input.ignore();
input >> obj.day;
input.ignore();
input >> obj.year;
obj.checkDate();
return input;
}
Code for Semester.h:
#ifndef SEMESTER_H
#define SEMESTER_H
#include <iostream>
#include "Date.h"
using namespace std;
class Semester
{
friend ostream &operator<<(ostream &, const Semester &);
friend istream &operator>>(istream &, Semester &);
private:
string semesterName;
Date startDate, endDate;
public:
Semester();
Semester(string semesterName, Date startDate, Date endDate);
string getSemesterName();
int getSemesterStartMonth();
int getSemesterStartDay();
int getSemesterStartYear();
int getSemesterEndMonth();
int getSemesterEndDay();
int getSemesterEndYear();
void setSemesterName(string newSemesterName);
void setSemesterStartMonth(int month);
void setSemesterStartDay(int day);
void setSemesterStartYear(int year);
void setSemesterEndMonth(int month);
void setSemesterEndDay(int day);
void setSemesterEndYear(int year);
};
#endif
Code for Semester.cpp:
#include <iostream>
#include "Semester.h"
using namespace std;
Semester::Semester()
{
semesterName = "";
startDate = Date();
endDate = Date();
}
Semester::Semester(string semesterName, Date startDate, Date endDate)
{
this->semesterName = semesterName;
this->startDate.setDate(startDate.getMonth(), startDate.getDay(), startDate.getYear());
this->endDate.setDate(endDate.getMonth(), endDate.getDay(), endDate.getYear());
}
string Semester::getSemesterName()
{
return semesterName;
}
int Semester::getSemesterStartMonth()
{
return startDate.getMonth();
}
int Semester::getSemesterStartDay()
{
return startDate.getDay();
}
int Semester::getSemesterStartYear()
{
return startDate.getYear();
}
int Semester::getSemesterEndMonth()
{
return endDate.getMonth();
}
int Semester::getSemesterEndDay()
{
return endDate.getDay();
}
int Semester::getSemesterEndYear()
{
return endDate.getYear();
}
void Semester::setSemesterName(string newSemesterName)
{
semesterName = newSemesterName;
}
void Semester::setSemesterStartMonth(int month)
{
startDate.setMonth(month);
}
void Semester::setSemesterStartDay(int day)
{
startDate.setDay(day);
}
void Semester::setSemesterStartYear(int year)
{
startDate.setYear(year);
}
void Semester::setSemesterEndMonth(int month)
{
endDate.setMonth(month);
}
void Semester::setSemesterEndDay(int day)
{
endDate.setDay(day);
}
void Semester::setSemesterEndYear(int year)
{
endDate.setYear(year);
}
ostream &operator<<(ostream &output, const Semester &s)
{
output << "Semester: " << s.semesterName;
output << " (" << s.startDate << " - " << s.endDate << ")";
return output;
}
istream &operator>>(istream &input, Semester &s)
{
getline(input, s.semesterName);
input >> s.startDate;
input >> s.endDate;
return input;
}
Code for main.cpp:
#include <stdio.h>
#include "Date.h"
#include "Semester.h"
using namespace std;
int main()
{
Semester s;
cin >> s;
cout << s;
return 0;
}
Compiling and Executing the program: