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;
}

Edited 3 Years Ago by happygeek: fixed formatting

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.

This question has already been answered. Start a new discussion instead.