1. Define a C++ class in a file person.h with the following declarations: #inclu
ID: 3809594 • Letter: 1
Question
1. Define a C++ class in a file person.h with the following declarations:
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
class person
{
private:
string name;
int feet;
int inches;
int weight;
public:
person ();
void input ();
void grow (int i);
void shrink (int i);
void gain (int w);
void lose (int w);
void print ();
};
The private data represents a person’s name, height measured in feet and inches and weight measured in pounds.
The public methods are:
person: Constructor that sets name equal to “Unassigned” and integer values equal to zero.
input: Prompts the user for name, feet, inches and weight and sets the private data values to these user entered values. Checks for negative values for feet, inches, and weight and for inches greater than 11. If any of these error conditions occurs, an error message should be printed and no values should be set.
grow: Increases the person’s height by the given number of inches. Feet and inches should be correctly set based on the increase. If the given number of inches is negative, an error message should be printed and the values not changed.
shrink: Decreases the person’s height by the given number of inches. Feet and inches should be correctly set based on the decrease. If the given number of inches is negative, an error message should be printed and the values not changed. If the decrease in inches gives a total height value less than 1 inch, an error message should be printed and the values not changed.
gain: Increases the person’s weight by the given weight. If the given weight is negative, an error message should be printed and the values not changed.
lose: Decreases the person’s weight by the given weight. If the given weight is negative, an error message should be printed and the values not changed. If the decrease in weight gives a total weight value less than 1 pound, an error message should be printed and the values not changed.
print: Prints the name, height and weight with labels and nice formatting. It also computes and prints the person’s body mass index, BMI. The formula for this is:
BMI = weight * 703
------------
inches2 (Note: this is total height in inches squared)
The BMI should be printed with two places after the decimal. See the example for the format the output should follow.
2. Write a personapp.cpp application program that utilizes the person object. It should be menu driven with choices for inputting a person’s statistics, growing, shrinking, gaining weight, losing weight, printing the person’s statistics and quitting.
The menu printed should be:
Enter one of the following:
e: Enter a persons's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
The values, text, and spacing of your program should closely follow this below example.
Sample run with user input in bold:
Enter one of the following:
e: Enter a person's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
Command: p
Name: Unassigned
Height: 0 feet, 0 inches
Weight: 0 pounds
BMI: 0.00
Enter one of the following:
e: Enter a person's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
Command: e
Enter the name: Neal
Enter height
Feet: 5
Inches: 6
Weight: 163
Enter one of the following:
e: Enter a person's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
Command: p
Name: Neal
Height: 5 feet, 6 inches
Weight: 163 pounds
BMI: 26.31
Enter one of the following:
e: Enter a person's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
Command: g
Enter a number of inches to grow: 4
Enter one of the following:
e: Enter a person's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
Command: d
Enter a number of pounds to decrease: 6
Enter one of the following:
e: Enter a person's statistics
g: Grow by a number of inches
s: Shrink by a number of inches
i: Increase in weight
d: Decrease in weight
p: Print the current statistics
q: Quit the program
Command: p
Explanation / Answer
Here is the code for Person.h:
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
class Person
{
private:
string name;
int feet;
int inches;
int weight;
public:
Person ();
void input ();
void grow (int i);
void shrink (int i);
void gain (int w);
void lose (int w);
void print ();
};
//person: Constructor that sets name equal to “Unassigned” and integer values equal to zero.
Person::Person()
{
name = "Unassigned";
feet = inches = weight = 0;
}
// input: Prompts the user for name, feet, inches and weight and sets the private data
// values to these user entered values. Checks for negative values for feet, inches, and
// weight and for inches greater than 11. If any of these error conditions occurs, an
// error message should be printed and no values should be set.
void Person::input()
{
string nm;
int ft, in, wt;
cout << "Enter the name: ";
cin >> nm;
cout << "Feet: ";
cin >> ft;
cout << "Inches: ";
cin >> in;
cout << "Weight: ";
cin >> wt;
if(ft > 0 && in > 0 && wt > 0 && in < 12)
{
name = nm;
feet = ft;
inches = in;
weight = wt;
}
else
cout << "Invalid values entered. The input is not being set..." << endl;
}
// grow: Increases the person’s height by the given number of inches. Feet and inches
// should be correctly set based on the increase. If the given number of inches is negative,
// an error message should be printed and the values not changed.
void Person::grow(int i)
{
if(i < 0)
cout << "Cannot grow with negative values..." << endl;
else
{
inches += i;
feet += inches / 12;
inches = inches % 12;
}
}
// shrink: Decreases the person’s height by the given number of inches. Feet and inches
// should be correctly set based on the decrease. If the given number of inches is
// negative, an error message should be printed and the values not changed. If the
// decrease in inches gives a total height value less than 1 inch, an error message should
// be printed and the values not changed.
void Person::shrink(int i)
{
if(i >= feet * 12 + inches)
cout << "Cannot shrink beyond his/her height..." << endl;
else if(i < 0)
cout << "Cannot shrink with negative values..." << endl;
else
{
int temp = feet * 12 + inches - i;
feet = temp / 12;
inches = temp % 12;
}
}
// gain: Increases the person’s weight by the given weight. If the given weight is
// negative, an error message should be printed and the values not changed.
void Person::gain(int w)
{
if(w < 0)
cout << "Cannot gain with negative values..." << endl;
else
weight += w;
}
// lose: Decreases the person’s weight by the given weight. If the given weight is
// negative, an error message should be printed and the values not changed. If the
// decrease in weight gives a total weight value less than 1 pound, an error message
// should be printed and the values not changed.
void Person::lose(int w)
{
if(w >= weight)
cout << "Cannot lose beyond his/her weight..." << endl;
else if(w < 0)
cout << "Cannot lose with negative values..." << endl;
else
weight -= w;
}
// print: Prints the name, height and weight with labels and nice formatting. It also
// computes and prints the person’s body mass index, BMI. The formula for this is:
//BMI = weight * 703
// ------------
// inches2 (Note: this is total height in inches squared)
//The BMI should be printed with two places after the decimal.
// See the example for the format the output should follow.
void Person::print()
{
double BMI = weight * 703 / ((feet * 12 + inches) * (feet * 12 + inches));
cout << "Name: " << name << endl;
cout << "Height: " << feet << " feet, " << inches << " inches" << endl;
cout << "Weight: " << weight << " pounds" << endl;
cout << "BMI: " << fixed << setprecision(2) << BMI << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.