RE: [C++] BMR Calculator 12-10-2013, 02:10 AM
#9
Here's a way you can encapsulate it's functionality:
Disregarding the user input parsing, there's an example of its usage as well. If you wanted it to say "BMR: {value} or something like that, obviously you'd overload the << operator.
Code:
#include <iostream>
#include <cstdint>
enum GENDER
{
MALE,
FEMALE
};
struct BMR
{
public:
BMR(GENDER gender) : sex(gender), years(0), weight_kg(0), height_cm(0), last_bmr(0) { }
BMR(GENDER gender, float weight, float height, uint32_t age) : sex(gender)
{
weight_kg = weight;
height_cm = height;
years = age;
last_bmr = 10 * weight + 6.25 * height - 5 * age
+ (gender == MALE ? 5 : -161);
}
uint32_t operator() () { return getBmr(); }
uint32_t last_bmr;
uint32_t years;
float weight_kg;
float height_cm;
GENDER sex;
private:
uint32_t getBmr()
{
return (last_bmr = 10 * weight_kg + 6.25 * height_cm - 5 * years
+ (sex == MALE ? 5 : -161));
}
};
int main()
{
BMR bmr(MALE, 100, 180, 27);
std::cout << bmr() << std::endl;
bmr.weight_kg = 80;
std::cout << bmr() << std::endl;
}Disregarding the user input parsing, there's an example of its usage as well. If you wanted it to say "BMR: {value} or something like that, obviously you'd overload the << operator.
-- cxS
[ Haskell/.NET/C/C++ - Software Engineer ]
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)