Create a class named Doctor that has three member variables: name - A string tha
ID: 3675380 • Letter: C
Question
Create a class named Doctor that has three member variables: name - A string that stores the name of the doctor num Patients - An integer that tracks how many patients the doctor must trea patient List - A dynamic array of strings used to store the patient names Write appropriate constructor(s), mutator, and accessor methods for the class along with the following: A method that inputs all values from the user, including the list of patient names. Note that this method has to support input for an arbitrary number of patient. A method that outputs the name and list of all patients.A method that resets the number of patient to 0 and the patient List to an empty list. An overloaded assignment operator that correctly makes a new copy of the list of patients.A destructor that releases all memory that has been allocated. Write a main method that tests (i.e., unit testing) all of your functions.Explanation / Answer
Doctor.cpp
#include "Doctor.h"
Doctor::Doctor()
{
patientList = new string[1];
numPatients = 0;
}
Doctor::Doctor(const Doctor &docIn)
{
setName(docIn.name);
setNumPatients(docIn.numPatients);
setPatientList(docIn.patientList);
}
Doctor& Doctor::operator= (const Doctor& docIn)
{
setName(docIn.name);
setNumPatients(docIn.numPatients);
setPatientList(docIn.patientList);
return *this;
}
Doctor::~Doctor()
{
delete &name;
delete &numPatients;
delete [] patientList;
patientList = NULL;
}
void Doctor::getInfo()
{
vector<string> patients;
vector<string>::iterator itr;
string in = "";
int i;
cout << " Enter Doctor name: ";
getline(cin, name);
cout << " Enter Patient Names, one at a time (enter '$' when finished): ";
getline(cin, in);
while (in.compare("$") != 0)
{
patients.push_back(in);
getline(cin, in);
}
numPatients = patients.size();
delete [] patientList;
patientList = new string[numPatients];
i = 0;
for (itr = patients.begin(); itr < patients.end(); itr++)
{
patientList[i] = *itr;
i++;
}
}
void Doctor::displayInfo()
{
cout << "Patients:";
for (int i = 0; i < numPatients; i++)
{
cout << endl << patientList[i];
}
cout << endl;
}
void Doctor::reset()
{
if (numPatients > 0)
{
numPatients = 0;
delete [] patientList;
patientList = NULL;
}
}
string Doctor::getName()
{
return name;
}
void Doctor::setName(string nameIn)
{
name = nameIn;
}
int Doctor::getNumPatients()
{
return numPatients;
}
void Doctor::setNumPatients(int numIn)
{
numPatients = numIn;
}
string* Doctor::getPatientList()
{
return patientList;
}
void Doctor::setPatientList(string* listIn)
{
patientList = listIn;
}
Doctor.h
#ifndef DOCTOR_H
#define DOCTOR_H
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Doctor
{
public:
Doctor();
virtual ~Doctor();
Doctor(const Doctor&);
string getName();
void setName(string);
int getNumPatients();
void setNumPatients(int);
string* getPatientList();
void setPatientList(string*);
void getInfo();
void displayInfo();
void reset();
Doctor& operator= (const Doctor&);
protected:
string name;
int numPatients;
string *patientList;
};
#endif // DOCTOR_H
main.cpp
#include "Doctor.h"
#include <iostream>
#include <string>
#include <stdlib.h>
//#define UNIT_TESTING
using namespace std;
int main()
{
#ifdef UNIT_TESTING
cout << "Unit testing!" << endl;
Doctor doc;
doc.setName("Doctor Bob");
cout << "Doctor's name (Doctor Bob): " << doc.getName() << endl;
string *patients = new string[2];
patients[0] = "Jim";
patients[1] = "Jane";
doc.setPatientList(patients);
doc.setNumPatients(2);
cout << "Patient Number (2): " << doc.getNumPatients() << endl;
cout << "Patients (Jim, Jane): ";
doc.displayInfo();
doc.reset();
cout << "RESET Patient Number (0): " << doc.getNumPatients() << endl;
cout << "Patients (): ";
doc.displayInfo();
#else
Doctor doc;
doc.getInfo();
doc.displayInfo();
#endif
exit(EXIT_SUCCESS);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.