I have a base class "Employee" that has two child classes "Manager" and "Salesperson". In the Employee class I have a method called
addSalesperon( Salesperson * )
and a vector member variable
vector<Salesperson *> staff_list
where it stores employee objects.
addSalesperon( Salesperson * )
just pushes the salesperson into the vector. Each employee also has another vector
vector<Insurance *> sale_list;
that stores Class objects from a class I create called "Insurance".
I have a print method that loops through the Employee objects in
staff_list
vector and prints the the insurgence objects stored in
sales_list
vector at each index of staff_list. (Sorry I probably explained that poorly but if you look at the code you should be able to easily see what I was explaining)
The problem is when I try to call the print method nothing gets printed, but if I print the
staff_list
and
sale_list
vectors on their own they print fine. I'm just not sure whats wrong with my print method, and help clearing this up will be greatly appreciated!
Sample main:
list<Insurance *> sales_list; vector<Employee *> employee_list;
Employee *m = new Manager("Bob", "Smith", 62000);
Employee *s1 = new Manager("Rick", "Smith", 45000);
Employee *s2 = new Manager("Dick", "Smith", 25000);
Insurance *a = new Auto("John", "Smith", "ford", "mustang", 123456789, 600, 796);
Insurance *b = new Home("Larry", "Smith", 45, 670, 3220, 4120);
m->addSalesperson(s1);
m->addSalesperson(s2);
s1->addSale(a);
s2->addSale(b);
employee_list.push_back(m);
employee_list.push_back(s1);
employee_list.push_back(s2);
sales_list.push_back(a);
sales_list.push_back(b);
printStaffSales::operator<<(cout, *m);
Employee class:
class Employee{
protected:
string first_name;
string last_name;
string employee_type;
float base_salary;
float commission;
float total_salary;
vector<Employee *> staff_list;
public:
vector<Insurance *> sale_list; Employee():first_name(""), last_name(""), base_salary(0){ };
Employee(string fn, string ln, float bs):first_name(fn), last_name(ln), base_salary(bs){ };
void addSale( Insurance * );
void addSalesperson( Employee * );
virtual ostream &print_staff_sales( ostream & ) = 0;
};
namespace printStaffSales {
ostream &operator<<( ostream &, Employee & );
}
Employee.cpp
ostream &printStaffSales::operator<<( ostream &strm, Employee &e ){
cout<< "Sales by employees under manager: " << endl;
return e.print_staff_sales( strm );
}
Manager Class:
class Manager: public Employee{
private:
float staff_commission;
public:
Manager(): Employee(){ employee_type = "Manager"; };
Manager(string fn, string ln, float bs): Employee(fn, ln, bs){ employee_type = "Manager";
virtual ostream &print_staff_sales( ostream & );
};
Print method
ostream &Manager::print_staff_sales( ostream &strm ){
strm << "Manager: " << first_name << " " << last_name << endl;
Salesperson *s_temp;
for (int i = 0; i < staff_list.size(); ++i){
s_temp = staff_list.at(i);
Insurance *i_temp;
for (int i = 0; i < s_temp->sale_list.size(); ++i){
i_temp = sale_list.at(i);
strm<< *i_temp << endl;
}
}
strm<< endl;
return strm;
}