fill code by comments are given /*This program demonstrates the use of vectors w
ID: 3573796 • Letter: F
Question
fill code by comments are given
/*This program demonstrates the use of vectors with classes, defines operator functions, uses the pointer defererencing (->) operator.*/
//Name
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Employee
{
public:
/*Place the default constructor declaration below */
//
void setName(string &_name){name=_name;}
string getName() {return name;}
void setID(string _id) {id = _id;}
/*Fill in the code for getID */
string getID(){/* */}
void setSupervisorName(string _name)
{
/*Set supervisor's name to _name. Remember: this is a pointer so you have to use the arrow pointer-deference operator */
//
}
string getSupervisorName()
{
/*Return supervisor's name */
//
}
void setSupervisor(Employee *_supervisor)
{supervisor = _supervisor; }
/*Create an Employee Operator as a friend function prototype */
//
private:
string name;
string id;
/*Define a pointer to an Employee type called supervisor */
//
};
/*Define the default constructor definition */
//
{
name = "";
id = -1;
supervisor = nullptr;
}
/*Define a the operator << function allowing Employee to be streamed. Remember to return ostream& */
//
{
out <<"Name: " << _employee.getName() << endl;
out <<"ID: " << _employee.getID()<<endl;
out <<"Supervisor: " << _employee.getSupervisorName() << endl;
return out;
}
/*Note that the input Employee function was not defined as a friend function to the Employee class, so you must use Mutator and Accessor functions*/
void inputEmployee(Employee &_employee)
{
string employeeName, supervisorName, id;
Employee * supervisor = new Employee;
cout << "Employee Name: ";
getline (cin, employeeName);
/*Set the name for _employee to employeeName*/
//
cout <<"Employee ID: ";
getline (cin, id);
_employee.setID(id);
cout <<"Supervisor Name: ";
getline (cin, supervisorName);
_employee.setSupervisor(supervisor);
_employee.setSupervisorName(supervisorName);
}
/*Note: The ostream works for cout AND for ofstream objects*/
void printVector (ostream& out, vector<Employee> &employeeVector)
{
for (int i=0; i < employeeVector.size(); i++)
{
out << employeeVector[i] <<endl <<endl;
}
}
int main()
{
vector <int> intVector;
/* create a vector of Employees called employeeVector */
//
Employee employee1;
char menu='z';
ifstream infile("input.txt");
ofstream outfile("output.txt");
while (menu != 'e')
{
cout << "a. add employee"<<endl;
cout << "b. print all employees to screen"<<endl;
cout << "c. print all employees to file"<<endl;
cout << "e. exit" << endl;
cin >> menu;
switch (menu)
{
case 'a':
inputEmployee(employee1);
/*add employee1 to the employee Vector */
//
break;
case 'b':
/*call the function printvector outputing to the monitor */
//
break;
case 'c':
/*call the function printvector outputing to the outfile */
//
break;
case 'e':exit(0);
break;
default:break;
}
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Employee
{
public:
/*Place the default constructor declaration below */
//
Employee()
/*Define the default constructor definition */
//
{
name = "";
id = -1;
supervisor = NULL;
}
void setName(string &_name)
{
name=_name;
}
string getName()
{
return name;
}
void setID(string _id)
{
id = _id;
}
/*Fill in the code for getID */
string getID()
{
/* */
return id;
}
void setSupervisorName(string _name)
{
/*Set supervisor's name to _name. Remember: this is a pointer so you have to use the arrow pointer-deference operator */
name=_name;
//
}
string getSupervisorName()
{
/*Return supervisor's name */
//
return supervisor->getName();
}
void setSupervisor(Employee *_supervisor)
{
supervisor = _supervisor;
}
/*Create an Employee Operator as a friend function prototype */
//
friend ostream& operator << (ostream &out, Employee &_employee);
private:
string name;
string id;
/*Define a pointer to an Employee type called supervisor */
Employee *supervisor;
};
/*Define a the operator << function allowing Employee to be streamed. Remember to return ostream& */
//
ostream& operator<< (ostream &out, Employee &_employee) //operator function
{
out <<"Name: " << _employee.getName() << endl;
out <<"ID: " << _employee.getID()<<endl;
out <<"Supervisor: " << _employee.getSupervisorName() << endl;
return out;
}
/*Note that the input Employee function was not defined as a friend function to the Employee class, so you must use Mutator and Accessor functions*/
void inputEmployee(Employee &_employee)
{
string employeeName, supervisorName, id;
Employee * supervisor = new Employee;
cout << "Employee Name: ";
getline (cin, employeeName);
/*Set the name for _employee to employeeName*/
//
cout <<"Employee ID: ";
getline (cin, id);
_employee.setID(id);
cout <<"Supervisor Name: ";
getline (cin, supervisorName);
_employee.setSupervisor(supervisor);
_employee.setSupervisorName(supervisorName);
}
/*Note: The ostream works for cout AND for ofstream objects*/
void printVector (ostream& out, vector<Employee> &employeeVector)
{
for (int i=0; i < employeeVector.size(); i++)
{
out << employeeVector[i] <<endl <<endl;
}
}
int main()
{
vector <int> intVector;
/* create a vector of Employees called employeeVector */
//
vector <Employee> employeeVector;
Employee employee1;
char menu='z';
ifstream infile("input.txt");
ofstream outfile("output.txt");
while (menu != 'e')
{
cout << "a. add employee"<<endl;
cout << "b. print all employees to screen"<<endl;
cout << "c. print all employees to file"<<endl;
cout << "e. exit" << endl;
cin >> menu;
switch (menu)
{
case 'a':
inputEmployee(employee1);
/*add employee1 to the employee Vector */
//
employeeVector.push_back(employee1);
break;
case 'b':
/*call the function printvector outputing to the monitor */
//
break;
case 'c':
/*call the function printvector outputing to the outfile */
//
cout<<employee1;
break;
case 'e':
//exit(0);
break;
default:
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.