here is the given code: #include <iostream> #include <string> #include <vector>
ID: 3699434 • Letter: H
Question
here is the given code:
#include <iostream> #include <string> #include <vector> using namespace std; /// Notice the personType base class has a pure virtual function. class personType { public: personType(string, string); string getFirstName() const; string getLastName() const; virtual void printName() const = 0; void setName(string, string); private: string firstName; string lastName; }; /// The base class's pure virtual function must /// be implemented in derived classes. class doctorType : public personType { public: doctorType(string, string, string); string getSpecialty() const; void printName() const; void setSpecialty(string); private: string specialty; }; /// The base class's pure virtual function must /// be implemented in derived classes. class lawyerType : public personType { public: lawyerType(string, string, int); int getLicense() const; void printName() const; void setLicense(int); private: int license; }; personType::personType(string first, string last) { setName(first, last); } string personType::getFirstName() const { return firstName; } string personType::getLastName() const { return lastName; } void personType::printName() const { cout << firstName << " " << lastName << endl; } void personType::setName(string first, string last) { firstName = first; lastName = last; } doctorType::doctorType(string first, string last, string s) :personType(first, last) { setSpecialty(s); } string doctorType::getSpecialty() const { return specialty; } void doctorType::printName() const { personType::printName(); cout << specialty << endl << endl; } void doctorType::setSpecialty(string s) { specialty = s; } lawyerType::lawyerType(string first, string last, int lic) :personType(first, last) { setLicense(lic); } int lawyerType::getLicense() const { return license; } void lawyerType::printName() const { personType::printName(); cout << license << endl << endl; } void lawyerType::setLicense(int lic) { license = lic; } // TODO: Implement the addDoctor function. This function // gets input from the user, dynamically creates a new // doctorType object and pushes it onto the back of the // vector of personType pointers. void addDoctor(vector<personType*> &vec) { // Implement your function here. } // TODO: Implement the addLawyer function. This function // gets input from the user, dynamically creates a new // lawyerType object and pushes it onto the back of the // vector of personType pointers. void addLawyer(vector<personType*> &vec) { // Implement your function here. } // The printVec function is provided for you. // This function uses an iterator to call the print function of each object // that is pointed to. Think of this as a pointer to a pointer. The iterator // points to a specific position in the vector. And that position in the // vector contains a pointer that points to either a doctorType or lawyerType object. void printVec(vector<personType*> &vec, vector<personType*>::iterator &it) { for (it = vec.begin(); it != vec.end(); it++) (*it)->printName(); cout << endl; } // TODO: Implement the clearVec function. This function uses an iterator // to step through every position of a vector and deletes the object // pointed to by personType pointer at each position. Afterwards, clear() // can be called to delete the positions themselves. void clearVec(vector<personType*> &vec, vector<personType*>::iterator &it) { // Implement your function here. } int main() { vector<personType*> vec; vector<personType*>::iterator it; addDoctor(vec); addLawyer(vec); printVec(vec, it); clearVec(vec, it); return 0; }
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/// Notice the personType base class has a pure virtual function.
class personType {
public:
personType(string, string);
string getFirstName() const;
string getLastName() const;
virtual void printName() const = 0;
void setName(string, string);
private:
string firstName;
string lastName;
};
/// The base class's pure virtual function must
/// be implemented in derived classes.
class doctorType : public personType {
public:
doctorType(string, string, string);
string getSpecialty() const;
void printName() const;
void setSpecialty(string);
private:
string specialty;
};
/// The base class's pure virtual function must
/// be implemented in derived classes.
class lawyerType : public personType {
public:
lawyerType(string, string, int);
int getLicense() const;
void printName() const;
void setLicense(int);
private:
int license;
};
personType::personType(string first, string last) {
setName(first, last);
}
string personType::getFirstName() const {
return firstName;
}
string personType::getLastName() const {
return lastName;
}
void personType::printName() const {
cout << firstName << " " << lastName << endl;
}
void personType::setName(string first, string last) {
firstName = first; lastName = last;
}
doctorType::doctorType(string first, string last, string s) :personType(first, last) {
setSpecialty(s);
}
string doctorType::getSpecialty() const {
return specialty;
}
void doctorType::printName() const {
personType::printName();
cout << specialty << endl << endl;
}
void doctorType::setSpecialty(string s) {
specialty = s;
}
lawyerType::lawyerType(string first, string last, int lic) :personType(first, last) {
setLicense(lic);
}
int lawyerType::getLicense() const {
return license;
}
void lawyerType::printName() const {
personType::printName();
cout << license << endl << endl;
}
void lawyerType::setLicense(int lic) {
license = lic;
}
// TODO: Implement the addDoctor function. This function
// gets input from the user, dynamically creates a new
// doctorType object and pushes it onto the back of the
// vector of personType pointers.
void addDoctor(vector<personType*> &vec) {
// Implement your function here.
doctorType doctor("RAM", "LAL", "Neuro");
doctorType *obj = &doctor;
vec.push_back(obj);
}
// TODO: Implement the addLawyer function. This function
// gets input from the user, dynamically creates a new
// lawyerType object and pushes it onto the back of the
// vector of personType pointers.
void addLawyer(vector<personType*> &vec) {
// Implement your function here.
lawyerType lawyer("VENU", "SINGH", 5462);
lawyerType *obj = &lawyer;
vec.push_back(obj);
}
// The printVec function is provided for you.
// This function uses an iterator to call the print function of each object
// that is pointed to. Think of this as a pointer to a pointer. The iterator
// points to a specific position in the vector. And that position in the
// vector contains a pointer that points to either a doctorType or lawyerType object.
void printVec(vector<personType*> &vec, vector<personType*>::iterator &it) {
for (it = vec.begin(); it != vec.end(); it++) {
(*it)->printName();
cout << endl;
}
}
// TODO: Implement the clearVec function. This function uses an iterator
// to step through every position of a vector and deletes the object
// pointed to by personType pointer at each position. Afterwards, clear()
// can be called to delete the positions themselves.
void clearVec(vector<personType*> &vec, vector<personType*>::iterator &it) {
// Implement your function here.
for (it = vec.begin(); it != vec.end(); it++) {
it = vec.erase(it);
}
vec.clear();
}
int main() {
vector<personType*> vec;
vector<personType*>::iterator it;
addDoctor(vec);
addLawyer(vec);
printVec(vec, it);
clearVec(vec, it);
return 0;
}
3. std::vector::push_back saves the copy of an object, but by using the vector of pointers the copying is done for pointers and which in turn increases the performance of the code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.