Create a Person class to represent a person's contact information. Your Person class must consist of the following:

PRIVATE members:
string firstName,lastName;
birthDate //you get to choose the data type, but the type you choose must be able to maintain a
// 1 day resolution

PUBLIC members:

1. Default constructor that sets the first name to "", last name to "", and the date of birth to January 1st, 2000.

2. Copy constructor

3. Initialization constructor that takes 3 arguments - 1 const string for each first name and last name, and a third argument of the type you use for your Person's age.

4. Three Accessor methods that each return one of the private data members - the first name, last name, and date of birth as strings (i.e. string type). In other words, you must implement these methods:

string getFirstName() const;
string getLastName() const;
string getBirthDate() const;

5. A method that compares the age of this person with another person's age, returning the number of days old this person is to the others. This method must have the following prototype:

int ageDifference(const Person& p); // returns #days older this person is than Person p

FRIEND FUNCTIONS:

ostream& operator<<(ostream& o, const Person& p); bool operator==(const Person& p1, const Person& p2); //returns true if p1 and p2 have same //first and last names AND the same birthdate. bool operator<(const Person& p1, const Person& p2); // returns true if p1 comes first alphabetically // look at last names first, then first names if // the last names are the same bool operator>(const Person& p1, const Person& p2); //returns true if p2 comes first alphabetically


I have no problems with steps 1 through 4. When it came down to step 5, I have no clue what to do. My problem is constructing ageDiffference and other methods that can support it. I am stuck with a string. (ex "01/01/2001") How can I do subtraction with strings?

Person() : fN(""), lN(""),BD(()){}
Person(const string& fn,const string& ln,const string& dob):
	    fN(fn), lN(ln), bD(dob) {}

int ageDifference(const Person& p);

For example I want my output for this case to be Micheal is 365 days older.
Assume every month is 30 days. No leap years.

int main{
Person a("Michael", "", "7/25/1980");
Person b("Marius", "", "6/25/1981");
cout << a.getFirstName() << " is " << a.ageDifference(b) << " days older than " << b.getFirstName() << endl;
}

I've been thinking about using tt_time but I have no clue how that works.

sixthsensess commented: ` +0

Recommended Answers

All 3 Replies

Once again, the solution is to implement a string2int function. It will convert digits from string form to int form (if they are given in the right form). String2int implementations are found on this forum.

You might want to implement your own function which would also separate the months/days/years using a '/' delimiter and multiply by 30/1/365 respectively

#include <sstream>
#include <iostream>
#include <string>

unsigned long diff(std::string);

int main ()  {
   std::cout << diff("7/12/1993");
 }

unsigned long diff(std::string date)  {   
   int dmy[3];
   std::string hld[3];
   std::string::iterator prs=date.begin();   

   for (int n=0;n<3;n++)  {
      while (prs!=date.end())  {
         if ((*prs)=='/') {
             ++prs;  
             break;
           }
         hld[n]+=(*prs);
         std::istringstream i(hld[n]);
         i >> dmy[n];
         ++prs;
        }
     }
    return dmy[0]+(dmy[1]*31)+(dmy[2]*365);
  }

Thanks alot caut_baia. The example you gave was very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.