C++ PROGRAM HELP! Please Help! I need help showing some sort of inheritence from
ID: 3577025 • Letter: C
Question
C++ PROGRAM HELP! Please Help!
I need help showing some sort of inheritence from base class Person to derived class Patient. I also need to know how to actually have the prgram create "Patient.txt" file if one is not found. Lastly, I need to know how to actually implement my main function members:
int carbInsulin(int);
int correctionDose(int, const int);
int totalInsulinDose(int, int)
into my main function. PLEASE HELP! CODE BELOW:
*********************************************
Person.h
*********************************************
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Person //Person class that takes date, name and age of person
{
protected:
string date;
string name;
int age;
public:
Person();
Person(string, string, int);
string getDate();
string getName();
int getAge();
};
*************************************
PersonImp.cpp
*************************************
#include "Person.h"
Person::Person()
{
date = "UNKNOWN";
name = "UNKNOWN";
age = 0;
}
Person::Person(string x, string y, int z)
{
date = x;
name = y;
age = z;
}
int Person::getAge()
{
return age;
}
string Person::getDate()
{
return date;
}
string Person::getName()
{
return name;
}
******************************
Patient.h
*******************************
#pragma once
#include "Person.h"
struct Record //stucture for node in a linked list
{
string date;
string name;
int age;
string food; // Food being consumed by patient
int carbs; // total carbs in food
int sugarLevel; //current blood sugar level
Record *next;
}
*head;
class Patient : public Person
{
public:
int count = 0; //used to determine how many entries in file
Record* create_node(string, string, int, string, int, int);
void insert();
void display();
Patient()
{
head = NULL;
}
};
********************************
PatientImp.cpp
********************************
#include "Patient.h"
#include "Person.h"
#include <fstream>
Record* Patient::create_node(string d, string e, int f, string a, int b, int c)
{
struct Record *newRecord, *s;
newRecord = new Record;
if (newRecord == NULL)
{
cout << "Sorry, memory not allocated!" << endl;
return 0;
}
else
{
newRecord->date = d;
newRecord->name = e;
newRecord->age = f;
newRecord->food = a;
newRecord->carbs = b;
newRecord->sugarLevel = c;
newRecord->next = NULL;
return newRecord;
}
}
void Patient::insert() //insert node into a file named Patient.txt
{
string s_date;
string s_name;
int s_age;
string s_food;
int s_carbs;
int s_sugarLevel;
string stuff[6] = { "Date: ", "Name: ", "Age: ", "Food: ", "Carbs: ", "SugarLevel: " };
Record *Record_ptr, *newRecord;
ofstream outfile;
outfile.open("Patient.txt");
cout << " Enter patient details.." << endl;
for (int i = 0; i < 6; ++i) //for loop and switch method using array to enter user input into variables
{
cout << " " << stuff[i];
switch (i)
{
case 0:
getline(cin, s_date);
break;
case 1:
getline(cin, s_name);
break;
case 2:
cin >> s_age;
break;
case 3:
getline(cin, s_food);
break;
case 4:
cin >> s_carbs;
break;
case 5:
cin >> s_sugarLevel;
break;
default:
cout << "ERROR! ";
}
}
outfile << s_date << " Name: " << s_name << " Age: " << s_age << " Food: " << s_food << " Carbs Consumed: " << s_carbs << " Sugar Levels: " << s_sugarLevel;
outfile << " ";
count++;
newRecord = create_node(s_date, s_name, s_age, s_food, s_carbs, s_sugarLevel); //calls create new node
if (head == NULL) //if linked list is empt
{
head = newRecord;
head->next = NULL; //put newRecord in first node and set next pointer to NULL
}
else
{
Record_ptr = head;
while (Record_ptr->next)
Record_ptr = Record_ptr->next;
Record_ptr->next = newRecord; //Putting record in end of linked list
}
outfile.close();
}
void Patient::display()
{
Record *Display_ptr;
Display_ptr = head; //initializing pointer to head
while (Display_ptr != NULL) //while pointer is not empty print data
{
cout << Display_ptr->date << endl;
cout << Display_ptr->name << endl;
cout << Display_ptr->age << endl;
cout << Display_ptr->food << endl;
cout << Display_ptr->carbs << endl;
cout << Display_ptr->sugarLevel << endl;
Display_ptr = Display_ptr->next;
cout << endl;
}
cout << " Number of entries in file:" << count << " ";
}
***************************************
InsulinCalcDemo
**************************************
/*
Purpose:To allow user to add variables Name, Food Eaten, Carbs(in grams), and Blood Sugar
to a linked list.
The program will calculate how much "mealtime" insulin must be administered to a diabetic
person about to eat with a target blood sugar of 120mg/dl(milligrams per deciliter).
The insulin will be the total calculated for carbohydrate coverage at meal and a
high blood sugar correction dose.
Carbohydrate Coverage insulin dose algorithym:(insulin to CHO ratio is 1:10)
(carbs in grams)/10
EXAMPLE: If eating 60 grams of carbs, 6 units of rapid
acting insulin to cover the carbohydrate.
High Blood Sugar correction insulin dose algorithym:(1 unit of insulin drops blood sugar 50 md/dl)
(actual blood sugar - target blood sugar)/50
EXAMPLE: if actual blood sugar is 220:
(220 - 120) / 50 -> 100/50 -> 2 units of rapid acting insulin
Add CHO insulin dose and high blood sugar correction dose to get complete mealtime dose.
**MEALTIME INSULIN ONLY STAYS IN SYSTEM FOR 3 TO 6 HOURS**
The program then copies the linked list into a file for later viewing.
*/
#include <iostream>
#include <string>
#include "Person.h"
#include "Patient.h"
#include <fstream>
using namespace std;
const int targetBloodSugar = 120; //For particular patient 120 mg is always target blood sugar levels
int carbInsulin(int);
int correctionDose(int, const int);
int totalInsulinDose(int, int);
int main()
{
int waitForIt;
head = NULL;
Patient p1;
p1.insert();
p1.insert();
p1.display();
}
int carbInsulin(int c) //carbs are to be passed
{
int cI = (c / 10);
return cI;
}
int correctionDose(int a, const int b) //current blood sugar followed by target blood sugar is to entered
{
int corDos = (a - b) / 50;
return corDos;
}
int totalInsulinDose(int x, int y) //carbInsulin result, correctionDose result
{
int totalDose = (x + y);
return totalDose;
}
Explanation / Answer
//first how inheritance working in this code
class Person //Person class that takes date, name and age of person
{
protected:
string date;
string name;
int age;
public:
Person();
Person(string, string, int);
string getDate();
string getName();
int getAge();
};
//here Patient is inherited from Person
//it have also data member such as : date ,name ,age
//member function getDate , getName() , getAge
//so we can access these function from instance of a Patient
//such as Patient P1; then old=P1.getAge();//as its visibility is public
class Patient : public Person
{
public:
int count = 0; //used to determine how many entries in file
Record* create_node(string, string, int, string, int, int);
void insert();
void display();
Patient()
{
head = NULL;
}
};
//if file Patient.txt does not exist , open it in write mode
//Now how to use
/*
* int carbInsulin(int);
* int correctionDose(int, const int);
* int totalInsulinDose(int, int)
* into my main function.
*/
//here we have instance of Patient P1, we can use P1 to pass the argument in above function to calculate
//
int main()
{
int waitForIt;
head = NULL;
Patient p1;
p1.insert();
p1.insert();
p1.display();
//head instance of record
cout<<"Carb CarbInsulin : "<<carbInsulin(head.carbs)<<endl;
//120 is target
cout<<"CorrectionDose : "<<correctionDose(head.sugarLevel,120)<<endl;
cout<<"Total Insulin Dose : "<<totalInsulinDose(carbInsulin(head.carbs), correctionDose(head.sugarLevel,120))<<endl;
}
int carbInsulin(int c) //carbs are to be passed
{
int cI = (c / 10);
return cI;
}
int correctionDose(int a, const int b) //current blood sugar followed by target blood sugar is to entered
{
int corDos = (a - b) / 50;
return corDos;
}
int totalInsulinDose(int x, int y) //carbInsulin result, correctionDose result
{
int totalDose = (x + y);
return totalDose;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.