Add the following to the programs:
1. Add a bonus attribute (in private data fields for manager
class
2. Add a set_bonus and set_bonus function to manager class
3. Show bonus in print() function of Manager class
4. In the print function of department class, you will be asked
product statistics such as total salaries, total bonuses, average
salary, lowest salary and higest salary.
5. Implement a function to get staff who have salaries in a
range from lowest to highest salaries
Note: To compute total bonuses, you have to collect bonus of all
staff. You have to implement get_bonus function for Staff and
return zero as staff does not have bonus like manager
------------------------------------------
/*
* manager.cpp
*
*/
#include <iostream>
#include <string>
#include<iomanip>
#include "manager.h"
using namespace std;
Manager::Manager()
: Staff()
{
}
void Manager::print() const
{
cout << setw(10) << left << "Manager: "
<< setw(15) << get_name()
<<
setw(10) << "Salary: " << setw(10) << right
<< get_salary()
<< setw(10)
<< left << " position: " << setw(15)
<< get_position() << "\n";
}
---------------------------------------
/*
* Manager.h
*
* Date:
* Author
*/
#ifndef MANAGER_H
#define MANAGER_H
#include <iostream>
#include <string>
#include "staff.h"
using namespace std;
class Manager : public Staff
{
public:
Manager();
void print() const;
private:
};
#endif /* MANAGER_H */
--------------------------------------------
#include<iostream>
#include<string>
#include<iomanip>
#include "staff.h"
using namespace std;
Staff::Staff()
{
}
Staff::Staff(string name, double salary, string pos)
: Employee(name, salary)
{
position = pos;
}
void Staff::set_position(string new_position)
{
string old = position;
position = new_position;
cout << get_name() << " changed
position from " << old << " to " << new_position
<< endl;
}
--------------------------------------------
#ifndef STAFF_H
#define STAFF_H
#include<iostream>
#include<string>
#include "ccc_empl.h"
using namespace std;
class Staff : public Employee
{
public:
Staff();
Staff(string name, double salary, string pos);
void set_position(string new_position);
string get_position() const;
void print() const;
private:
string position;
};
#endif
---------------------------------------
#include <string>
#include <iostream>
#include <vector>
#include <iomanip>
#include "staff.h"
#include "department.h"
#include "manager.h"
using namespace std;
int main()
{
// create staff in heap
Staff* harry = new Staff("Hacker, Harry", 100000,
"Manager");
Staff* thomas = new Staff("Budds, Thomas", 45000,
"Programmer");
Staff* john = new Staff("Lee, john", 90000,
"Programmer");
Staff* monica = new Staff("White, Monica", 90000,
"Programmer");
// create department shipping
Department shipping("Shipping");
// add Staff to shipping department
shipping.add_staff(harry);
shipping.add_staff(thomas);
shipping.add_staff(john);
shipping.add_staff(monica);
// show Shipping department
shipping.print();
cout << endl;
// change Department's name
shipping.set_department("Global Shipping");
// change John's position
john->set_position("Director");
// increase Thomas's salary 20%
thomas->set_salary(thomas->get_salary()*1.2);
// increase Harry's to Larry's
john->set_salary(harry->get_salary());
// remove john from Shipping department
shipping.remove_staff(monica);
// create new Manager mike and add him to
Shipping department
// display shipping department
cout << "After updates" << endl;
shipping.print();
// vector of staff with higest salary
vector<Staff*> all_highest =
shipping.get_all_highest_staff();
cout << "Staff who have highest salary" <<
endl;
for ( int i=0; i < all_highest.size(); i++)
{
all_highest->print();
}
delete harry;
delete thomas;
delete john;
delete monica;
return 0;
}
---------------------------------
b#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <string>
#include <iostream>
#include <vector>
#include <iomanip>
#include "manager.h"
using namespace std;
/**
A department in an organization.
*/
class Department
{
public:
Department(string n);
Department(string n, Manager* mgr);
void add_staff(Staff* e);
void set_department(string new_dept);
void remove_staff(Staff* e);
string get_department() const;
void set_manager(Manager* mgr);
Manager* get_manager() const;
void print() const;
vector<Staff*> get_all_highest_staff()
const;
private:
string name;
Manager* manager;
vector<Staff*> staff;
};
#endif
-------------------------------------
#include <string>
#include <iostream>
#include <vector>
#include <iomanip>
#include "department.h"
using namespace std;
/**
Constructs a department with a given name.
@param n the department name
*/
Department::Department(string n)
{
name = n;
staff.reserve(20);
manager = NULL;
}
Department::Department(string n, Manager* mgr)
{
name = n;
staff.reserve(20);
manager = mgr;
}
void Department::set_manager(Manager* mgr)
{
manager = mgr;
}
/**
Add new Staff
@param e the Staff
*/
void Department::add_staff(Staff* e)
{
staff.push_back(e);
}
void Department::set_department(string new_name)
{
name = new_name;
}
/*
get a vector of staff who have highest salary
@return vector of Staff
*/
vector<Staff*> Department::get_all_highest_staff()
const
{
vector<Staff*> all_highest;
if( staff.size() == 0 )
{
return all_highest;
}
Staff* highest = staff[0];
double highest_sal = staff[0]->get_salary();
for(int i = 0; i < staff.size(); i++)
{
if( highest_sal < staff->get_salary()
)
{
highest_sal = staff->get_salary();
}
}
for(int i = 0; i < staff.size(); i++)
{
if( highest_sal == staff->get_salary() )
{
all_highest.push_back(staff);
}
}
return all_highest;
}
/**
Remmove a Staff member from vector of
Staff
@param e the Staff
*/
void Department::remove_staff(Staff* e)
{
for ( int i = 0; i < staff.size(); i++)
{
if ( e == staff )
{
staff =
staff[staff.size()-1];
staff.pop_back();
return;
}
}
cout << e->get_name() << " Not found"
<< endl;
return;
}
/**
get name of department
@return department name, string
*/
string Department::get_department() const
{
return name;
}
/**
get Manager
@return manager
*/
Manager* Department::get_manager() const
{
return manager;
}
/**
Diplay department and list of employees in the department
*/
void Department::print() const
{
if ( staff.size() == 0)
{
cout << get_department()
<< " department has
no staff" << "\n";
return;
}
cout << "Department: " <<
get_department() << "\n";
for ( int i = 0; i < staff.size(); i++)
{
if(staff != NULL)
{
cout
<< i+1 << ". ";
staff->print();
}
}
return;
}
-------------------------------------
#ifndef CCC_EMPL_H
#define CCC_EMPL_H
#include <string>
using namespace std;
/**
A basic employee class that is used in many
examples
in the book "Computing Concepts with C++
Essentials"
*/
class Employee
{
public:
/**
Constructs an employee with empty name and no
salary.
*/
Employee();
/**
Constructs an employee with a given name and
salary.
@param employee_name the employee name
@param initial_salary the initial salary
*/
Employee(string employee_name, double
initial_salary);
/**
Sets the salary of this employee.
@param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
Gets the salary of this employee.
@return the current salary
*/
double get_salary() const;
/**
Gets the name of this employee.
@return the employee name
*/
string get_name() const;
private:
string name;
double salary;
};
#endif
----------------------------------
#include "ccc_empl.h"
Employee::Employee()
{
salary = 0;
}
Employee::Employee(string employee_name, double
initial_salary)
{
name = employee_name;
salary = initial_salary;
}
void Employee::set_salary(double new_salary)
{
salary = new_salary;
}
double Employee::get_salary() const
{
return salary;
}
string Employee::get_name() const
{
return name;
}
-------------------------------------
makefile
# make departmentReivew
departmentReview: departmentReview.o department.o staff.o
ccc_empl.o manager.o
g++ -o departmentReview departmentReview.o
department.o staff.o ccc_empl.o manager.o
departmentReview.o: departmentReview.cpp
g++ -c departmentReview.cpp
department.o: department.cpp department.h
g++ -c department.cpp
staff.o: staff.cpp staff.h
g++ -c staff.cpp
ccc_empl.o: ccc_empl.cpp ccc_empl.h
g++ -c ccc_empl.cpp
manager.o: manager.cpp manager.h
g++ -c manager.cpp
clean:
rm -f *.o
Add the following to the programs: 1. Add a bonus attribute (in private data fields for manager class 2. Add a set_bonus
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am