This example is a Target Heart Rate Calculator. This is from the Deitel C++ how to program book. The instructor asked us to add a couple of things to the program and I will post a shot of the instructions he gave.
The first screenshot contains the modified instructions.
Above is a screen shot of the header file. Here is the code:
#include <string>
class HeartRate
{
public:
explicit HeartRate(std::string,std::string, int, int, int, int, int, int);
std::string getLastName()const;
std::string getFirstName() const;
int getBirthMonth() const;
int getBirthDay()const;
int getBirthYear()const;
int getCurMonth()const;
int getCurDay() const;
int getCurYear()const;
int getAge();
int getMaxHr();
float getTargetHeartRate50();
float getTargetHeartRate85();
private:
std::string firstName;
std::string lastName;
int bMonth;
int bDay;
int bYear;
int age;
int hr;
int curMonth;
int curYear;
int curDay;
int maxHr;
float targetHr50;
float targetHr85;
};
The next screen shot is the heartrate.cpp
Here is the full code for the heartrate cpp. You can't see it all in the picture.
#include <iostream>
#include "HeartRate.h"
using namespace std;
HeartRate::HeartRate(string fname,string lname, int bM, int bD, int bY, int cM, int cD, int cY)
:firstName(fname), lastName(lname), bMonth(bM), bDay(bD), bYear(bY), curMonth(cM), curDay(cD), curYear(cY)
{
}
string HeartRate::getFirstName() const
{
return firstName;
}
string HeartRate::getLastName() const
{
return lastName;
}
int HeartRate::getBirthMonth() const
{
return bMonth;
}
int HeartRate::getBirthDay() const
{
return bDay;
}
int HeartRate::getBirthYear() const
{
return bYear;
}
int HeartRate::getCurMonth() const
{
return curMonth;
}
int HeartRate::getCurDay() const
{
return curDay;
}
int HeartRate::getCurYear() const
{
return curYear;
}
int HeartRate::getAge()
{
int age = curYear-bYear;
if (curMonth < bMonth)
return age - 1;
if ( curMonth==bMonth )
if(curDay<bDay)
return age - 1;
return age;
}
int HeartRate::getMaxHr()
{
int age= curYear-bYear;
int maxHr = 220 - age;
return maxHr;
}
float HeartRate::getTargetHeartRate50()
{
int age = curYear-bYear;
int maxHr = 220- age;
float targetHr50 = maxHr *.50;
return targetHr50;
}
float HeartRate::getTargetHeartRate85()
{
int age = curYear-bYear;
int maxHr = 220- age;
float targetHr85 = maxHr *.85;
return targetHr85;
}
Next is the main cpp:
Here is the code:
#include <iostream>
#include "HeartRate.h"
using namespace std;
int main()
{
std::string firstName;
std::string lastName;
int bMonth;
int bDay;
int bYear;
int curMonth;
int curDay;
int curYear;
//int age; this is not needed
//int maxHr; this is not needed
cout << "Enter your first name " ;
cin >> firstName;
cout << "Enter your last name ";
cin >> lastName;
cout << "Enter your birth month as an integer ";
cin >> bMonth;
cout << "Enter your day of birth as an integer ";
cin >> bDay;
cout << "Enter your birth year as a four digit integer ";
cin >> bYear;
cout << "Please enter the current month " ;
cin >> curMonth;
cout << "Please enter the current day " ;
cin >> curDay;
cout << "Please enter the current year " ;
cin >> curYear;
cout << endl;
HeartRate myHR(firstName,lastName,bMonth,bDay,bYear,curMonth,curDay,curYear);
HeartRate anything(firstName,lastName,bMonth,bDay,bYear,curMonth,curDay,curYear);
cout << "Hello " << myHR.getFirstName() <<" " << myHR.getLastName() << "\n";
cout << "Birthdate " << myHR.getBirthMonth()<<"/"<<myHR.getBirthDay()<<"/"<<myHR.getBirthYear() <<"\n";
cout << endl;
cout << "Your age is " << anything.getAge(); cout <<endl;
cout << "Your maximum heart rate is " << anything.getMaxHr(); cout <<endl;
cout << "Your target heart rate should be between " << anything.getTargetHeartRate50() << " and " << anything.getTargetHeartRate85(); cout << endl;
cout << endl;
return 0;
}