Advertisement
TheWhiteFang

Tutorial 8 Question 1 v2 [Inheritance]

Dec 14th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. // http://pastebin.com/u/TheWhiteFang
  2. // Tutorial 8 Question 1 v2 [Inheritance]
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Employee
  8. {
  9. private:
  10.  
  11.     string m_name;
  12.     string m_company;
  13.     string m_deptName;
  14.     unsigned int m_id;
  15.  
  16. protected:
  17.     double m_salary;
  18.  
  19. public:
  20.  
  21.     Employee(string inName, int inId, double inSalary){
  22.  
  23.         m_name = inName;
  24.         m_id = inId;
  25.         m_salary = inSalary;
  26.  
  27.     }
  28.  
  29.     void SetCompanyName(string inComName){
  30.  
  31.         m_company = inComName;
  32.     }
  33.  
  34.     void SetDepartmentName(string inDeptName){
  35.  
  36.         m_deptName = inDeptName;
  37.  
  38.     }
  39.  
  40.     void GetEmployeeName(string &name){ // returned as output argument
  41.  
  42.         name = m_name; // passed by address
  43.  
  44.     }
  45.  
  46.     //void GetEmployeeName(string *name){ // pass by referenece
  47.  
  48.     //      *name= m_name;
  49.     //
  50.     //}
  51.  
  52.     unsigned int GetEmployeeId(){ return m_id; }
  53.  
  54.     void GetAnnualSalary(double &inSalary){
  55.  
  56.         inSalary = m_salary; //returns employee annual salary as output argument
  57.         //format
  58.         //outputarg = inputarg;
  59.     }
  60.  
  61.  
  62.  
  63. };
  64.  
  65.  
  66. class Manager : public Employee //inheritance
  67. {
  68. private:
  69.     unsigned int m_Exec;
  70.    
  71.  
  72. public:
  73.     Manager(string inName, int inId, double inSalary)//para constr takes in same arg as employee constr
  74.         :Employee(inName, inId, inSalary) // call the parent class constructor
  75.     {
  76.         m_Exec = 0;
  77.     }
  78.  
  79.     ~Manager() = default;
  80.  
  81.     void SetNoExecutives(unsigned int exec){
  82.  
  83.         m_Exec = exec;
  84.     }
  85.  
  86.     double CalculateBonus(){
  87.  
  88.         //double salary =0.0;
  89.         //GetAnnualSalary(salary);
  90.         //
  91.  
  92.         //we can also make the m_salary into protected
  93.         return (m_salary*m_Exec*0.15);
  94.     }
  95.  
  96. };
  97.  
  98. class Engineer : public Employee
  99. {
  100. private:
  101.  
  102.     unsigned int m_Tech;
  103.  
  104. public:
  105.     Engineer(string inName, int inId, double inSalary)
  106.         :Employee(inName, inId, inSalary)
  107.     {
  108.    
  109.         m_Tech = 0;
  110.    
  111.     }
  112.  
  113.     void SetNoTechnicians(unsigned int Tech){
  114.  
  115.         m_Tech = Tech;
  116.  
  117.     }
  118.  
  119.     double CalculateBonus(){
  120.  
  121.         //Bonus = Salary * (Technicians * 10%)
  122.         return (m_salary*m_Tech*0.10);
  123.     }
  124.  
  125.  
  126. };
  127.  
  128.  
  129. int main(){
  130.  
  131.  
  132.     string name, companyName, departmentName;
  133.     unsigned int id, execs, techs;
  134.     double salary;
  135.  
  136.     cout << "Enter Manager's name: ";
  137.     getline(cin, name);
  138.     cout << "Enter Company's name: ";
  139.     getline(cin, companyName);
  140.     cout << "Enter Dept. name: ";
  141.     getline(cin, departmentName);
  142.     cout << "specify Manager's id: ";
  143.     cin >> id;
  144.     cout << "specify no of Executives under manager: ";
  145.     cin >> execs;
  146.     cout << "specify Manager's annual salary: ";
  147.     cin >> salary;
  148.    
  149.     /*cin.clear();*/
  150.     cout << "Enter Engineer's name: ";
  151.     cin >>name;
  152.     cout << "specify Engineer's id: ";
  153.     cin >> id;
  154.     cout << "specify no of Technicians under manager: ";
  155.     cin >> techs;
  156.     cout << "specify Engineer's annual salary: ";
  157.     cin >> salary;
  158.  
  159.     Manager obj(name, id, salary);
  160.     obj.SetCompanyName(companyName);
  161.     obj.SetDepartmentName(departmentName);
  162.     obj.SetNoExecutives(execs);
  163.     double bonus = obj.CalculateBonus();
  164.  
  165.  
  166.     Engineer obj1(name, id, salary);
  167.    
  168.     obj1.SetNoTechnicians(techs);
  169.     double bonus2 = obj1.CalculateBonus();
  170.  
  171.     cout << "Manager's Bonus is: " << bonus << endl << endl;
  172.     cout << "Engineer's Bonus is: " << bonus2 << endl << endl;
  173.  
  174.  
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement