Ref Polymorphism:

I am trying to get the method calculate_pay() to work. As each type of doctor will have a differnt payrate I am trying to call the calculate_pay() but i am having difficulty. Should it be

int doctor::calculate_pay()
{
return (doctor::get_Payrate()*hours);
}

will this line return each type of doctors payrate multiplied by hours or will it only call the doctor payrate??? or am i way off track

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;



class doctor{

    protected:
    string newname;
    string address; 
    int payrate; 
    int hours;
    int age;   

    public:

    doctor();   //constructor
    virtual ~doctor();
    virtual void set_Name(string itsname);  
    virtual string get_Name();  

    virtual void set_Address(string itsaddress);
    virtual string get_Address();    

    virtual void set_Age(int itsage);  
    virtual int get_Age();   

    virtual void set_Hours(int itshours);  
    virtual int get_Hours();  

    //virtual void set_Payrate();
    virtual int get_Payrate();  

    virtual void get_holidaypay();
    [COLOR=Red]virtual int calculate_pay();[/COLOR]

    };          //end of class declaration

    doctor::doctor()
    {    
    } 

    doctor::~doctor()
    {
    cout<<"Doctor Destructer called"<<endl;
    }                                         

    void doctor::set_Name(string itsname){
    newname=itsname;
     }
    string doctor::get_Name(){
    return newname;
    }

    void doctor::set_Address(string itsaddress){
    address=itsaddress;
    }

    string doctor::get_Address(){
    return address;
    }

    void doctor::set_Age(int itsage){
    age=itsage;
    }

    int doctor::get_Age(){
    return age; 
    }

    void doctor::set_Hours(int itshours){
    hours=itshours;
    }

    int doctor::get_Hours(){
    return hours;
    }

    //void doctor::set_Payrate(int itspay){
    //payrate=itspay;
    //}

    int doctor::get_Payrate(){
    return payrate=100;
    }

    void doctor::get_holidaypay(){
    cout<<"You Have weekend off "<<endl;
    }

    [COLOR=Red]int doctor::calculate_pay()
    {
    return (doctor::get_Payrate()*hours);
    }[/COLOR]


    //Junior Doctor Class 

    class junior: virtual public doctor
    {
    public:
    junior(){}
    virtual ~junior();
    virtual void get_holidaypay();
    virtual int get_Payrate();


    }; 

    void junior::get_holidaypay()
    {
    cout<<"You Have 2months off "<<endl;
    }

    int junior::get_Payrate(){
    return payrate=200;
    }

    junior::~junior()
    {
    cout<<"junior destroctor called"<<endl;
    }


    //Consultant Class
    class consultant:virtual public doctor
    {
    public:
    virtual void get_holidaypay();
    consultant(){}  
    virtual ~consultant();
    virtual int get_Payrate();

    };                   

   consultant::~consultant()
   {
   cout<<"Consultant Destructor"<<endl;
   }

   int consultant::get_Payrate(){
   return payrate=600;
   }

   void consultant::get_holidaypay()
   {
   cout<<"You Have 6 months off "<<endl;
   }


int main(int argc, char *argv[])
{
  doctor* Array[5];
  doctor* newdoctor;
  int choice;
  int i;
  int ag;
  string nm;
  int a,b;
  string c;
  int hrs;

  for(int i=0;i<3;i++)
  {
  cout<<"1.doctor, 2.Junior, 3.Consultant"<<endl;
  cin>>choice;
   if (choice==1)
      newdoctor = new doctor();
  else if (choice==2)
      newdoctor = new junior();
  else
      newdoctor = new consultant();

  cout<<"Enter doctor name"<<endl;
  cin>>nm;
  newdoctor->set_Name(nm);
  cout<<"Enter doctor Age"<<endl;
  cin>>ag;
  newdoctor->set_Age(ag);
  cout<<"Enter no of hrs worked"<<endl;
  cin>>hrs;






  Array[i]=newdoctor;

  }

  cout<<"\n";

  for(i=0;i<3;i++)
  {
  cout<<Array[i]->get_Name();
  cout<<"\n";
  cout<<Array[i]->get_Age();
  cout<<"\n";
  Array[i]->get_holidaypay();
  cout<<"\n";
  cout<<Array[i]->calculate_pay();
  cout<<"\n";
  cout<<Array[i]->get_Payrate();
  cout<<"\n";
  delete Array[i];
  }


  system("PAUSE");  
  return 0;
}

Dani AI

Generated

Short answer: calling doctor::get_Payrate() forces the base-class implementation; it bypasses polymorphism. To get the derived class rate at runtime, call the virtual function without the class qualifier so the call goes through the vtable. Also note that virtual calls from a constructor or destructor do not dispatch to derived overrides — they resolve to the current class's implementation.

A minimal fix is to have calculate_pay call the virtual getter unqualified (for example, return get_Payrate() * hours;). I see later added the missing set_Hours(hrs) — that was required so hours actually contains the entered value.

Design tips to avoid brittle behavior:

  • Don’t make a getter have side effects (avoid payrate = 200 inside get_Payrate). Make the getter const and return a value, or set the member in the constructor/initializer.
  • If every derived doctor must supply its own rate, consider making get_Payrate() pure virtual in doctor and keep calculate_pay() implemented once in the base (non-virtual) so calculation logic is centralized.
  • Use modern hints where possible: override on derived methods, const on accessors, and initialization lists for members.

Other small cleanups: you don’t need virtual inheritance here (no diamond), keep the virtual destructor (you already have one), and avoid system("PAUSE") in portable code. With those changes you’ll get correct, predictable polymorphic pay calculations for junior/consultant instances.

DOWWWWWWWWW;

starting to get the hand of this C++ stuff.

cout<<"Enter no of hrs worked"<<endl;
cin>>hrs;
newdoctor->set_Hours(hrs);// i forgot this line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.