Question

Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int...

Implement the operator +=, -=, +, -, in the class Date
class Date
{
public:
  Date(int y=0, int m=1, int d=1);  
  static bool leapyear(int year);
  int getYear() const;
  int getMonth() const;
  int getDay() const;

  // add any member you need here  
};

You implementation should enable the usage like this:
void f()
{
  Date date = d;

  cout << "date = " << date << endl;
  cout << "date+1 = " << date+1 << endl;
  cout << "date-1 = " << date-1 << endl;

  date+=366;
  cout << "date = " << date << endl;
  date-=365;
  cout << "date = " << date << endl;
  date-=-365;
  cout << "date = " << date << endl;
  date+=-366;
  cout << "date = " << date << endl;

  cout << endl;
}


The output of f() should be:

date = 2004-2-28
date+1 = 2004-2-29
date-1 = 2004-2-27
date = 2005-2-28
date = 2004-2-29
date = 2005-2-28
date = 2004-2-28

//main.cpp

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

ostream& operator<<(ostream& os, const Date& date)
{
os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
return os;
}

/*
f1() test for:
constructor, copy constructor, assign assignment
static member function leapyear()
operator ==, !=, <, <=, >, >=
*/
void f1()
{
Date date1, date2(2003,1,1);
Date date3 = Date(2007,2,28);
cout << "date1: " << date1 << endl;
cout << "date2: " << date2 << endl;

cout << "date3 after copy constructor: " << date3 << endl;
date3 = date1;
cout << "date3 after copy asignment: " << date3 << endl;

cout << "year 1996 is leap year? " << Date::leapyear(1996) << endl;
cout << "year 1200 is leap year? " << Date::leapyear(1200) << endl;
cout << "year 1300 is leap year? " << Date::leapyear(1300) << endl;
cout << "year 1999 is leap year? " << Date::leapyear(1999) << endl;

cout << "(date1==date3)? " << (date1==date3) << endl;
cout << "(date1!=date3)? " << (date1!=date3) << endl;
cout << "(date1==date2)? " << (date1==date2) << endl;
cout << "(date1!=date2)? " << (date1!=date2) << endl;

cout << "(date1<date1)? " << (date1<date1) << endl;
cout << "(date1<=date1)? " << (date1<=date1) << endl;
cout << "(date1<date2)? " << (date1<date2) << endl;
cout << "(date1<=date2)? " << (date1<=date2) << endl;

cout << "(date1>date1)? " << (date1>date1) << endl;
cout << "(date1>=date1)? " << (date1>=date1) << endl;
cout << "(date1>date2)? " << (date1>date2) << endl;
cout << "(date1>=date2)? " << (date1>=date2) << endl;

}
/*
f2() test for:
subscript opeartor [] as both lvalue and rvalue
*/
void f2()
{
Date date1(2011,4,1);
cout << "date1: " << date1 << endl;
cout << date1["year"] << endl;
cout << date1["month"] << endl;
cout << date1["day"] << endl;
date1["year"] = 2000;
date1["month"] = 10;
date1["day"] = 10;

cout << "date1: " << date1 << endl;
}

/*
f3_0() test for:
operator
+=, -=,
+, -,
*/
void f3_0(const Date& d)
{
Date date = d;

cout << "date = " << date << endl;
cout << "date+1 = " << date+1 << endl;
cout << "date-1 = " << date-1 << endl;

date+=366;
cout << "date = " << date << endl;
date-=365;
cout << "date = " << date << endl;
date-=-365;
cout << "date = " << date << endl;
date+=-366;
cout << "date = " << date << endl;
    
cout << endl;
}
/*
f3_1() test for:
operator
++(prefix), --(prefix),
++(postfix), --(postfix),
+=, -=,
+, -,
<<
*/

void f3_1(const Date& d)
{
Date date = d;
cout << "date = " << date << endl;
cout << "++date = " << ++date << endl;
cout << "--date = " << --date << endl;
cout << "date++ = " << date++ << endl;
cout << "date-- = " << date-- << endl;
cout << "date = " << date << endl;
date+=366;
cout << "date = " << date << endl;
date-=365;
cout << "date = " << date << endl;
date-=-365;
cout << "date = " << date << endl;
date+=-366;
cout << "date = " << date << endl;
    
cout << endl;
}

void f3()
{
Date date1, date2(2004,2,28), date3(2007,2,28),date4(2000,2,28),date5(99,12,31);
f3_0(date1);
f3_0(date2);
f3_0(date3);
f3_0(date4);
f3_0(date5);
}


int main()
{
f3();
//system("PAUSE");
return 0;
}


//source.h

???

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
using namespace std;

