In: Computer Science
Program Specifications:
Write a program that defines a class HumanBMI, implements it as
required, and tests the class implementation. The class definition
and implementation should be separated into HumanBMI.h and
HumanBMI.cpp files.
A. The class HumanBMI consists of three private member variables:
name of type string, height of type int in inches, and weight of
type int in pounds. The class HumanBMI also includes the following
public member functions:
1) setName to set the name member variable with a string
argument.
2) setHeight to set the height member variable with an int argument. The value of height should be a positive number, otherwise use 1 as a valid value.
3) setWeight to set the weight member variable with an int argument. The value of weight should be a positive number, otherwise use 1 as a valid value.
4) getBMI to calculate and return the value of BMI (Body Mass Index), in floating-point value, based on the following formula:
BMI = 703 * weight /(height * height)
5) print to output the name, height, weight, and BMI of a person. If a BMI is lower than 18.5, print out "Underweight", otherwise, print out "Not underweight".
6) equals to compare two Human objects’ name, height, and weight values, respectively, and return true if they are the same, otherwise return false.
7) An overloaded constructor with default parameters to initialize name, height and weight. The default values for name, height, and weight are "None", 1, and 1, respectively.
8) A copy constructor to create an object and initialize it with
an existing HumanBMI object.
B. In the client program Main.cpp, you should declare class objects
and test each constructor and member function.
--------------------------------------------------------------------------------------------------------------------------------------------------------------
>> HINT (Submit 3 Files): HumanBMI.h, HumanBMI.cpp, and Main.cpp
***DO NOT USE! Advanced Codinng Methods***
**Must Include Comments for FULL CREDIT**
Program Code [C++]
HumanBMI.h
// HumanBMI class
#include <iostream>
using namespace std;
class HumanBMI {
private:
// Required member variables
string name;
int height;
int weight;
public:
// All required member
functions
void setName(string);
void setHeight(int);
void setWeight(int);
float getBMI();
void print();
bool equals(const
HumanBMI&);
// Overloaded & copy constructor
HumanBMI(string, int,
int);
HumanBMI(const
HumanBMI&);
};
HumanBMI.cpp
#include "HumanBMI.h"
// Overloaded constructor
HumanBMI::HumanBMI(string n, int h, int w):name{"None"}, height{1},
weight{1} {
setName(n);
setHeight(h);
setWeight(w);
}
// Copy constructor
HumanBMI::HumanBMI(const HumanBMI& other) {
this->name = other.name;
this->height = other.height;
this->weight = other.weight;
}
// Other methods
void HumanBMI::setName(string n) {
name = n;
}
void HumanBMI::setHeight(int h) {
if(h > 0) // Check height must be positive
height = h;
else
height = 1;
}
void HumanBMI::setWeight(int w) {
if(w > 0)
weight = w;
else
weight = 1;
}
float HumanBMI::getBMI() {
float BMI = 703 * ((float)weight / (height *
height));
return BMI;
}
void HumanBMI::print() {
// Printing each member info
cout << "Name: " << name <<
endl
<< "Height: " << height
<< endl
<< "Weight: " << weight
<< endl;
float BMI = getBMI();
// Calculating condition for BMI and printing it
if(BMI < 18.5)
cout << "Underweight";
else
cout << "Not
underweight";
cout << endl;
}
bool HumanBMI::equals(const HumanBMI& other) {
// Comparing all data members
return ((name == other.name) && (height ==
other.height) && (weight == other.weight));
}
Main.cpp
#include <iostream>
#include "HumanBMI.h"
using namespace std;
int main() {
// Creating HumanBMI object and testing all
methods
HumanBMI h1("John", 60, 100);
HumanBMI h2(h1);
h2.setName("Kate");
h2.setHeight(50);
h2.setWeight(120);
h1.print();
cout << endl;
h2.print();
// Comparing two HumanBMI objects
cout << endl;
if(h1.equals(h2))
cout << "h1 and h2 objects
are equal" << endl;
else
cout << "h1 and h2 objects
are not equal" << endl;
return 0;
}
Sample Output:-
----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!