Due: Wednesday, March 8, 2017 1. Define a C++ class in a file person.h with the
ID: 3854359 • Letter: D
Question
Due: Wednesday, March 8, 2017
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(){
}
void person::input(){
cout << "Enter Person Name :";
cin >> name;
cout << "Enter feet :";
cin >> feet;
cout << "Enter inches :";
cin >> inches;
cout << "Enter weight :";
cin >> weight;
}
void person::grow(int i){
if(i<0){
cout<<"Invalid input"<<endl;
return;
}
int f = i/12;
i = i%12;
feet +=f;
inches +=i;
}
void person::shrink(int i){
if(i<0){
cout<<"Invalid input"<<endl;
return;
}
int f = i/12;
i = i%12;
feet -=f;
inches -=i;
}
void person::gain(int w){
if(w<0){
cout<<"Invalid input"<<endl;
return;
}
weight += w;
}
void person::lose(int w){
if(w<0){
cout<<"Invalid input"<<endl;
return;
}
weight -= w;
}
void person::print(){
cout <<"Name :"<<name<<endl;
cout <<"Height : "<<feet<<" feet, "<<inches<<" inches "<<endl;
cout <<"Weight : "<<weight<<endl;
cout <<"BMI : "<<((double)(weight*703))/(inches*inches)<<endl;
}
int main()
{
cout << "Hello World" << endl;
person* p = new person;
p->input();
int q =0;
while(q==0){
char ch;
cout << " e: Enter a persons's statistics" << endl;
cout << "g: Grow by a number of inches" << endl;
cout << "s: Shrink by a number of inches" << endl;
cout << "i: Increase in weight" << endl;
cout << "d: Decrease in weight" << endl;
cout << "p: Print the current statistics" << endl;
cout << "q: Quit the program" << endl;
cout << "Enter your choice :";
cin >> ch;
int i;
switch(ch){
case 'e':
p->input();
break;
case 'g':
cout << "Enter number of inches:";
cin >>i;
p->grow(i);
break;
case 's':
cout << "Enter number of inches:";
cin >>i;
p->shrink(i);
break;
case 'i':
cout << "Enter number of weight:";
cin >>i;
p->gain(i);
break;
case 'd':
cout << "Enter number of weight:";
cin >>i;
p->lose(i);
break;
case 'p':
cout << " ========Persorn Detail======== ";
p->print();
break;
case 'q':
p->print();
q = 1;
break;
}
if(q==1)
break;
}
p->print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.