1. Define a C++ class in a file person.h with the following declarations: #inclu
ID: 3854837 • 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
There is a lengthy sample output of the program on Blackboard. The values, text, and spacing of your program should closely follow that example.
Explanation / Answer
#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::person()
{
name="Unassigned";
feet=0;
inches=0;
weight=0;
}
void person::input()
{
cout<<"Enter name ";
cin>>name;
cout<<"Enter height in feet ";
int hf;
cin>>hf;
if(hf<0)
cout<<"Height is less than 0 ";
else
feet=hf;
cout<<"Enter height in inches ";
int hi;
cin>>hi;
if(hi<0 )
cout<<"Height is less than 0 ";
else if(hi>11)
cout<<"Height is greater than 11 ";
else
inches=hi;
cout<<"Enter weight in pound ";
int w;
cin>>w;
if(w<0)
cout<<"weight is less than 0 ";
else
weight=w;
}
void person::grow(int i)
{
if(i<0)
cout<<"Inches cannot be negative ";
else if(i>=11)
{
int div=i/11;
i=i%11;
inches=0;
inches+=i;
feet+=div;
}
else
inches+=i;
}
void person::shrink(int i)
{
if(i<0)
cout<<"Inches cannot be negative ";
else if(i>=11)
{int div=i/11;
i=i%11;
inches-=i;
feet-=div;
}
else
inches-=i;
}
void person::gain(int w)
{
if(w<0)
cout<<"Weight is less than 0 ";
else
weight+=w;
}
void person::lose(int w)
{
if(w<0)
cout<<"Weight is less than 0 ";
else
weight-=w;
}
void person::print()
{
cout<<"Name is "<<name<<" ";
cout<<"Heigth is in feet"<<feet<<" ";
cout<<"Heigth is in inches"<<inches<<" ";
cout<<"Weight is "<<weight<<" ";
int total_inches=feet*11+inches;
int bmi=weight*703/(total_inches*total_inches);
cout<<"BMI is "<<bmi<<" ";
}
===========================================================================
personapp.cpp
#include "person.cpp"
using namespace std;
class str;
int main()
{
person p;
char ch;
int flag=true;
while(flag)
{
cout<<"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 ";
cin>>ch;
int i;
switch(ch)
{
case 'e':p.input();
break;
case 'g':cin>>i;
p.grow(i);break;
case 's':cin>>i;
p.shrink(i);break;
case 'i':cin>>i;
p.gain(i);break;
case 'd':cin>>i;
p.lose(i);break;
case 'p':
p.print();break;
case 'q':
flag=false;break;
}
}
return 0;
}
======================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ personapp.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
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
e
Enter name
ak
Enter height in feet
5
Enter height in inches
11
Enter weight in pound
123
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
g
11
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
p
Name is ak
Heigth is in feet6
Heigth is in inches0
Weight is 123
BMI is 19
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
s
11
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
p
Name is ak
Heigth is in feet5
Heigth is in inches0
Weight is 123
BMI is 28
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
i
123
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
p
Name is ak
Heigth is in feet5
Heigth is in inches0
Weight is 246
BMI is 57
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
d
123
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
p
Name is ak
Heigth is in feet5
Heigth is in inches0
Weight is 123
BMI is 28
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
q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.