Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

The struct and class definitions for Person should be expanded so that a Person besides having a birthday, also has a Date for when their

The struct and class definitions for Person should be expanded so that a Person besides having a birthday, also has a Date for when their height and weight was measured. Youll need to edit both the class and struct descriptions as well as the main functions which exercise them.

You will modify the StructOne.cpp and ClassOne.cpp files so that they produce output like that in the Listings at the end of this document. This is where you need to do the planning.

1. This dialog demonstrates the desired operation of StructOne.cpp source code when modified to include this new capability, now referred to as Lab03 struct.cpp.

H:\Temp\Lab03_struct\Debug> Lab03_struct.exe

Enter month as integer 1-12: 3

Enter day as integer 1-31: 21

Enter year as integer: 2002 Printing the dates:

10/6/2000

3/21/2002

Birthday:

Enter month as integer 1-12: 5

Enter day as integer 1-31: 10

Enter year as integer: 1991

Measure date:

Enter month as integer 1-12: 10

Enter day as integer 1-31: 6

Enter year as integer: 1962

Joe was born in 1991

Height is 72

Weight is 180

Last measured on 10/6/1962

Birthday is 5/10/1991

And here is Fred.

Height is 74 Weight is 200

Last measured on 6/21/2007

Birthday is 11/27/1968

H:\Temp\Lab03_struct\Debug>

2. The following dialog demonstrates the desired operation of ClassOne.cpp source code when modified to include this new capability, now referred to as Lab03 class.cpp.

H:\Temp\Lab03_class\Debug> Lab03_class.exe

Enter month as integer 1-12: 3

Enter day as integer 1-31: 21

Enter year as integer: 2002

Printing the date:

3/21/2002

Birthday:

Enter month as integer 1-12: 5

Enter day as integer 1-31: 19

Enter year as integer: 1993

Measure date:

Enter month as integer 1-12: 10

Enter day as integer 1-31: 6

Enter year as integer: 1962

Joe was born in 1993

And here is Joe:

Height is 72

Weight is 180

Last measured on 10/6/1962

Birthday is 5/19/1993

Joe was last measured in 1962

H:\Temp\Lab03_class\Debug>

STRUCTONE.cpp:

#include

using std::cin;

using std::cout;

using std::endl;

// Declare a simple structure to hold a date.

struct Date

{

int month;

int day;

int year;

};

// Here are prototypes for access functions associated with Date.

void fillDate( Date& );

void showDate( Date );

// A Date can be used as a component of another structure.

struct Person

{

int height;

int weight;

Date birthday;

};

// Here is a prototype for a function to access a Person

void showPerson( Person );

int main()

{

Date first={10,6,2000}; // Here is initialization on the fly.

Date second; // No initialization, so need to fill later.

cout << endl;

fillDate(second); // Here is initialization with a helper

function.

cout << " Printing the dates:" << endl;

showDate(first); // Then display the results for either.

showDate(second);

Person Joe; // Some examples for manipulating a person

Joe.height=72;

Joe.weight=180;

cout << endl;

fillDate(Joe.birthday);

cout << endl << " Joe was born in " << Joe.birthday.year << endl;

cout << "Here's Joe:" << endl;

showPerson(Joe);

Person Fred={74,200,{11,27,1968}}; // Here's the direct initializer.

cout << " And here is Fred." << endl;

showPerson(Fred);

cout << endl;

return(0);

}

void fillDate(Date& the_date)

{

int tmp;

// Enter the month with some verification of data.

do {

cout << "Enter month as integer 1-12: ";

cin >> tmp;

} while( tmp<1 || tmp>12);

the_date.month=tmp;

// Enter the day, limited verification.

do {

cout << "Enter day as integer 1-31: ";

cin >> tmp;

} while( tmp<1 || tmp>31);

the_date.day=tmp;

cout << "Enter year as integer: ";

cin >> the_date.year;

}

void showDate(Date the_date)

{

cout << the_date.month << "/"

<< the_date.day << "/"

<< the_date.year << endl;

}

void showPerson(Person the_person)

{

cout << "Height is " << the_person.height << endl

<< "Weight is " << the_person.weight << endl

<< "Birthday is ";

showDate(the_person.birthday);

}

ClassOne.cpp:

#include

using std::cin;

using std::cout;

using std::endl;

// Declare a simple class to hold a date.

class Date

{

private:

int month;

int day;

public:

int year;

void fill(void );

void show(void );

};

// A Date can be used as a component of another class

class Person

{

public:

int height;

int weight;

Date birthday;

void show(void);

};

int main()

{

Date first; // Declaration with no initialization.

cout << endl;

first.fill(); // Here is initialization with a member function.

cout << " Printing the date:" << endl;

first.show(); // Then display the results for either.

Person Joe; // Some examples for manipulating a person

Joe.height=72; // Because these fields are public, we have direct

access.

Joe.weight=180;

cout << endl;

Joe.birthday.fill();

cout << " Joe was born in " << Joe.birthday.year << endl;

cout << "Here's Joe:" << endl;

Joe.show();

cout << endl;

return(0);

}

// Here are the member function definitions. Note use of scoping

operator ::

void Date::fill(void)

{

int tmp;

// Enter the month with some verification of data.

do {

cout << "Enter month as integer 1-12: ";

cin >> tmp;

} while( tmp<1 || tmp>12);

month=tmp;

// Enter the day, limited verification.

do {

cout << "Enter day as integer 1-31: ";

cin >> tmp;

} while( tmp<1 || tmp>31);

day=tmp;

cout << "Enter year as integer: ";

cin >> year;

cout << "You entered this date: \t";

show();

}

void Date::show(void)

{

cout << month << "/"

<< day << "/"

<< year << endl;

}

void Person::show(void)

{

cout << "Height is " << height << endl

<< "Weight is " << weight << endl

<< "Birthday is ";

birthday.show();

}

Uglyclass.cpp (not sure if this code is needed???)

#include

using std::cin;

using std::cout;

using std::endl;

// Declare a simple structure to hold a date.

struct Date

{

int month;

int day;

int year;

void fill(void);

void show(void);

};

// A Date can be used as a component of another structure.

struct Person

{

int height;

int weight;

Date birthday;

void show(void);

};

int main()

{

Date first={10,6,2000}; // Here is initialization on the fly.

Date second;

cout << endl;

second.fill(); // Here is initialization with a helper function.

cout << " Printing the dates:" << endl;

first.show(); // Then display the results for either.

second.show();

Person Joe; // Some examples for manipulating a person

Joe.height=72;

Joe.weight=180;

cout << endl;

Joe.birthday.fill();

cout << " Joe was born in " << Joe.birthday.year << endl;

cout << "Here's Joe:" << endl;

Joe.show();

Person Fred={74,200,{11,27,1968}}; // Here's a direct initializer

cout << " And here is Fred." << endl;

Fred.show();

cout << endl;

return(0);

}

void Date::fill(void)

{

int tmp;

// Enter the month with some verification of data.

do {

cout << "Enter month as integer 1-12: ";

cin >> tmp;

} while( tmp<1 || tmp>12);

month=tmp;

// Enter the day, limited verification.

do {

cout << "Enter day as integer 1-31: ";

cin >> tmp;

} while( tmp<1 || tmp>31);

day=tmp;

cout << "Enter year as integer: ";

cin >> year;

}

void Date::show(void)

{

cout << month << "/"

<< day << "/"

<< year << endl;

}

void Person::show(void)

{

cout << "Height is " << height << endl

<< "Weight is " << weight << endl

<< "Birthday is ";

birthday.show();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions