Hey guys, I'm looking to add on to a recent program of mine that uses classes. My next step is to add in a working overloaded equality function in order to compare the gross pay of two Employees within the class. Now, I've read up on this quite a bit but I can't seem to quite figure out how to set this up correctly. The overloaded function deal is a bit over my head at the moment, I don't get exactly what makes it so different from a regular ol' function.


Here is my function as it stands now, but I know this doesn't work because of the call to getGrossPay() function. Problem is, I just can't figure out exactly how to get it to work correctly, or if my overloaded function is even set up properly.

bool operator==(const Employee& e1, const Employee& e2)
{
        cout << "Employee::Operator==(const Employee& e1, const Employee& e2)" << endl;
        return e1.getGrossPay() == e2.getGrossPay();
}

Here is the rest of the structure of my code, if that helps

//Header File

#include <iostream>
#include <string>

class Employee {

        public:
                Employee();
                Employee(int id, std::string f_name, std::string l_name, double hoursworked, double hourlyrate);

                int getID();
                double getNetPay();
                double getGrossPay();
                double getTax();
                double getHRate();
                double getHWorked();
                std::string getFname();
                std::string getLname();
                std::string getString();
                std::string EString();

                void setFname(std::string f_name);
                void setLname(std::string l_name);
                void setHRate(double hourlyrate);
                void setHWorked(double hoursworked);

                friend std::ostream &operator<< (std::ostream &obj, Employee str);
                friend bool operator==(const Employee& e1, const Employee& e2);


          private:
                double hoursworked, hourlyrate, tax;
                int id;
                std::string f_name, l_name;
                double totalpay;
                int i_;
};
#include "employee.h"
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <sstream>

#include <cstring>
using std::strcmp;
using std::strcpy;
using std::strcat;
using namespace std;
Employee::Employee()
{

}

Employee::Employee(int id, std::string f_name, std::string l_name, double hoursworked, double hourlyrate)
{
        this->tax = 30;
        this->id = id;
        this->f_name = f_name;
        this->l_name = l_name;
        this->hoursworked = hoursworked;
        this->hourlyrate = hourlyrate;
}

std::string Employee::EString()
{
        std::string string = "";

        {std::ostringstream oss;
                oss << getID();
                string+= oss.str();
        }

        string+="\t" + getFname();
        string+="\t\t" + getLname();

        {std::ostringstream oss;
                oss << getHWorked();
                string+="\t\t" + oss.str();
        }

        {std::ostringstream oss;
                oss << getHRate();
                string+="\t$" + oss.str();
        }

        {std::ostringstream oss;
                oss << getGrossPay();
                string+="\t$" + oss.str();
        }

        {std::ostringstream oss;
                oss << getTax();
                string+="\t" + oss.str()+"%";
        }

        {std::ostringstream oss;
                oss << getNetPay();
                string+="\t$" + oss.str();
        }

        return string;
}

std::ostream &operator << (std::ostream &obj, Employee p)
{
        return obj << p.EString();
}




int Employee::getID()
{
        return id;
}

double Employee::getNetPay()
{
        return getGrossPay() * (1 - (tax * 0.01));
}

double Employee::getGrossPay()
{
        double grosspay;

        if(hoursworked > 40)
                return (hoursworked * hourlyrate) * 1.5;
        else
                return (hoursworked * hourlyrate);
}
double Employee::getTax()
{
        return tax;
}

double Employee::getHRate()
{
        return hourlyrate;
}

double Employee::getHWorked()
{
        return hoursworked;
}

std::string Employee::getFname()
{
        return f_name;
}

std::string Employee::getLname()
{
        return l_name;
}

void Employee::setFname(std::string f_name)
{
        this->f_name = f_name;
}

void Employee::setLname(std::string l_name)
{
        this->l_name = l_name;
}

void Employee::setHRate(double hourlyrate)
{
        this->hourlyrate = hourlyrate;
}

void Employee::setHWorked(double hoursworked)
{
        this->hoursworked = hoursworked;
}

bool operator==(const Employee& e1, const Employee& e2)
{
        cout << "Employee::Operator==(const Employee& e1, const Employee& e2)" << endl;
        return e1.getGrossPay() == e2.getGrossPay();
}

I understand this is a lot of very beginner-level code to trudge through, but I would really appreciate any help anyone could offer in helping to grasp this overloaded concept.

I tried overloading the == operator and found that the following code worked.

bool operator== (const Employee& temp)
{
	if(&temp == this)
		return true;

	bool ret = true;

	if(getGrossPay() != temp.getGrossPay())
		ret = false;
	return ret;
}

You didn't need to have two parameters because the object calling the function already counts as one of the objects your comparing.

Well hello again smcp :-)

You actually got the part about operator overloading right. The problem is the keyword 'const'.

One important aspect of 'const' is that:
a) A non-const object can call both const and non-const functions.
b) A const object can call const functions, but NOT non-const functions.

Have a look at this example:

#include <iostream>

class FunWithConst
{
    public:
        void NonConstFunction();
        void ConstFunction() const;
};

void FunWithConst::NonConstFunction()
{
    std::cout << "NonConstFunction" << std::endl;
}

void FunWithConst::ConstFunction() const
{
    std::cout << "ConstFunction" << std::endl;
}

int main()
{
    FunWithConst aNonConstObject;
    const FunWithConst aConstObject;

    aNonConstObject.ConstFunction();
    aNonConstObject.NonConstFunction();
    aConstObject.ConstFunction();
    aConstObject.NonConstFunction(); //ERROR: WILL NOT COMPILE
}

You have two options. You can make the arguments in 'operator==' non-const or you can make 'getGrossPay' const.

Personally I would make 'getGrossPay' const. By making it const you tell the compiler that the function will NOT change the value of the object.

Thanks guys, I appreciate you taking the time to help me out.

Green frog, I tried following your guidelines and changed my code around, but it kept returning an error that the overloaded function must take exactly two arguments.

Mb, nice to see you again :)

Did you mean editing the 'double grosspay' to become constant? I'm not sure exactly what you mean, but I tried changing that to a constant it returns an error about an uninitialized constant.

Also, now for some reason I am getting this error:

employee.cpp:144: error: passing âconst Employeeâ as âthisâ argument of âdouble Employee::getGrossPay()â discards qualifiers
employee.cpp:144: error: passing âconst Employeeâ as âthisâ argument of âdouble Employee::getGrossPay()â discards qualifiers

Scratch that, I fixed that error. Read your code a bit more thoroughly and saw that I should add a const after the function to make it getGrossPay() const, which cleared up my error. Now, to figure out how to test this thing :)

And it all works perfectly now :)

Mbulow, I really appreciate your assistance, you have really helped me out more than you can imagine in these past few days.

In employee.h change

double getGrossPay();

to

double getGrossPay() const;

In employee.cpp change

double Employee::getGrossPay()
{
...
}

to

double Employee::getGrossPay() const
{
...
}

These two changes makes the getGrossPay-function constant. This tells the compiler that the function will NOT change the state of an Employee-object.

The arguments in operator== are declared as const objects.
Const objects can't change state (member variables can't change value).
Because these objects are const, only member-functions that are const as well can be called.

EDIT: Glad to see you fixed it :)

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.