Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal Practice using the following: Simple file documentation Using header and source files Constructors and destructors Overloading the + and the < < operators Requirements

Goal

Practice using the following:

  • Simple file documentation
  • Using header and source files
  • Constructors and destructors
  • Overloading the + and the << operators

Requirements

Implement a Date class using a C++ program. Match the output against the sample output given the provided test harness.

The Date Class

The members must be declared in a header file and implemented in a source file. You may also have another source file containing the test harness.

Date

Class

Fields

- year_ : int = 2019

- month_ : int = 1

- day_ : int = 1

Methods

+ constructor Date(

year : int,

month : int,

day : int)

+ destructor ~Date()

+ operator+(rhs : const Date&) : Date

+ operator+(rhs : constint) : Date

+ friend operator<<(

os : ostream&,

rhs : const Date&) : ostream&

- normalize() : void

Description of class members

Fields

There are three int fields that are private.

year_- this is a private int field that represents the year value of this object.

month_ - this is a private int field that represents the month value of this object.

day_ - this is a private int field that represents the day value of this object.

Constructors

Date(int year, int month, int day) - This public constructor takes three int arguments and assigns them to the appropriate fields. Please be aware of the default argument declared in the header file.

Before assignments it does the following sanity checks:

  • If the argument year is less than 0, then the field is set to 2022.
  • If the argument month is not within the range 1 to 12, then the field is set to 1.
  • If the argument day is not within the range 1 to 30, then the field is set to 1.

Destructors

~Date() - This public destructor does not implement any logic at this time. It simply output the string "destructor" to the console.

Methods

There are three friend methods that overload the plus and the insertion operators.

Dateoperator+(constDate&rhs) - This method overloads the plus operator. It facilitates the addition of two Date objects using the arithmetic + operator. It returns a Date object with the following features:

  • year_ field as the sum of the year_ fields of the argument and the appropriate field.
  • month_ field as the sum of the month_ fields of the argument and the appropriate field.
  • day_ field as the sum of the day_ fields of the argument and the appropriate field.

Dateoperator+(constintrhs) - This method overloads the plus operator. It facilitates the addition of two Date objects using the arithmetic + operator. It returns a Date object with the following features:

  • year_ field the same as the year_ field.
  • month_ field as the same as of the month_ field.
  • day_ field as the sum of the day_ field and the argument.

std::ostream&operator<<(std::ostream&os,constDate&date) - This method overloads the insertion operator. It facilitates the easy display of an object using and stream object. See the sample output for clues on how to implement the logic.

voidDate::normalize()- This private method constrains the day_ and the month_ fields to a sensible range. (You may assume that day_ may range from 1 to 30 and month_ may range from 1 to 12.)

This method is call whenever addition is performed.

Test harness

Copy the following statements and paste into your main method.

std::cout<<" creating an anonymous object - 1 "; std::cout<< Week01::Date(2019) <<' '; std::cout<<" creating a named object - 2 "; Week01::Date d1(2020, 3, 28); std::cout<< d1<<' '; std::cout<<" creating another named object - 3 "; Week01::Date d2(2, 10, 14); std::cout<< d2<<' '; std::cout<<" creating another named object - 4 "; Week01::Date d3 = d1+ d2; std::cout<< d1<<" + "<< d2<<" = "<< d3<<' '; int days = 84; //setting a new object to d3 std::cout<<" setting a new object to an existing variable "; d3= d2+ days; std::cout<< d2<<" + "<< days<<" = "<< d3<<' '; std::cout<< d1<<' '; std::cout<< d2<<' '; std::cout<< d3<<' ';

Sample output

Try to understand why and when constructors and destructor are called.

creating an anonymous object - 1 constructor <2019-1-1> destructor creating a named object - 2 constructor <2020-3-28> creating another named object - 3 constructor <2-10-14> creating another named object - 4 constructor destructor <2020-3-28> + <2-10-14> = <2023-2-12> setting a new object to an existing variable constructor destructor destructor <2-10-14> + 84 = <3-1-8> <2020-3-28> <2-10-14> <3-1-8> destructor destructor destructor

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions