Write a C++ program using classes and objects, which can find the overall average of the entire group(say n) of patients. A single object with default constructor adds 10 patients with 60 kg average kg overweight or underweight. All objects of this patient class(and ONLY objects of this patient class) update the total weight and the total number(n) of patients and can display the overall average. Your C++ program should provide all these facilities.
#include <iostream>
#include <cstdlib> // for random. rand(), srand()
#include <ctime> // for random. time()
using namespace std;
class Patient{
private:
int amountPatients;
float totalWeight;
public:
Patient(){
amountPatients+=10;
for(int i = 0; i<10;i++)
totalWeight+= 60 + (rand()%2-1);
}
void displayAverageWeight(){
cout<<totalWeight/amountPatients<<endl;
}
};
int main()
{ // test
srand(time(0)); // for random overweight/underweight by 1(?) kg
Patient *a = new Patient();
a->displayAverageWeight();
Patient *b = new Patient();
b->displayAverageWeight();
return 0;
}
Comments
Leave a comment