c++modular-design

In C++ modular programming, how do you assemble the .cpp and .h files?


I am currently working on a large project for school that is divided into 6 Milestones. In the first milestone, I am given the code to add into my files but I don't know how to assort them. What is the general convention to distribute code in modular programming?

This is what I have been given:

MILESTONE 1: THE DATE CLASS

To kick-start this project, clone/download milestone 1 from the course repository and code the missing parts of the Date class.

The Date class encapsulates a date that is readable by an std::istream and printable by an std::ostream using the following format for both reading and writing: YYYY/MM/DD, where YYYY refers to a four-digit value for the year, MM refers to a two-digit value for the month and DD refers to a two-digit value for the day in the month.

Complete the implementation of the Date class using following specifications:

Pre-defined constants:

Pre-define the limits on the years to be considered acceptable:

const int min_year = 2000 const int max_year = 2030

Private members:

Data:

The year – a four digit integer between min_year and max_year

The month of the year – a value between 1 and 12 inclusive

The day of the month – a value between 1 and the number of days in the month (see the

mday(int,int) member function described below) – Note that February has 29 days in a leap year.

The comparator value to be used for comparing the date stored in the current object with the date stored in another Date object. Your constructors set this value and your public operators use it to compare two dates. (If the value of date one is larger than the value of date two, then date one is more recent than date two; that is, date one is after date two).

The error state which the client can reference to determine if the object holds a valid date, and if not, which part of the date is in error. The possible error states are integer values defined as macros in the Date class header:

NO_ERROR 0 -- No error - the date is valid

CIN_FAILED 1 -- istream failed on information entry

YEAR_ERROR 2 -- Year value is invalid

MON_ERROR 3 -- Month value is invalid

DAY_ERROR 4 -- Day value is invalid

Member functions:

int mdays(int month, int year) const; (this query is already implemented and provided). This query returns the number of days in month of year.

void errCode(int errorCode); This function sets the error state variable to one of the values listed above.

Public members:

Constructors:

No argument (default) constructor: initializes the object to a safe empty state and sets the error state to NO_ERROR. Use 0000/00/00 as the date for a safe empty state and set the comparator value to 0.

Three argument constructor: accepts in its parameters integer values for the year, month andday. This constructor checks if each number is in range, in the order of year, month andday. If any of the numbers are not within range, this function sets the error state to theappropriate error code and stops further validation.

(Use the mday(int,int) member function to obtain the number of days in the received month for the received year. The month value can be between 1 and 12 inclusive). If all of the data received is valid, this constructor stores the values received in the current object, calculates the comparator value, and sets the error state to NO_ERROR. If any of the data received is not valid, this constructor initializes the object to a safe empty state, sets the comparator value to 0 and sets the error state to NO_ERROR.

Use the following formula to set the comparator value for a valid date: = year * 372 + month * 13 + day

Operators

bool operator==(const Date& rhs) const;

bool operator!=(const Date& rhs) const;

bool operator<(const Date& rhs) const;

bool operator>(const Date& rhs) const;

bool operator<=(const Date& rhs) const;

bool operator>=(const Date& rhs) const;

These comparison operators return the result of comparing the current object as the left-hand side operand with another Date object as the right-hand side operand if the two objects are not empty. If one or both of them is empty, these operators return false.

For example operator< returns true if the Date stored in the current object is before the date stored in rhs; otherwise, this operator returns false.

Queries and modifier

int errCode() const; This query returns the error state as an error code value.

bool bad() const; This query returns true if the error state is not NO_ERROR.

std::istream& read(std::istream& istr); This function reads the date from the console in the following format: YYYY?MM?DD (e.g. 2016/03/24 or 2016-03-24). This function does not prompt the user. If istr fails at any point, this function sets the error state to CIN_FAILED and does NOT clear istr. If istr has failed, a call to istr.fail() returns true. If your read() function reads the numbers successfully, Regardless of the result of this input process, this function returns a reference to the std::istream object.

std::ostream& write(std::ostream& ostr) const; This query writes the date to an std::ostream object in the following format: YYYY/MM/DD, and then returns a reference to the std::ostream object. Helper functions: operator<< This operator works with an std::ostream object to print a date to the console. operator>> This operator works with an std::istream object to read a date from the console. Use the read and write member functions in these operators; DO NOT use friends for these operator overloads. Include the prototypes for these two operators in the header file. Place their prototypes after the class definition.


Solution

  • I am not sure what are you aiming for. But the common practice is to divide you projects into sub project directories. And then separate the header(.h, .hpp etc) and source(.c, .cpp, .cxx etc) files. Take a division motivating with the Single Responsibility Principle