Friday, 19 July 2013

Calculate your Heart beat rate using C++ (OOP)

Here is a little program used for getting the heart beat rate

Declaration of Methods (Heart.h)

// Program for calculating the Maximum Heart
#include <iostream>
#include <string>

using namespace std;

//Class for Heart Rate
class HeartRate {

public:
HeartRate (string, string, int, int, int); // constructor for the class

void setFirstName ( string ); // function for name
string getFirstName (); // function to return first name

void setLastName ( string ); // function for last name
string getLastName (); // function to return last name

void setDateOfBirth ( int, int, int); // Function to set date of birth
int getDateOfBirth ();

int getAge (); // function to get age
int getMaximumHeartRate (); // function to get Maximum Heart Rate
int getTargetHeartRate (); // function to get Target Heart Rate

private:

string fName; // Constructor's first name
string lName; // Constructor's last  name
int month; // Constructor's month
int day; // Constructor's day
int year; // Constructor's year
int yearToday; // Present year
int birthYear; // Year of birth

}; // end of function

Definition of the methods (HeartRate.cpp)

//Program definition for Heart beat rate
#include <iostream>
#include <string>
#include "HeartRate.h"


//While solving this question, thoughts about how best to create the constructor with having to had parameters
using namespace std;

HeartRate::HeartRate ( string fNamel, string lNamel, int monthl, int dayl, int yearl){

setFirstName (fNamel);
setLastName ( lName );
setDateOfBirth ( monthl, dayl, yearl);

}

void HeartRate::setFirstName ( string fNamel) { // function for set first name
fName = fNamel;
}

void HeartRate::setLastName ( string lNamel ){ // function for set last name
lName = lNamel;
}

void HeartRate::setDateOfBirth ( int monthl, int dayl, int yearl) { // function for set date of birth
month = monthl;
day = dayl;
year = yearl;
}

string HeartRate::getFirstName (){ // function to return first name
return fName;
}

string HeartRate::getLastName(){ // function to return last name
return lName;
}

int HeartRate::getDateOfBirth () { // function for date of birth
return 0;

}

int HeartRate::getAge () { // function to calculate age
int monthB, dayB, yearB, yearT;
cout << "Enter your date of birth in format: month day year" << endl;
cin >> monthB  >> dayB  >> yearB;

cout << "Enter current year: " << endl;
cin >> yearT;
yearToday = yearT;
birthYear = yearB;
cout << endl;
cout << "Your date of birth is: "<< monthB << "/" << dayB << "/" << yearB << endl;
cout << endl;
cout << "Your age is: " << (yearToday - birthYear) << endl;
return 0;

}

int HeartRate::getMaximumHeartRate () { //function for the maximum heart rate
cout << "Your maximum heart rate is: " << 220- (yearToday - birthYear) << endl;
return 0;
}

int HeartRate::getTargetHeartRate () { // function for the target heart rater
cout << "Your target heart rate range is: " << (0.5 * (220-(yearToday - birthYear))) << " - " << (0.85 * (220-(yearToday - birthYear))) << endl;
return 0;
}


Main Method (ex03_316.cpp)

//Program for the main

#include <iostream>
#include <string>
#include "HeartRate.h"

using namespace std;

int main () {
string  firstName, lastName;
HeartRate myHeartRate ( "*", "*", day(**), month(**), year (****));

cout << " Enter your Name " << endl;
cin >> lastName >> firstName;
cout << "Your name is: " << lastName  << " "<< firstName << endl;


myHeartRate.getAge ();
myHeartRate.getMaximumHeartRate ();
myHeartRate.getTargetHeartRate ();

return 0;
}

The asterisks are required inputs from the user in the formats expected as defined in the Declaration.