Question
Done in C++ (Class) Implement a User class. Create the specification file (User.h) and implementation file (User.cpp). User class has the following attributes: char firstN
Done in C++
(Class) Implement a User class. Create the specification file (User.h) and implementation file (User.cpp). User class has the following attributes: char firstName[6]; char lastName[6]; and dateOfBirth as DateType. The following constructor and functions must be implemented in the User class:
- Default constructor User()
- Void Initialize( char firstName[6], char lastName[6], DateType date): this function is to initialize a user object with given values.
- RelationType comparedTo(User* aUser) const: this function compare the self user with aUser and return a ReliationType value. It must call the CompareTo(DateType aDate) function of the DateType class (see case study in chapter 2). The rule of comparison and returning values is as follow:
- Returns LESS: if CompareTo(DateType aDate) returns LESS
- Return GREATER: if CompareTo(DateType aDate) returns GREATER
- Return EQUAL: if (both self user with aUser object have same firstName and lastName) and CompareTo(DateType aDate) returns EQUAL
(Note that the RelationType is defined as an enum: enum RelationType{LESS, EQUAL, GREATER} )
- string ToString(): this function returns a string that contains all information of the user (firstName, lastName, and dateOfBirth).
DateType.h:
#include
DateType.cpp:
//File DataType.cpp contains the implementation of class DataType #include "DateType.h" #include
// Numbers of days in each month static int daysInMonth[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//Names of the months static string conversionTable[] = {"Error", "January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December"};
void DateType::Initialize (int newMonth, int newDay, int newYear) /* Post: if newMonth, newDay and newYear represents a valid date year is set to newYear; month is set to newMonth; day is set to newDay; otherwise a string exception is thrown, stating the first incorrect parameter */ { if (newMonth < 1 || newMonth>12) throw string("Month is invalid"); else if (newDay < 1 || newDay > daysInMonth[newMonth]) throw string("Day is invalid"); else if (newYear < 1583) throw string("Year is invalid"); year = newYear; month = newMonth; day = newDay; } int DateType::GetMonth() const //Accessor function for data member month { return month; }
string DateType::GetMonthAsString() const //returns data member as a string { return conversionTable[month]; }
int DateType::GetYear() const //Accessor function for data member year. { return year; }
int DateType::GetDay() const //Accessor function for data member day { return day; }
RelationType DateType::ComparedTo(DateType aDate) const /* Pre: self and aDate have been initialized post: function value = LESS, if self comes before aDate. = EQUAL, if self is the same as aDate. = GREATER, if self comes after aDate. */ { if (year < aDate.year) return LESS; else if (year > aDate.year) return GREATER; else if (month < aDate.month) return LESS; else if (month > aDate.month) return GREATER; else if (day < aDate.day) return LESS; else if (day > aDate.day) return GREATER; else return EQUAL; }
DateType DateType::Adjust(int daysAway) const //Pre: self has been initialized //Post: function value =newDate daysAway from self { int newDay = day + daysAway; int newMonth = month; int newYear = year; bool finished = false; int daysInThisMonth; DateType returnDate; while (!finished) { daysInThisMonth = daysInMonth[newMonth]; if (newMonth == 2) if (((newYear % 4 == 0) && !(newYear % 100 == 0)) || (newYear % 400 == 0)) daysInThisMonth++; if (newDay <= daysInThisMonth) finished = true; else { newDay = newDay - daysInThisMonth; newMonth = (newMonth % 12) + 1; if (newMonth == 1) newYear++; } }
returnDate.Initialize(newMonth, newDay, newYear); return returnDate; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started