hi im doing a project for my class and cant figure out how to code some of it. for the main .cpp file heres the code:

#include <stdafx.h>
#include "derivedClass.h"
#include <iostream>
#include <string>
using namespace std;
string firstName="";
string lastName="";
int num;
void getName()
{
cout << "\nEnter employee's first name: ";
cin >> firstName;
cout << "\nEnter employee's last name: ";
cin >> lastName;
};

void multipleWorkers()
{
char answer;
cout << "\nWould you like to enter a new employee record?('y','n')\n";
cin >> answer;
if (answer=='y')
{
getName();
derivedClass myEmployee(firstName, lastName, 35, 50, 10);
myEmployee.calcPay();
++num;
multipleWorkers();
}
else if (answer=='n')
{
cout << "\nYou entered "
<< num
<< " number of records.\n";
}
else
{
cout << "\nYou entered an incorrect character, please try again.";
multipleWorkers();
}
};

int main() {

multipleWorkers();

return 0;
}

______________________________________
then for the derivedClass.h heres the code:

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

class derivedClass: public abstractClass {

public:
//Clerical variables
double payRate;
double hoursWorked;
double overTime;

//Default constructor
derivedClass () {};
//Overloading function
derivedClass (string FName, string LName, double payAmount, double hours, double ot)
{
lName=LName;
fName=FName;
payRate=payAmount;
hoursWorked=hours;
overTime=ot;
};
//Calculate clerical pay
double calcPay()
{
//calcPay variables
double pay;
double overTimePay;
double otPay;
//Calculates overtime pay(time and a half)
otPay = payRate * 1.5;
//Calculates overtime wage
overTimePay = overTime * otPay;
//Calculates entire pay
pay = hoursWorked * payRate + overTimePay;
//Outputs individual's pay
};
};

__________________________________________
finally the abstractClass.h:

//Preprocessor to prevent multiple definitions of header files
#ifndef _guard_
#define _guard_
#include <iostream>
#include <string>
using namespace std;
//Abstract superclass
class abstractClass {

public:
virtual double calcPay()=0;
//Variables
string lName;
string fName;
string empID;
//Default construct
abstractClass (){};
//Overloading function
abstractClass (string FName, string LName)
{
lName = LName;
fName = FName;
};
};
#endif

***********************************
basically this all works fine and does what i want it to. but i cant figure out how to implement operator overloading into the project(one of the requirements) and i also need to display the highest/lowest/average net pays with the employee names next to them. any ideas would be extremely helpful. also i dont really understand operator overloading and how to use it so any information on that would also be greatly appreciated.
***********************************

Which operator do you need to overload? Such as, do you need to write and read the classes to file? IN that case you could overload the << and >> operators.

Which operator do you need to overload? Such as, do you need to write and read the classes to file? IN that case you could overload the << and >> operators.

the specifications just say i need to have an example of overloading operators, doesn't matter which one i do. i just dont know how.

Lets say you want to write the class out to a file, then overload the << oeprator

class derivedClass: public abstractClass 
{
public:
    friend ostream& operator<< (ostream& out, derivedClass& cl);
};

// in the implementation file

ostream& operator<< (ostream& out, derivedClass& cl)
{

   out << cl.payRate << " " << hoursWorked << " " << overtime
        << lname << " " << fname << " " << empID << "\n";
   return out;
}

Now do something similar with the >> operator.

This article has been dead for over six months. Start a new discussion instead.