CS 1440 Lab 8

Cover sheetPrevious ActivityNext Activity


Activity 8-3: A Person class

Activity Goals

        More practice with C++ classes

        Passing class objects as parameters and return types

Activity Procedure

Copy and paste the Date class definition and the Date:: function definitions from the previous activity into a file named Act3.C. 

Add to Act3.C the definition of a Person class.  A "Person" has a name (which is a string) and a birthday (which is a Date).  We can "set" the values of Person's data members and Display Personal information.

Want to try to define the class yourself?  Go ahead, you can peek below if you get stuck.

 

 

class  Person
{
public:
     void  set(string n, Date bd);
     void  Display();
private:
     string name;
     Date birthday;
};

 

Here are the class function definitions (notice how a Date object is passed as a parameter):

void  Person::set(string n, Date bd)
{
     name = n;
     birthday = bd;
}
void  Person::Display()
{
     cout  <<  name  <<  " has a birthday on ";
     birthday.ShortDisplay();
     cout  <<  endl;
}

 

 

Here is the main function AND another function that just gets Dates.  Notice that this is not part of the Date class, and that it returns a Date object.

int  main()
{
     Person  mom;
     string  name;
     Date  bday;

     cout  <<  "Enter mom's name: ";
     getline(cin, name);
     bday  =  getDate();

     mom.set(name, bday);
     mom.Display();

     return 0;
}

/* Date is return type; this is NOT class function */
Date  getDate() 
{
     Date  date;
     int  m, d, y;

     cout  <<  "Enter month: ";
     cin  >>  m;
     cout  <<  "Enter day: ";
     cin  >>  d;
     cout  <<  "Enter year: ";
     cin  >>  y;

     date.set(m,d,y);

     return date;
}

 

Activity Followup

Put it all together: the Date class, the Person class, the main, the getDate, and whatever header files are needed.  Make sure it works by compiling and running it.