Question

In: Computer Science

C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪...

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;
}

Solutions

Expert Solution

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:


Related Solutions

Separate code into .cpp and .h files: // C++ program to create and implement Poly class...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class representing Polynomials #include <iostream> #include <cmath> using namespace std; class Poly { private: int degree; int *coefficients; public: Poly(); Poly(int coeff, int degree); Poly(int coeff); Poly(const Poly& p); ~Poly(); void resize(int new_degree); void setCoefficient(int exp, int coeff); int getCoefficient(int exp); void display(); }; // default constructor to create a polynomial with constant 0 Poly::Poly() { // set degree to 0 degree = 0; //...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
Create a C++ program that consists of the following: In maincreate the following three variables:...
Create a C++ program that consists of the following: In main create the following three variables: A char named theChar A double named theDouble An int named theInt Fill each of these variables with data taken as input from the keyboard using a single cin statement. Perform the following task on each variable: Increment theChar by one Decrement theDouble by two Square theInt This should be done on separate lines. Output the value of each variable to the screen on...
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file....
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an array of objects and practice passing objects to and return objects from functions. String functions practice has also been included. Extend the BankAccount class. The member fields of the class are:...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the secret word char * secretWord; char *guessedWord; public: //please create related constructors, getters, setters,constructor() constructor() You will need to initialize all member variables including the two dynamic variables. destructor() Please deallocate/freeup(delete) the two dynamic arrays memories. guessALetter(char letter) 1.the function will check if the letter guessed is included in the word. 2. display the guessed word with related field(s) filled if the letter guessed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT