1. Define a C++ class in a file person.h with the following declarations: #inclu
ID: 673705 • 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
Explanation / Answer
******** person.h ********
#include <cstdlib>
#include <string>
#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()
{
string named;
int f=0;
int i=0;
int w=0;
while(f <=0 || i <= 0 || w <= 0)
{
cout << " Enter Name: ";
cin.clear();
cin.sync();
getline(cin,named);
cout << "Enter Height";
cout <<endl<< "Feet: ";
cin >> f;
cout << "Inches: ";
cin >> i;
cout << "Enter Weight: ";
cin >>w;
if (f <=0 || i <= 0 || w <= 0)
{
if (f>0)
{
break;
}
else if(i>0)
{
break;
}
else
{
cout << "Illegal data entry! You'll need to try again! ";
f = 0;
i = 0;
w = 0;
}
}
}
name = named;
weight = w;
inches=i;
feet=f;
if (inches>=12)
{
feet+=inches/12;
inches=inches%12;
}
}
void person::grow(int i)
{
if (i <= 0)
{
cout << "Number of inches to grow must be positive!"<<endl;
i = 0;
}
else
{
inches = inches +i;
}
if (inches>=12)
{
feet+=inches/12;
inches=inches%12;
}
}
void person::shrink(int i)
{
int temp=0;
if (i <= 0)
{
cout << "Number of inches to shrink must be positive and not 0!"<<endl;
i = 0;
}
else if ((feet*12)+(inches-i)<=0)
{
cout<<"HOW IS THAT POSSIBLE...TRY AGAIN"<<endl;
}
else
{
if (inches<i)
{
temp = ((feet*12)+(inches))-i;
inches=(((feet*12)+(inches))-i)%12;
feet=temp/12;
}
else
inches -=i ;
}
}
void person::gain (int w)
{
if (w <= 0)
{
cout << "Number of pounds to gain must be positive and not 0!"<<endl;
w = 0;
}
else
{
weight = w + weight;
}
}
void person::lose (int w)
{
if (w <= 0)
{
cout << "Number of pounds to lose must be positive and not 0!"<<endl;
w = 0;
}
else if ((weight-w)<=0)
{
cout<<"HOW IS THAT POSSIBLE...TRY AGAIN"<<endl;
}
else
{
weight = weight - w;
}
}
void person::print ()
{
cout<<"----------------------------------------------------------------"<<endl
<< "INFORMATION"<<endl
<< "----------------------------------------------------------------"
<< " Name: " << name
<< " Height: " << feet << " feet, " << inches << " inches "
<< " Weight: " << weight << " pounds "<<endl
<<"----------------------------------------------------------------";
}
********** main.cpp *********
#include <cstdlib>
#include <iostream>
#include <string>
#include "person.h"
using namespace std;
void menu()
{
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"
<<" >> ";
}
int main()
{
int i,w;
char command;
person p;
p.input ();
p.print();
menu();
cin >> command;
while (command != 'q')
{
switch (command)
{
case 'e':
p.input();
break;
case 'g':
//corrected
cout<<endl<<"Enter Number Inches to grow>>";
cin >> i;
p.grow( i);
break;
case 's':
//corrected
cout<<endl<<"Enter Number Inches to shrink>>";
cin >> i;
p.shrink(i);
break;
case 'i':
//corrected
cout<<endl<<"Enter Number Weight to gain>>";
cin>>w;
p.gain(w);
break;
case 'd':
//corrected
cout<<endl<<"Enter Number Weight to shrink>>";
cin>>w ;
p.lose(w);
break;
case 'p': p.print();
break;
default: cout << "You must enter either e, g, s, i, d, p, or q!";
}
menu();
cin >> command;
}
cout << " Thank you!!!";
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.