Question
How can I complete the implementation of a Person class. #ifndef PERSON_H #define PERSON_H #include #include #include Address.h #include Date.h using namespace std; // interface
How can I complete the implementation of a Person class.
#ifndef PERSON_H
#define PERSON_H
#include
#include
#include "Address.h"
#include "Date.h
using namespace std;
// interface of Person class (what will be the header file) class Person { public: // member functions // constructors Person(); // default constructor - no name, address, DOB, phone Person(string ); // just a name nothing else Person(string, Address ); Person(string, Address, Date ); Person(string, Address, Date, string ); // mutators - setters void set_name(string ); void set_address(Address ); void set_dob(Date ); void set_phone(string ); void read(); void read(ifstream& ); // accessors - getters string get_name() const; Address get_address() const; Date get_dob() const; string get_phone() const; int get_age() const; void display() const; void print(ofstream& ) const; private: // data members string name; Address addr; Date dob; string phone; };
#endif
Also, add the following member functions to Address. #ifndef ADDRESS_H #define ADDRESS_H #include #include using namespace std; // interface of Address class (what will be the header file) class Address { public: // member functions // constructors Address(); // default constructor Address(int, string, string, string, int ); // mutators - setters void read(); void read(ifstream& ); void change_address(Address ); // accessors - getters void display() const; void print(ofstream& ) const; bool equals(const Address& ) const; private: // data members int street_number; string street_name; string town; string city; int zip_code; }; #endif The driver program should test all member functions in Person and the newly added member functions to Address as well.
people.txt file is:-
Leonhard Euler
75 Grasslands Rd
Valhalla, NY 10595
4/15/1707
914-606-6235
Leonardo Da Vinci
75 Grasslands Rd
Valhalla, NY 10595
4/15/1452
914-606-6235
Donald Knuth
450 Serra Mall
Stanford, CA 94305
1/10/1938
650-723-2300
Bjarne Stroustrup
116th and Broadway
New York, NY 10027
12/30/1950
212-854-1754
Seymour Cray
1050 Lowater Rd
Chippewa Falls, WI 54729
9/28/1925
715-723-5558
Albert Einstein
70 Washington Rd
Princeton, NJ 08544
3/14/1879
609-258-3000
Ricki Lake
330 roast meat hill rd
Killingworth, ct 06419
9/21/1968
860-663-1765
Lonnie Warneke
1140 Farfrompoopen Rd
fannie township, AR 71970
3/28/1909
888-555-1212
Date.h
#ifndef DATE_H #define DATE_H #include#include using namespace std; class Date { public: /** * Constructs a default Date object set to todays' date. */ Date(); /** * Constructs a Date object with year set to current year. * @param m the month * @param d the day */ Date(int m, int d); /** * Constructs a Date object. * @param m the month * @param d the day * @param y the year */ Date(int m, int d, int y); /** * Reads in a date as MM/DD/YYYY and sets * the month, day and year accordingly */ void read(); /** * Reads in a date as MM/DD/YYYY from file in * and sets the month, day and year accordingly */ void read(ifstream& ); /** * Reads in a date as MM DD YYYY from file in * and sets the month, day and year accordingly */ void read2(istream& ); /** * Sets a Date to the given month, day, and year. * @param m the month * @param d the day * @param y the year */ void set_date(int m, int d, int y); /** * Gets the month of this date. * @return the month */ int get_month() const; /** * Gets the day of this date. * @return the day */ int get_day() const; /** * Gets the year of this date. * @return the year */ int get_year() const; /** * Displays this date in form MM/DD/YYYY. */ void print_date1() const; /** * Displays this date in form monthName DD, YYYY. */ void print_date2() const; /** * Displays this date in form MM/DD/YYYY. */ void print_date1(ofstream& ) const; /** * Calculates the number of days since Jan 1 of the same year. * @return the number of days since start of year */ int day_number() const; /** * Calculates the number of days since Jan 1 of year 0000. * @return the number of days since the dawn of time */ int days_since() const; /** * Determine if this date is on a leap year. * @return true if this year is a leap year, false otherwise */ bool is_leap_year() const; /** * Determines the name of the month for this date. * @return the name of the month as a string */ string month_name() const; /** * Determines the name of the day of the week for this date. * @return the name of the day of the week as a string */ string day_name() const; /** * Determines whether this date is equal to other. * @return true if other equals this date false otherwise. */ bool equals(const Date& other) const; /** * Compares this date to other and returns the number of days * since this date and the earlier other date * @returns negative if this date is earlier than other * zero if this date is equal to other * positive if this date is later than other */ int compare_to(const Date& other) const; private: int month; int day; int year; }; #endif
Date.cpp
#include#include #include #include "Date.h" using namespace std; // constructors Date::Date() { time_t today = time(0); // get time now struct tm &now = *localtime( &today ); month = now.tm_mon + 1; day = now.tm_mday; year = now.tm_year + 1900; } Date::Date(int m, int d) { time_t today = time(0); // get time now struct tm &now = *localtime( &today ); year = now.tm_year + 1900; assert(1 <= m && m <= 12); month = m; if (m == 4 || m == 6 || m == 9 || m == 11) assert(1 <= d && d <= 30); else if (m != 2) assert(1 <= d && d <= 31); else if (is_leap_year()) assert(1 <= d && d <= 29); else assert(1 <= d && d <= 28); day = d; } Date::Date(int m, int d, int y) { assert(1 <= m && m <= 12); month = m; assert(y >= 0); year = y; if (m == 4 || m == 6 || m == 9 || m == 11) assert(1 <= d && d <= 30); else if (m != 2) assert(1 <= d && d <= 31); else if (is_leap_year()) assert(1 <= d && d <= 29); else assert(1 <= d && d <= 28); day = d; } // mutators void Date::read() { char slash; cin >> month >> slash >> day >> slash >> year; } void Date::read(ifstream& in) { char slash; in >> month >> slash >> day; if (in.peek() != ' ') in >> slash >> year; } void Date::read2(istream& in) { char slash; in >> month >> slash >> day; if (in.peek() != ' ') in >> slash >> year; } void Date::set_date(int m, int d, int y) { month = m; day = d; year = y; } // accessor int Date::get_month() const { return month; } int Date::get_day() const { return day; } int Date::get_year() const { return year; } void Date::print_date1() const { cout << month << "/" << day << "/" << year; } void Date::print_date1(ofstream& out) const { out << month << "/" << day << "/" << year; } void Date::print_date2() const { cout << month_name() << " " << day << ", " << year; } int Date::day_number() const { // 31 28 31 30 31 30 31 31 30 31 30 31 int DAYS_IN_MONTHS[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int day_count = DAYS_IN_MONTHS[month - 1] + day; if (is_leap_year() && month > 2) day_count++; return day_count; } int Date::days_since() const { int day_count = (year - 1) * 365; day_count += (year - 1) / 4; day_count -= (year - 1) / 100; day_count += (year - 1) / 400; day_count += day_number(); return day_count; } bool Date::is_leap_year() const { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true; else return false; } string Date::month_name() const { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } } string Date::day_name() const { /* Zeller's congruence is an algorithm devised by Christian Zeller to calculate the day of the week for any Gregorian calendar date. h = (d + (13*(m + 1)/5 + K + K/4 + J/4 - 2*J) % 7 where h is the day of the week (0=Saturday, 1=Sunday, 2=Monday, ..., 6=Friday) d is the day of the month m is the month (3=March, 4=April, 5=May, ..., 14=February) K the year of the century (y % 100) J is the zero-based century (y / 100) For example, the zero-based centuries for 1995 and 2000 are 19 and 20 respectively (to not be confused with the common ordinal century enumeration which indicates 20th for both cases). NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (14/2/2009 in MM/DD/YYYY format) */ } bool Date::equals(const Date& other) const { return (month == other.month && day == other.day && year == other.year); } int Date::compare_to(const Date& other) const { return days_since() - other.days_since(); }
Address.h
#ifndef ADDRESS_H #define ADDRESS_H #include#include using namespace std; // interface of Address class (what will be the header file) class Address { public: // member functions // constructors Address(); // default constructor Address(int, string, string, string, int ); // mutators - setters void read(); void read(ifstream& ); void change_address(Address ); // accessors - getters void display() const; void print(ofstream& ) const; bool equals(const Address& ) const; private: // data members int street_number; string street_name; string town; string city; int zip_code; }; #endif
Person.h
#ifndef PERSON_H #define PERSON_H #include#include #include "Address.h" #include "Date.h" using namespace std; // interface of Person class (what will be the header file) class Person { public: // member functions // constructors Person(); // default constructor - no name - no address - no DOB - no phone Person(string ); // just a name nothing else Person(string, Address ); Person(string, Address, Date ); Person(string, Address, Date, string ); // mutators - setters void set_name(string ); void set_address(Address ); void set_dob(Date ); void set_phone(string ); void read(); void read(ifstream& ); // accessors - getters string get_name() const; Address get_address() const; Date get_dob() const; string get_phone() const; int get_age() const; void display() const; void print(ofstream& ) const; private: // data members string name; Address addr; Date dob; string phone; }; #endif
Address.cpp
#include#include #include "Address.h" using namespace std; // non-member helper functions void to_upper(string& s) { for (int i = 0; i < s.length(); i++) if (s[i] >= 'a' && s[i] <= 'z') s[i] -= 32; } void capitalize(string& s) { int len = s.length(); int space1 = s.find(' '); if (s[0] >= 'a' && s[0] <= 'z') s[0] -= 32; if (space1 < len) { if (s[space1 + 1] >= 'a' && s[space1 + 1] <= 'z') s[space1 + 1] -= 32; // look for 2nd space int space2 = s.find(' ', space1 + 1); if (space2 < len) { if (s[space2 + 1] >= 'a' && s[space2 + 1] <= 'z') s[space2 + 1] -= 32; // look for 3rd space int space3 = s.find(' ', space2 + 1); if (space3 < len) { if (s[space3 + 1] >= 'a' && s[space3 + 1] <= 'z') s[space3 + 1] -= 32; } } } } // implementation of the Address class member functions (Address.cpp) Address::Address() // default constructor : street_number(0), zip_code(0) { } Address::Address(int sn, string n, string t, string c, int z) : street_number(sn), street_name(n), town(t), city(c), zip_code(z) { } void Address::read() { string garbage; cout << "Enter the street number: "; cin >> street_number; getline(cin, garbage); cout << "Enter the street name: "; getline(cin, street_name); capitalize(street_name); cout << "Enter the town: "; getline(cin, town); capitalize(town); cout << "Enter the city (2 Letter code): "; cin >> city; city = city.substr(0, 2); to_upper(city); cout << "Enter the zip code: "; cin >> zip_code; } void Address::change_address(Address new_addr) { street_number = new_addr.street_number; street_name = new_addr.street_name; town = new_addr.town; city = new_addr.city; zip_code = new_addr.zip_code; } void Address::display() const { cout << street_number << " " << street_name << endl; cout << town << ", " << city << " " << setw(5) << setfill('0') << zip_code << endl; }
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