SabirSazzad

Virtual function

Feb 23rd, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4. class Employee{
  5. protected:
  6.     string name;
  7.     float salary;
  8. public:
  9.     Employee(string name, float salary){this->name = name; this->salary = salary;}
  10.     virtual float calculateBonus(){return this->salary;}
  11.     string getName(){return this->name;}
  12.  
  13. };
  14.  
  15. class Manager: public Employee{
  16. public:
  17.     Manager(string name, float salary):Employee(name, salary){}
  18.     float calculateBonus(){return this->salary+this->salary*.5;}
  19.  
  20. };
  21.  
  22.  
  23. class SeniorManager: public Employee{
  24. public:
  25.     SeniorManager(string name, float salary):Employee(name, salary){}
  26.     float calculateBonus(){return this->salary+this->salary*.7;}
  27.  
  28. };
  29.  
  30. void printBonus(Employee *emp){
  31.     cout<<emp->getName()<<"\t"<<emp->calculateBonus()<<endl;
  32. }
  33.  
  34. int main(){
  35. Employee e("x", 100);
  36. Manager m("m", 200);
  37. SeniorManager sm("Sr.Mngr", 300);
  38.  
  39. Employee *ep[3];
  40. ep[0] = &e;
  41. ep[1] = &m;
  42. ep[2] = &sm;
  43.  
  44. for(int i=0; i<3; i++)
  45.     printBonus(ep[i]);
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment