Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://pastebin.com/u/TheWhiteFang
- // Tutorial 8 Question 1 v2 [Inheritance]
- #include <iostream>
- #include <string>
- using namespace std;
- class Employee
- {
- private:
- string m_name;
- string m_company;
- string m_deptName;
- unsigned int m_id;
- protected:
- double m_salary;
- public:
- Employee(string inName, int inId, double inSalary){
- m_name = inName;
- m_id = inId;
- m_salary = inSalary;
- }
- void SetCompanyName(string inComName){
- m_company = inComName;
- }
- void SetDepartmentName(string inDeptName){
- m_deptName = inDeptName;
- }
- void GetEmployeeName(string &name){ // returned as output argument
- name = m_name; // passed by address
- }
- //void GetEmployeeName(string *name){ // pass by referenece
- // *name= m_name;
- //
- //}
- unsigned int GetEmployeeId(){ return m_id; }
- void GetAnnualSalary(double &inSalary){
- inSalary = m_salary; //returns employee annual salary as output argument
- //format
- //outputarg = inputarg;
- }
- };
- class Manager : public Employee //inheritance
- {
- private:
- unsigned int m_Exec;
- public:
- Manager(string inName, int inId, double inSalary)//para constr takes in same arg as employee constr
- :Employee(inName, inId, inSalary) // call the parent class constructor
- {
- m_Exec = 0;
- }
- ~Manager() = default;
- void SetNoExecutives(unsigned int exec){
- m_Exec = exec;
- }
- double CalculateBonus(){
- //double salary =0.0;
- //GetAnnualSalary(salary);
- //
- //we can also make the m_salary into protected
- return (m_salary*m_Exec*0.15);
- }
- };
- class Engineer : public Employee
- {
- private:
- unsigned int m_Tech;
- public:
- Engineer(string inName, int inId, double inSalary)
- :Employee(inName, inId, inSalary)
- {
- m_Tech = 0;
- }
- void SetNoTechnicians(unsigned int Tech){
- m_Tech = Tech;
- }
- double CalculateBonus(){
- //Bonus = Salary * (Technicians * 10%)
- return (m_salary*m_Tech*0.10);
- }
- };
- int main(){
- string name, companyName, departmentName;
- unsigned int id, execs, techs;
- double salary;
- cout << "Enter Manager's name: ";
- getline(cin, name);
- cout << "Enter Company's name: ";
- getline(cin, companyName);
- cout << "Enter Dept. name: ";
- getline(cin, departmentName);
- cout << "specify Manager's id: ";
- cin >> id;
- cout << "specify no of Executives under manager: ";
- cin >> execs;
- cout << "specify Manager's annual salary: ";
- cin >> salary;
- /*cin.clear();*/
- cout << "Enter Engineer's name: ";
- cin >>name;
- cout << "specify Engineer's id: ";
- cin >> id;
- cout << "specify no of Technicians under manager: ";
- cin >> techs;
- cout << "specify Engineer's annual salary: ";
- cin >> salary;
- Manager obj(name, id, salary);
- obj.SetCompanyName(companyName);
- obj.SetDepartmentName(departmentName);
- obj.SetNoExecutives(execs);
- double bonus = obj.CalculateBonus();
- Engineer obj1(name, id, salary);
- obj1.SetNoTechnicians(techs);
- double bonus2 = obj1.CalculateBonus();
- cout << "Manager's Bonus is: " << bonus << endl << endl;
- cout << "Engineer's Bonus is: " << bonus2 << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement