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.