Question

In: Computer Science

C++ Define a MyDate class to represent a data that has 3 data members: day, month,...

C++
Define a MyDate class to represent a data that has 3 data members: day, month, year. The class should have: A default constructor to initialize the day, month, and year to 0. A constructor that accepts three integers as arguments to initialize the day, month, and year if the three arguments are valid. A copy constructor that accepts a reference to a MyDate object as argument. This constructor use the argument's three data fields to initialize the day, month, and year. Overload the operator + : this function adds a number of days to the calling object and return the result date. Overload the operator -: this function accepts a MyDate object as argument. It calculates the number of days difference between the calling object and the argument object. Overload the operator -: this function subtract a number of days to the calling object and return the result date. Overload the opertor ==: this function accepts a MyDate object as argument. It compares the calling object with the argument object and returns true if they are the same date, otherwise, the function returns false. Friend function to overload operator >>: this function accepts an istream object and a MyDate object as argument. In this function, it get the day, month, and year as input and returns a MyDate object. Friend function to overload operator <<: this function accepts an ostream object and a MyDate object as argument. In this function, it displays the day, month, and year of the MyDate object in mm/dd/yyyy format.

Solutions

Expert Solution

// C++ program to create MyDate class

#include <iostream>

#include <iomanip>

using namespace std;

class MyDate

{

private:

       int day, month, year;

       static bool validDate(int day, int month, int year);

       static int daysInMonth(int month, int year);

public:

       MyDate();

       MyDate(int day, int month, int year);

       MyDate(const MyDate &other);

       MyDate operator+(int days);

       int operator-(const MyDate &other);

       MyDate operator-(int days);

       bool operator==(const MyDate &other);

       friend ostream& operator<<(ostream &out, const MyDate &other);

       friend istream& operator>>(istream &in, MyDate &other);

};

// helper function to validate date

bool MyDate::validDate(int day, int month, int year)

{

       if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

       {

             if(day < 1 || day > 31)

                    return false;

             else

                    return true;

       }else if(month == 4 || month == 6 || month == 9 || month == 11)

       {

             if(day < 1 || day > 30)

                    return false;

             else

                    return true;

       }else if(month == 2)

       {

             if((year%400 == 0 ) || ((year%4 == 0) && (year%100) != 0))

             {

                    if(day < 1 || day > 29)

                           return false;

                    else

                           return true;

             }else

             {

                    if(day < 1 || day > 28)

                           return false;

                    else

                           return true;

             }

       }else

             return false;

}

// helper function to return the days in the month

int MyDate::daysInMonth(int month, int year)

{

       if(month == 2)

       {

             if((year%400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))

                    return 29;

             else

                    return 28;

       }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month ==10 || month==12)

             return 31;

       else

             return 30;

}

// default constructor

MyDate ::MyDate() : day(0), month(0), year(0)

{}

// parameterized constructor

MyDate ::MyDate(int day, int month, int year)

{

       this->day = day;

       this->month = month;

       this->year = year;

       if(!validDate(day,month,year))

       {

             this->day = 0;

             this->month = 0;

             this->year = 0;

       }

}

// copy constructor

MyDate::MyDate(const MyDate &other) : day(other.day), month(other.month), year(other.year)

{}

// method to add days to this data and return the resultant date

MyDate MyDate:: operator+(int days)

{

       MyDate newDate(month,day,year);

       newDate.day += days;

       while(newDate.day > daysInMonth(newDate.month, newDate.year))

       {

             newDate.day -= daysInMonth(newDate.month,newDate.year);

             newDate.month++;

             if(newDate.month > 12)

             {

                    newDate.year++;

                    newDate.month=1;

             }

       }

       return newDate;

}

// method to return the number of days between this date and other date (other date - this date)

int MyDate:: operator-(const MyDate &other)

{

       int days = 0;

       for(int i=year+1;i<other.year;i++)

       {

             if((i%400 == 0) || ((i%4 == 0) && (i%100 != 0)))

                    days += 366;

             else

                    days += 365;

       }

       if(year == other.year)

       {

             for(int i=month+1;i<other.month;i++)

                    days += daysInMonth(i,year);

             days += daysInMonth(month,year) - day;

             days += other.day;

       }else

       {

             for(int i=month+1;i<=12;i++)

                    days += daysInMonth(i,year);

             days += daysInMonth(month,year) - day;

             for(int i=1;i<other.month;i++)

                    days += daysInMonth(i,other.year);

             days += other.day;

       }

       return days;

}

// method to subtract days from this date and return the resultant date

MyDate MyDate:: operator-(int days)

{

       MyDate newDate(month,day,year);

       while(days > 0)

       {

             if(days < newDate.day)

             {

                    newDate.day -= days;

                    days = 0;

             }else

             {

                    newDate.month--;

                    if(newDate.month < 1)

                    {

                           newDate.month = 12;

                           newDate.year--;

                    }

                    days -= newDate.day;

                    newDate.day = daysInMonth(newDate.month, newDate.year);

             }

       }

       return newDate;

}

// method to check if the this date is equal to other date

bool MyDate:: operator==(const MyDate &other)

{

       return((year == other.year) && (month == other.month) && (day == other.day));

}

// method to output the date as mm/dd/yyyy

ostream& operator<<(ostream &out, const MyDate &other)

{

       out<<setw(2)<<setfill('0')<<other.month<<"/"<<setw(2)<<setfill('0')<<other.day<<"/"<<other.year;

       return out;

}

// method to read the date in the format mm/dd/yyyy

istream& operator>>(istream &in, MyDate &other)

{

       char ch;

       in>>other.month>>ch>>other.day>>ch>>other.year;

       return in;

}

//end of MyDate class


Related Solutions

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...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
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...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
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...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
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,...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary....
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary. An itinerary has a title, a source position and a destination position. In addition, it may include intermediate positions. All positions have the same abstract type T. The real type can be decided later, for example it can be a cartesian point with x,y,z coordinates, or an airport code, or a city name, or a country name, etc .. The itinerary includes a vector...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT