1

For my exam practice I am trying to overload ">" operator to check if left handside is bigger than right handside.

It needs to compare salaries from two different classes. But only PartTimeDoctor class has "salary" variable. FullTimeDoctor class has "baseSalary" variable which calculated differently from "salary".

The original question is,

Overload ‘greater than’ operator as a global function which gets two Doctor objects as parameters and returns true if the left hand side doctor object earns more than that of right hand side doctor.

Base class:

class Doctor {

protected:
    string name, surname, expertise;

public:

    Doctor();
    void print();
    void calcTotalPayment();
};

Inherited Class:

class FullTimeDoctor : public Doctor {

private:

    int baseSalary;
    int feeShift;

public:

    void setBaseSalary(int);
    int getBaseSalary();

    void setFeeShift(int);
    int getFeeShift();

    FullTimeDoctor();

    void print();

    void calcTotalPayment();

};

Inherited Class

class PartTimeDoctor : public Doctor {

private:

    int feePerEx;
    int numOfEx;
    int salary;

public:

    PartTimeDoctor();

    void setFeePerEx(int);
    int getFeePerEx();

    void setNumOfEx(int);
    int getNumOfEx();

    void calcTotalPayment();

    void print();
};

I did not add cpp files here but they are all ok. So how can I do this overloading? Thanks in advance.

  • 1
    You'll need to either 1) Have a common `Doctor` method that will return the values you need to compare and write your greater than operator in the `Doctor` class 2) Write `bool FullTimeDoctor::operator>(const FullTimeDoctor&)`, `bool FullTimeDoctor::operator>(const PartTimeDoctor&)`, `bool PartTimeDoctor::operator>(const FullTimeDoctor&)`, and `bool PartTimeDoctor::operator>(const PartTimeDoctor&)` – Jonathan Mee May 18 '16 at 15:47
  • Shouldn't `calcTotalPayment()` return some numeric value? It appears that its result should be decisive for the comparison. If so, make it virtual and implement `bool operator>(Doctor const &a, Doctor const &b)`. – Thomas B Preusser May 18 '16 at 15:54

0 Answers0