Create class to represent heart rate - C++ Class

C++ examples for Class:Class Creation

Description

Create class to represent heart rate

Demo Code

                  
#include <iostream>
#include <string>
                  //w  ww. j a va  2  s  .  c om
class Employee {
 private:
    std::string firstName;
    std::string lastName;
    int month = 1;
    int day = 1;
    int year = 1900;
    int ageInYears = 0;
                  
 public:
    Employee(std::string, std::string, int, int, int);
                  
    // SETTERS
    void setFirstName(std::string);
    void setLastName(std::string);
    void setDOB(int, int, int);
    void setMonth(int);
    void setDay(int);
    void setYear(int);
    void setAge();
                  
    // GETTERS
    std::string getFirstName();
    std::string getLastName();
    int getMonth();
    int getDay();
    int getYear();
    int getAge();
    int getMaximumHeartRate();
    void getTargetHeartRate();
                  
    void displayInformation();
};
                  
                  
Employee::Employee(std::string fName, std::string lName, int m, int d, int y) {
    setFirstName(fName);
    setLastName(lName);
    setDOB(m, d, y);
}
                  
// SETTERS
void Employee::setFirstName(std::string fName) {
    firstName = (fName.length() > 0) ? fName : "FirstName";
}
void Employee::setLastName(std::string lName) {
    lastName = (lName.length() > 0) ? lName : "Surname";
}
void Employee::setDOB(int m, int d, int y) {
    setMonth(m);
    setDay(d);
    setYear(y);
    setAge();
}
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() { ageInYears = getAge(); }
// GETTERS
std::string Employee::getFirstName() { return firstName; }
std::string Employee::getLastName() { return lastName; }
int Employee::getMonth() { return month; }
int Employee::getDay() { return day; }
int Employee::getYear() { return year; }
int Employee::getAge() {
    if (ageInYears > 0) return ageInYears;
                  
    int cDay, cMonth, cYear;
                  
    std::cout << "Enter the current date(mm dd yyyy): ";
    std::cin >> cMonth >> cDay >> cYear;
                  
    if (cMonth < getMonth()) {
        return (cYear - getYear()) - 1;
    } else if (cMonth > getMonth()) {
        return cYear - getYear();
    } else {
        if (cDay < getDay()) {
            return (cYear - getYear()) - 1;
        } else {
            return cYear - getYear();
        }
    }
}
                  
int Employee::getMaximumHeartRate() { return 220 - getAge(); }
                  
void Employee::getTargetHeartRate() {
    std::cout << "Your Rate: ";
    std::cout << 0.5 * getMaximumHeartRate() << " - " << 0.85 * getMaximumHeartRate() << std::endl;
}
                  
void Employee::displayInformation() {
    std::cout << "\nName: " << getFirstName() << " " << getLastName() << std::endl;
    std::cout << "D.O.B: " << getMonth() << "/" << getDay() << "/" << getYear() << std::endl;
    std::cout << "Maximum Heart Rate: " << getMaximumHeartRate() << std::endl;
    getTargetHeartRate();
}
                  
int main(int argc, const char *argv[]) {
    Employee emp1("B", "Mary", 11, 23, 1956);
    Employee emp2("S", "Edith", 5, 13, 2020);
                  
    emp1.displayInformation();
    emp2.displayInformation();
    return 0;
}

Result


Related Tutorials