C++ examples for Class:Class Creation
Create class present health profile
#include <iostream> #include <string> /*from ww w .j a v a 2 s .co m*/ class Employee { private: std::string firstName; std::string lastName; std::string gender; int month, day, year; double height, weight; int ageInYears = 0; public: Employee(std::string, std::string, std::string, int, int, int, double,double); // SETTERS void setFirstName(std::string); void setLastName(std::string); void setGender(std::string); void setDOB(int, int, int); void setMonth(int); void setDay(int); void setYear(int); void setAge(); void setHeight(int); void setWeight(int); // GETTERS std::string getFirstName(); std::string getLastName(); std::string getGender(); int getMonth(); int getDay(); int getYear(); void getDOB(); int getAge(); double getHeight(); double getWeight(); double getBMI(); void getTargetHeartRate(); int getMaxHeartRate(); void showEmployee(); void printBMIInfo(); }; Employee::Employee(std::string fName, std::string lName,std::string sex, int m, int d, int y, double h,double w) { setFirstName(fName); setLastName(lName); setGender(sex); setDOB(m, d, y); setHeight(h); setWeight(w); setAge(); } // SETTERS void Employee::setFirstName(std::string fName) { firstName = (fName.length() > 0) ? fName : "FirstName"; } void Employee::setLastName(std::string lName) { lastName = (lName.length() > 0) ? lName : "LastName"; } void Employee::setGender(std::string sex) { gender = (sex == "Male" || sex == "Female") ? sex : "Undefined"; } void Employee::setDOB(int m, int d, int y) { setMonth(m); setDay(d); setYear(y); } void Employee::setMonth(int m) { month = (m > 0 && m <= 12) ? m : 1; } void Employee::setDay(int d) { day = (d > 0 && d <= 31) ? d : 1; } void Employee::setYear(int y) { year = y; } void Employee::setAge() { int cDay, cMonth, cYear; std::cout << "Enter the current date (mm dd yyyy): "; std::cin >> cMonth >> cDay >> cYear; if (cMonth < getMonth()) { ageInYears = (cYear - getYear()) - 1; } else if (cMonth > getMonth()) { ageInYears = cYear - getYear(); } else { if (cDay < getDay()) { ageInYears = (cYear - getYear()) - 1; } else { ageInYears = cYear - getYear(); } } } void Employee::setHeight(int h) { height = h; } void Employee::setWeight(int w) { weight = w; } // GETTERS std::string Employee::getFirstName() { return firstName; } std::string Employee::getLastName() { return lastName; } std::string Employee::getGender() { return gender; } int Employee::getMonth() { return month; } int Employee::getDay() { return day; } int Employee::getYear() { return year; } void Employee::getDOB() { std::cout << getMonth() << "/" << getDay() << "/" << getYear() << std::endl; } int Employee::getAge() { if (ageInYears == 0) setAge(); return ageInYears; } double Employee::getHeight() { return height; } double Employee::getWeight() { return weight; } double Employee::getBMI() { return ((getWeight() * 703) / (getHeight() * getHeight())); } // returns a persons target heart rate void Employee::getTargetHeartRate() { std::cout << "Your Target Heart Rate: "; std::cout << 0.5 * getMaxHeartRate() << " - " << 0.85 * getMaxHeartRate() << std::endl; } int Employee::getMaxHeartRate() { return 220 - getAge(); } void Employee::showEmployee() { std::cout << "HEALTH PROFILE FOR: " << getFirstName() << " " << getLastName() << std::endl; std::cout << "Gender: " << getGender() << std::endl; std::cout << "Age: " << getAge() << std::endl; std::cout << "BMI: " << getBMI() << std::endl; getTargetHeartRate(); std::cout << "Max Heart Rate: " << getMaxHeartRate() << std::endl; printBMIInfo(); } void Employee::printBMIInfo() { std::cout << "\nBMI VALUES" << std::endl; std::cout << "Underweight: less than 18.5" << std::endl; std::cout << "Normal: between 18.5 and 24.9" << std::endl; std::cout << "Overweight: between 25 and 29.9" << std::endl; std::cout << "Obese: 30 or greater\n" << std::endl; } #include <iostream> int main(int argc, const char *argv[]) { std::string fName, lName, gender; int month, day, year; double weight, height; std::cout << "Enter your first and last name: "; std::cin >> fName >> lName; std::cout << "Enter your gender (Male/Female): "; std::cin >> gender; std::cout << "Enter your date of birth (mm dd yyyy): "; std::cin >> month >> day >> year; std::cout << "Enter your weight in pounds: "; std::cin >> weight; std::cout << "Enter your height in inches: "; std::cin >> height; Employee hp(fName, lName, gender, month, day, year, height, weight); hp.showEmployee(); return 0; }