| #include<iostream.h> #include<string.h> class Employee { protected: char *name; float payRate; public: Employee(char theName[], float thePayRate); char* getName() const; float getPayRate() const; float pay(float hoursWorked) const; }; //--------------------------------------------- Employee::Employee(char *theName, float thePayRate) { //strcpy(name,theName); name=theName; payRate = thePayRate; } //-------------------------------------------- char* Employee::getName()const { return name; } //-------------------------------------------- float Employee::getPayRate() const { return payRate; } //--------------------------------------------- float Employee::pay(float hoursWorked) const { return hoursWorked * payRate; } //========================================= class Manager : public Employee { public: Manager(char* theName,float thePayRate, int isSalaried); int getSalaried() const; float pay(float hoursWorked) const; protected: int salaried; }; //--------------------------------- Manager::Manager(char* theName ,float thePayRate,int isSalaried) : Employee(theName, thePayRate) { salaried = isSalaried; } //------------------------------------ int Manager::getSalaried() const { return salaried; } //-------------------------------------- float Manager::pay(float hoursWorked) const { if (salaried) return payRate; /* else */ return Employee::pay(hoursWorked); } //----------------------------------- main(){ Employee *emplP; if (0) emplP = new Employee("EMP",4.0); else emplP = new Manager("MANAGER",5.5,1); cout << "\n Name: " << emplP->getName(); cout << "\n Pay rate: " << emplP->getPayRate(); //----------- cout<<"\n\n What, however, will happen when a method that was overridden is called?\n"; cout<<" ( Object Call ) --> Static binding \n"; Employee empl("Mohd",5.0); Manager mgr("Modeer",6.5,1); cout << "\n Pay: " << empl.pay(40.0); // calls Employee::pay() cout << "\n Pay: " << mgr.pay(40.0); // calls Manager::pay() //---------- cout<<"\n\n ( Pointer Call ) --> Dynamic binding \n"; emplP = &empl; // make point to an Employee cout << "\n Pay: " << emplP->pay(40.0); // calls Employee::pay() emplP = &mgr; // make point to a Manager cout << "\n Pay: " << emplP->pay(40.0); // calls Employee::pay() //------------------------------------- } |