Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ 4. Lab Exercise Overloading Part 1 Change the sameDay function into a overloaded == operator. This will be a member function as demonstrated in

c++

4. Lab Exercise Overloading

Part 1

  1. Change the sameDay function into a overloaded == operator. This will be a memberfunction as demonstrated in the notes. Remember that you will want to compare the month, day, and year.
  2. Add an overloaded ++ operator (as a member function, shown in the notes)
  3. Just for fun, change the printDate function into an overloaded << operator as a non-member. The prototype in the .h will look like this:
  4. ostream& operator<<(ostream&, const Date&);

    The function itself will look like this:

    ostream& operator<<(ostream& os, const Date& myDate) { os << "Date is: " << myDate.getMonth() << "/" << myDate.getDay() << "/" << myDate.getYear() << endl; return os; }
  5. Explain why operator<< must be overloaded as a non-member.

Part 2

Overload the + operator so that the following lines of code run in main:

 Date date3, date4; date3 = date1 + 82; date4 = 6 + date2;

More Details:

  • Define two functions. The prototypes will look like this:
    Date operator+ (const Date&, int); Date operator+ (int, const Date&);
    Are they member or non-member functions? (Notice how many arguments they have)

  • Both functions will create a temporary date and return it.

  • Think about reusing as much code as possible.
    1. For one implementation of operator+, can you call the operator++ function?
    2. For the second implementation of operator+, can you call the first operator+ function?
    3. Together these two functions should be no more that 10 lines of code in the bodies. If you make it longer, you are working too hard.

  • Which of the two functions must be defined as a non-member? Why?

Sample Run (after Part 2)

>./main Date is: 0/0/0 Please enter integer Year, Day, and Month separated by spaces: 1 1 1 Please enter integer Year, Day, and Month separated by spaces: 2 2 2 Date is: 1/1/1 Date is: 2/2/2 The dates are different. After incrementing Date is: 1/2/1 Date is: 2/3/2 After addition Date is: 1/2/1 Date is: 2/3/2 Date is: 3/25/1 Date is: 2/9/2 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

=+while using another name?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago