152k views
5 votes
There exists a data type Date with member function Increment that increments the current Date object by one. The ++ operator is being overloaded to postincrement an object of type Date. Select the correct implementation:

1.Date Date::operator++( int ){Date temp = *this;Increment();return *temp;}2.Date Date::operator++( int ){Increment();Date temp = *this;return temp;}3.Date Date::operator++( int ){Date temp = *this;return this;temp.Increment();}4.Date Date::operator++( int ){Date temp = *this;Increment();return temp;}

User Hous
by
5.7k points

1 Answer

2 votes

Answer:

Date Date::operator++( int )

{

Date temp = *this;

Increment();

return temp;

}

Step-by-step explanation:

Of options 1 through 4, only option 4 correctly implement the post increment of the ++ operator.

Interpreting each line of option for and comparing it to other options

Line 1: Date Date::operator++( int )

This declares the data type and its ++ operator

This is same for all options

Line 2: The Date type needs to be declared

In option 4, it was declared correctly as Date temp = *this; using a pointer variable *this.

Here, option 2 is removed from the list of possible options.

Comparing other options

Line 3: The member increment option needs to be declared after the declaration of the date type variable.

This is correctly declared as Increment(); in option 4

Here, option 3 is removed from the list of possible options.

Comparing other options

Line 4: The value declared variable temp needs to be returned the way it was declared (without pointer indicator)

Hence, option 4 is best appropriate

User Andymcgregor
by
5.3k points