class Date
{
public:
   Date(int y = 0, int m = 1, int d = 1) { year = y; month = m; day = d; } // parameterized constructor.
   bool leapyear(int year)
   {
       if (year % 4 == 0)
       {
           if (year % 100 == 0)
           {
               if (year % 400 == 0)
                   return true;
               else
                   return false;
           }
           else
               return true;
       }
       else
           return false;
   } // is leap year?
   // getters.
   int getYear() const { return year; }
   int getMonth() const { return month; }
   int getDay() const { return day; }
   Date(const Date& obj)
   {
       day = obj.day;
       month = obj.month;
       year = obj.year;
   } // copy constructor
   const Date& operator=(const Date& obj)
   {
       day = obj.day;
       month = obj.month;
       year = obj.year;
   } // assignment operator overloaded.
   const Date operator+(int obj)
   {
       Date res(*this);
       while (obj > 0)
       {
           res.day++;
           if (!validateDate(res.day, res.month, res.year))
           {
               res.month++;
               // every month has the same start date, but not the same ending date.
               res.day = 1;
           }
           obj--;
       }
       return res;
   }
   const Date operator-(int obj)
   {
       Date res(*this);
       while (obj > 0)
       {
           res.day--;
           if (!validateDate(res.day, res.month, res.year))
           {
               res.month--;
               // set the day to the last day of the respective month.
               if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
               {
                   res.day = 31;
               }
               else if ((month == 4 || month == 6 || month == 9 || month == 11))
               {
                   res.day = 30;
               }
               else if ((month == 2))
               {
                   res.day = 29;
               }
               if (!validateDate(res.day, res.month, res.year))
               {
                   res.day = 30;
               }
           }
           obj--;
       }
       return res;
   }
   const Date& operator+=(int obj)
   {
       while (obj > 0)
       {
           day++;
           if (!validateDate(day, month, year))
           {
               month++;
               // every month has the same start date, but not the same ending date.
               day = 1;
               if (!validateDate(day, month, year))
               {
                   month = 1;
                   year++;
               }
           }
           obj--;
       }
       return *this;
   }
   const Date& operator-=(int obj)
   {
       while (obj > 0)
       {
           day--;
           if (!validateDate(day, month, year))
           {
               month--;
               // set the day to the last day of the respective month.
               if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
               {
                   day = 31;
               }
               else if ((month == 4 || month == 6 || month == 9 || month == 11))
               {
                   day = 30;
               }
               else if ((month == 2))
               {
                   day = 29;
               }
               if (!validateDate(day, month, year))
               {
                   month = 12;
                   year--;
               }
           }
           obj--;
       }
       return *this;
   }
   // check if a date is vaild.
   bool validateDate(int day, int month, int year)
   {
       if (leapyear(year))
       {
           if (month >= 1 || month <= 12)
           {
               if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >= 1 && day <= 31))
               {
                   return true;
               }
               else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
               {
                   return true;
               }
               else if ((month == 2) && (day >= 1 && day <= 29))
               {
                   return true;
               }
               else
               {
                   return false;
               }
           }
       }
       else
       {
           if (month >= 1 || month <= 12)
           {
               if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day >= 1 && day <= 31))
               {
                   return true;
               }
               else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
               {
                   return true;
               }
               else if ((month == 2) && (day >= 1 && day <= 28))
               {
                   return true;
               }
               else
               {
                   return false;
               }
           }
       }
   }
   // outstream operator overload.
   friend ostream& operator<<(ostream& os, const Date& date);
   ~Date(){} // destructor.
private:
   // attributes.
   int year, month, day;
};

ostream& operator<<(ostream& os, const Date& date){
   os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
   return os;
}

void f3_0(const Date& d)
{
   Date date = d;

   cout << "date = " << date << endl;
   cout << "date+1 = " << date + 1 << endl;
   cout << "date-1 = " << date - 1 << endl;

   date += 366;
   cout << "date = " << date << endl;
   date -= 365;
   cout << "date = " << date << endl;
   date -= -365;
   cout << "date = " << date << endl;
   date += -366;
   cout << "date = " << date << endl;

   cout << endl;
}

void f3()
{
   Date date1, date2(2004, 2, 28), date3(2007, 2, 28), date4(2000, 2, 28), date5(99, 12, 31);
   f3_0(date2);
   f3_0(date2);
   f3_0(date2);
   f3_0(date2);
   f3_0(date2);
}
int main()
{
   f3();
   return 0;
}

date 2004-2-28 date 12004-2-29 1 date-12004-2-27 date = 2005-2-28 date = 2004-1-30 date 2004-1-30 date = 2004-1-30 date 2004-

for any query leave a comment here ill get back to you as soon as possible :)

PLEASE DO NOT FORGET TO LEAVE A THUMBS UP! THANKS! :)

Add a comment
Know the answer?
Add Answer to:
Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for c++ Implement the following overloading operator functions: Matrix (int rowSize, int colSize); ~Matrix...

    This is for c++ Implement the following overloading operator functions: Matrix (int rowSize, int colSize); ~Matrix (); Matrix operator + (Matrix & m); Matrix operator += (Matrix & m); Matrix operator += (const int &num); Matrix operator * (Matrix & m); Matrix operator ++(); friend Matrix operator +(const int &num, const Matrix &m); friend istream& operator>> (istream& in, const Matrix& m); friend ostream &operator<<(ostream &os, const Matrix &m); Test your Matrix class withe following main function: cout << "Matrix 1:"...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • Given a number, calculate how many years into the future it is, and what date. Assume...

    Given a number, calculate how many years into the future it is, and what date. Assume no leap years. For example: Please enter a day of the year (0 to exit): 1 jan 1 Please enter a day of the year (0 to exit): 365 dec 31 Please enter a day of the year (0 to exit): 366 1 year jan 1 Please enter a day of the year (0 to exit): 0 Thanks for playing! My current code: #include...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT