Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Plain Text by Charlie ( 12 years ago )
/*
*CLD0023
*cld0023_hw5.cpp
*Compile with any C++ compiler or a makefile(g++).
*/
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cassert>
//#define UNIT_TESTING
using namespace std;
class Doctor {
string name;
int numPatients;
vector<string> patientList;
public:
Doctor();
Doctor(string nameIn);
~Doctor();
Doctor& operator=(Doctor &rhs;);
void getInput();
void showPatientList();
void resetPatientList();
void setDoctorName(string nameIn);
void setPatientList(vector<string> vec);
void setNumPatients(int patientNum);
void unitTest();
string getDoctorName();
vector<string> getPatientList();
int getPatientAmount();
};
Doctor::Doctor() {
}
Doctor::Doctor(string nameIn) {
setDoctorName(nameIn);
}
//destructor resets all global memory
Doctor::~Doctor() {
patientList.clear();
numPatients = 0;
name = "";
}
Doctor& Doctor::operator=(Doctor &temp;) {
if(this == &temp;) {
return *this;
}
string tempStr = temp.getDoctorName();
vector<string> tempVec = temp.getPatientList();
int patientAmount = temp.getPatientAmount();
resetPatientList();
new Doctor();
setDoctorName(tempStr);
setNumPatients(patientAmount);
setPatientList(tempVec);
return *this;
}
void Doctor::setPatientList(vector<string> vec) {
patientList = vec;
}
void Doctor::setDoctorName(string nameIn) {
name = nameIn;
}
void Doctor::setNumPatients(int num){
numPatients = num;
}
int Doctor::getPatientAmount() {
return numPatients;
}
string Doctor::getDoctorName() {
return name;
}
void Doctor::getInput() {
string input = "";
cout << "\nWhat is the name of the Doctor? " << endl;
getline(cin, input);
setDoctorName(input);
input = "";
cout << "\nHow many patients does he have? " << endl;
getline(cin, input);
stringstream(input) >> numPatients;
input = "";
//input user into patientList
for(int i = 0; i < numPatients; i++) {
cout << "\nInput patient number: " << i+1 << endl;
getline(cin, input);
patientList.push_back(input);
input = "";
}
}
void Doctor::showPatientList() {
cout << "\n";
cout << name << "'s list of patients is: ";
for(int i = 0; i < patientList.size(); i++) {
cout << "\nNumber: " << i+1 << " " << patientList[i];
}
}
vector<string> Doctor::getPatientList() {
return patientList;
}
//resets number of patients to zero and patientList to an empty list.
void Doctor::resetPatientList() {
numPatients = 0;
patientList.clear();
}
void Doctor::unitTest() {
cout << "*** This is a debugging version ***";
cout << "\nUnit Test Case set 1 test getter/setter methods... "<< endl;
Doctor testDoc;
vector<string> testVec;
testVec.push_back("Bob");
testVec.push_back("Susan");
testVec.push_back("Joe");
testVec.push_back("Lee");
testVec.push_back("Charles");
testDoc.setDoctorName("Bob Dole");
testDoc.setNumPatients(5);
testDoc.setPatientList(testVec);
assert(testDoc.getDoctorName() == "Bob Dole");
cout << "Case 1.1 passed for getDoctorName() and setDoctorName()..." << endl;
assert(testDoc.getPatientAmount() == 5);
cout << "Case 1.2 passed for getNumPatients() and setNumPatients()..." << endl;
assert(testDoc.getPatientList() == testVec);
cout << "Case 1.3 passed for getPatientList() and setPatientList()..." << endl;
cout << "Unit test 2 test assignment operator for Doctor... " << endl;
//test overloaded assignement operator..
Doctor newDoc = testDoc;
assert(newDoc.getDoctorName() == "Bob Dole");
cout << "Case 2.1 passed for name assignment.." << endl;
assert(newDoc.getPatientAmount() == 5);
cout << "Case 2.2 passed for patient amount assignment.." << endl;
assert(newDoc.getPatientList() == testVec);
cout << "Case 2.3 passed for Patient list assignement.." << endl;
cout << "Unit test Case 3 test resetPatientList()... " << endl;
newDoc.resetPatientList();
assert(newDoc.getPatientAmount() == 0);
cout << "Case 3.1 passed for patient amount.." << endl;
assert(newDoc.getPatientList() != testVec);
cout << "Case 3.2 passed for patientList reset" << endl;
}
int main()
{
Doctor doc;
#ifdef UNIT_TESTING
//test all functions except user inputed functions
doc.unitTest();
#else
//get input from user and display input
doc.getInput();
doc.showPatientList();
#endif
//call destructor
return 0;
}
Revise this Paste