I've looked around and it turns out there isn't a C++ version of the java instanceof operator. I've also found out that using instanceof is supposed to be bad, for some reason.
So let me tell you why I want to use this instanceof operator so badly. I've got a Vector that holds two types of objects, either Manager or Casual classes, that are both derived from the base class StaffMember.
I want to display the Salary for Manager objects, and the hourlyWage and hoursWorked for the Casual object. First I have to determine whether the item in the Vector is a Manager or a Casual object. So I went looking for an instanceof operator, but no such thing exists. Then I was pointed towards the typeid() function.
I tried it out and my objects are declared as the base class StaffMember. So I think that as I am adding them to this Vector, they are being cast as StaffMembers.
So right now I'm thinking about making two seperate vectors, one for Managers and another for Casual objects. However, it just isn't in the spirit of the assessment. I'll show you some code segments and provide a link to the full project for anyone interested.
From Company.cpp
void Company::addStaff(StaffMember* x)
{
staffList.push_back(x);
numStaff++;
}
void Company::addStaff(Casual* x)
{
staffList.push_back(x);
numStaff++;
}
void Company::addStaff(Manager* x)
{
staffList.push_back(x);
numStaff++;
}
From Company.h
class Company
{
private:
string name;
vector<StaffMember*> staffList;
int numStaff;
public:
//Constructors
Company(string name);
Company();
//Writers
void setName(string name);
//Readers
string getName();
StaffMember* getStaff(int ID);
//Mutators
void addStaff(StaffMember* x);
void addStaff(Manager* x);
void addStaff(Casual* x);
};
From payroll.cpp (The client)
void addFour(Company* westaff)
{
westaff->addStaff(new Manager("Bill Gates", 200000));
westaff->addStaff(new Casual("Angus Cheng", 36, 19.8));
westaff->addStaff(new Manager("Fernando Alonso", 49888));
westaff->addStaff(new Manager("Nelson Piquet", 8000));
}
[Full listing](http:// rapidshare.com/files/215333186/payRoll.rar.html)