Help with Classes and Structs

This is the instructions for the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Problem Statement:
A health care issue that has been used in the news lately is the computerization of health records.  This possibility is being approached cautiously because of sensitive privacy and security concerns among others.  Computerizing these records can make it easier for patients to share their health profiles and histories among their various health care professionals.  This could improve the quality of health care, help avoid drug conflicts and erroneous drug prescriptions, reduce costs and possibly save lives.  In this exercise, you’ll design a “starter” class to help with this. :

Task #1:
Define a object type called HealthProfile class for a person.   A HealthProfile object has the following members:
•	a struct containing a person’s first name, last name, gender, date of birth (another struct, with attributes month, day and year), height (in inches) and weight (in pounds)
•	an accessor function for each member variable (a function that returns that data)
•	a mutator function for each member (a function that receives data of that type passed in and uses it to set the member variable
•	a function that calculates and returns the user’s age in years.  Do this by asking for the current date and use the years to calculate the age.
•	a function that returns the maximum heart rate (in beats per minute), calculated by subtracting your age in years from 220
•	a function that returns the target heart rate (in beats per min), represented in a range that is 50 – 85% of the maximum heart rate
•	a function that calculates the body mass index (BMI), which is the person’s (weight in pounds times 703) divided by the height in inches squared

Place the class declaration in the .h file for the class.




Task #2:
Write the definition of each of the member functions above in the .cpp file for the class.  You may add any additional functions you wish, but be sure the adhere to encapsulation (put only those things in the class that belong there).  Make sure you include the .h file for the class as well.

Task #3:
Write an application that prompts the user for the person’s information, and uses that to create an object of this type.  The program should print the information from that object, including the personal data, and then calculate and print the person’s age in year, BMI, maximum heart rate and target heart rate.  

 


My Header File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class HealthProfile
{
struct birth
{
int month, day, year;
};
 
struct Patient
{
string Fname;
string Lname;
char gender;
birth b; //-| Struct previously defined data type
float inches, weight;
};

public:
//-| Accessor Functions 
string get_Fname();
string get_Lname();
char get_gender();
float get_height();
float get_weight();
birth get_bday();
birth get_bmonth();
birth get_byear();

//-| Calculation Functions
int person_age(); // Calculate Person's age;
int max_heartrate(int age); // Calculate Max Heart Rate
float body_mass(); // calculate BMI
int targetheartrate(int heart_rate);
//-| Mutator Functions

void set_Fname(string namef);
void set_Lname(string namel);
void set_gender(char a);
void set_height(float inches);
void set_weight(float weights);
void set_bday(int days);
void set_bmonth(int months);
void set_byear(int years);

};

#include "Fields_HealthProfile.cpp" 


Here's my implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

//-| Accessor Functions
 
string get_Fname()
{
return Patient.Fname;
}
  
string get_Lname()
{
return Patient.Lname;
}
 
char get_gender()
{
return Patient.gender;
}
 
float get_height()
{
return Patient.inches;
}
 
float get_weight()
{
return Patient.weight;
}
 
birth get_bday()
{
return Patient.b.day;
}
 
birth get_bmonth();
{
return Patient.b.month;
}
 
birth get_byear();
{
return Patient.b.year;
}
 
//-| Mutator Functions
 
void set_Fname(string namef)
{
Patient.Fname = namef;
}
void set_Lname(string namel)
{
Patient.Lname = namel;
}
void set_gender(char a)
{
Patient.gender = a;
}
void set_height(float inches)
{
Patient.inches = inches;
}
void set_weight(float weights
{
Patient.weight = weights;
}
void set_bday(int days)
{
Patient.b.day = days;
}
void set_bmonth(int months)
{
Patient.b.month = months;
}
void set_byear(int years)
{
Patient.b.year = years;
}
 
//-| Calculation Functions
 
int person_age() // Calculate Person's age;
{
int month;
int day;
int year;
 
cout << "What is the month (in form ex. 01) ?" << endl;
cin >> month;
 
cout << "What is the day (in form ex. 05) ?" << endl;
cin >> day;
 
cout << "What is the year (in form ex. 1992) ?" << endl;
cin >> year;
 
return Patient.b.year - year;
}
 
int maxheartrate(int age) // Calculate Max Heart Rate
{
return 220 - age;
}
 
int targetheartrate(int heart_rate) // Calculate Target Heart Rate 50 - 85% of max
{
int target;
target = heart_rate*.6;
 
return target;
}
 
float body_mass() // calculate BMI
{
 
float mass;
float height2;
mass = weight*730;
height2 = Patient.inches * Patient.inches;
mass = mass/height2;
 
return mass;
 
}


and my main program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <iomanip>
#include <string>
#include "Fields_HealthProfile.h"

using namespace std;



int main()
{

string a;
char b;
int c;
float e;

//-| Collecting Person's Name

cout << "Enter the person's First Name" << endl;
cin >> a;
set_Fname(a); 

cout << "Enter the person's Last Name" << endl;
cin >> a;
set_Lname(a);

//-| Collecting Male or Female
cout << "Enter the person's Gender" << endl;
cin >> b; 
set_gender(b);

//-| Collectiong Birth Information
cout << "What is the birth month (in form ex. 01) ?" << endl;
cin >> c;
set_bmonth(c);
 
cout << "What is the birth day (in form ex. 05) ?" << endl;
cin >> c;
set_bday(c);
 
cout << "What is the birth year (in form ex. 1992) ?" << endl;
cin >> c;
set_byear(c);

//-| Collecting Height and Weight 

cout << "What is the Person's Height in inches?" << endl;
cin >> e;
set_height(e);

cout << "What is the Person's Weight in pounds?" << endl;
cin >> e;
set_weight(e);

//-| Calculating and Printing: Age, BMI, Max Heart Rate, and Target Heart Rate.

int age;

age = person_age();

float bodymass;
bodymass = body_mass();

int mheart;
int theart;

mheart = max_heartrate(age);
theart = targetheartrate(mheart);

cout << "The Person's Age is: " << age << " their BMI is: " << bodymass << " their Maximum Heart Rate is: " << mheart << " and their Target Heart Rate is: " << theart << '.' << endl;

return 0;

}


these are some of the errors i get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
and here are the errors i get:

[code] 1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(10): error C2146: syntax error : missing ';' before identifier 'Fname'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(11): error C2146: syntax error : missing ';' before identifier 'Lname'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(19): error C2146: syntax error : missing ';' before identifier 'get_Fname'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(19): warning C4183: 'get_Fname': missing return type; assumed to be a member function returning 'int'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(20): error C2146: syntax error : missing ';' before identifier 'get_Lname'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(20): warning C4183: 'get_Lname': missing return type; assumed to be a member function returning 'int'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(35): error C2061: syntax error : identifier 'string'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.h(36): error C2061: syntax error : identifier 'string'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(3): error C2146: syntax error : missing ';' before identifier 'get_Fname'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(5): error C2065: 'Patient' : undeclared identifier
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(5): error C2228: left of '.Fname' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(5): warning C4508: 'get_Fname' : function should return a value; 'void' return type assumed
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(8): error C2146: syntax error : missing ';' before identifier 'get_Lname'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(8): error C2086: 'int string' : redefinition
1>          c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(3) : see declaration of 'string'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(10): error C2065: 'Patient' : undeclared identifier
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(10): error C2228: left of '.Lname' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(10): warning C4508: 'get_Lname' : function should return a value; 'void' return type assumed
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(15): error C2065: 'Patient' : undeclared identifier
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(15): error C2228: left of '.gender' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(20): error C2065: 'Patient' : undeclared identifier
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(20): error C2228: left of '.inches' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(25): error C2065: 'Patient' : undeclared identifier
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(25): error C2228: left of '.weight' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(28): error C2146: syntax error : missing ';' before identifier 'get_bday'
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(30): error C2065: 'Patient' : undeclared identifier
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(30): error C2228: left of '.b' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(30): error C2228: left of '.day' must have class/struct/union
1>c:\users\byron_000\desktop\programming\fields_healthprofile.cpp(30): warning C4508: 'get_bday' : function should return a value; 'void' return  
Hi byron,

First, I am always dismayed to see this in assignments:


• an accessor function for each member variable (a function that returns that data)
• a mutator function for each member


IMO that introduces a bad habit right from the start. Get functions are reasonably benign, but public set functions are almost always very bad. byron.setBankBalance(-1000000.00) // instant 1 million dollar mortgage It also creates misunderstanding about encapsulation

We had a huge discussion about this here:

www.cplusplus.com/forum/lounge/101305/7/


I wonder whether you could get some brownie points for making use of initialiser lists and constructors (google these), and update functions that set several or all the variables at once.

The purpose of constructors is to initialise data for an object.

Get functions can be avoided by having member functions that do what you want - for example print out all the data for a person, rather than call a function to retrieve each individual member variable.

Anyway, some observations about your problem.

Please indent your code - it makes it easier to understand. If using an IDE this should be automatic. In the IDE format indents to be 4 spaces say not tabs, as tabs are expanded to 8 spaces on this site, so things will display better here if you do that.

With formatting, it would have been nice if you had formatted the assignment part, so it wasn't 5 screens wide on my system!!

You need to #include header files for any thing you want to use. #include <string> to use strings for example. This will fix a lot of your errors.

Line 46 in your header file - Never include .cpp files, only include header files. Include the header file for the class in the implementation file, and in any other file that needs to use that type of object.

With filenames, name them the same as the class name.

In fields_healthprofile.cpp, your function definitions should have the scope resolution operator, as in:

4
5
6
7
string HealthProfile::get_Fname()
{
return Patient.Fname;
}


In your header file, the structs name a type not an object. You can put an object name after the closing brace.

struct birth
{
int month, day, year;
} DOB;

Then refer to items within the struct like this:

DOB.month = 6;



In main(), you don't create an object. This is required so you can call the object's functions defined in the class.

Now there is an idea called encapsulation. This was mentioned in item 2 of the assignment. Part of this means that everything to do with an object (it's data & functions ) should reside in that objects class. So most of the code you have in main() should be in member functions that is part of the HealthProfile class. So you could have one member function that gets all the user input validates it, then assigns it to member variables. At least that keeps it all tidy within the class. See below for more thoughts about this.

Because you app is an interactive one (user types info in) this is a good approach. If you were reading data from a file, then you would define & use constructors to get data into the object.

I just realised I could called out about what I just said. There could be a bit of debate as to which is better:

1. Have a function external to the class, which collects user input, validates it, then uses a constructor with arguments & initialiser list to create the object. This avoids creating an object with bad data. Because the function is external to the class, it is not really encapsulation is it? Would get messy in a bigger application.

2. Create a blank object with a default constructor. Have a member function that does the same as item 1 above, except that member initialisation is by assignment and it throws an exception if data is invalid.

Option 2 sounds better to me.

Any way hopefully all this helps.



Topic archived. No new replies allowed.