943,879 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4078
  • C++ RSS
Mar 30th, 2009
0

C++ instanceof

Expand Post »
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
Quote ...
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
Quote ...
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)
Quote ...
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnGuRuSO is offline Offline
11 posts
since Oct 2008
Mar 30th, 2009
1

Re: C++ instanceof

Since you are storing pointer to type objects why not try using polymorhism by writing a base method called displayPay() declared with the keyword virtual that would be overloaded with in each of the inherited classes. Then you can loop through the vector of pointer to StaffMembers calling the displayPay() method and the correct version of displayPay() will be called for each referenced object.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 30th, 2009
0

Re: C++ instanceof

Here Here!
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 30th, 2009
0

Re: C++ instanceof

Click to Expand / Collapse  Quote originally posted by Lerner ...
Since you are storing pointer to type objects why not try using polymorhism by writing a base method called displayPay() declared with the keyword virtual that would be overloaded with in each of the inherited classes. Then you can loop through the vector of pointer to StaffMembers calling the displayPay() method and the correct version of displayPay() will be called for each referenced object.
I have actually done that, although instead of displayPay() I used wage(). One of the requirements is to output the ID, Name, Type e.g. Casual or Manager and Wage of each staff member.

So somehow I must figure out the type of the object.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnGuRuSO is offline Offline
11 posts
since Oct 2008
Mar 30th, 2009
0

Re: C++ instanceof

Nah, in the overridden virtual wage() method, simply put something like cout << "Manager" in the Manager's method, and cout << "Casual" in the Casual's method.

Or make a virtual overridden method for each class that is like "GetType()", and have wage() call GetType(). The proper way to do what you want to do, is to use polymorphism... regardless of how you choose to get the type.
Last edited by Comatose; Mar 30th, 2009 at 2:20 pm.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 30th, 2009
0

Re: C++ instanceof

I think the problem is I am adding it to a Vector of StaffMember objects. Staff Member is the base class, so would case the derived classes into StaffMember objects?

I took your advice and wrote a GetType() method.
Casual objects return the char 'c'
Manager objects return the char 'm'
StaffMember objects return the char 's'

After adding Casual/Manager objects to the Vector, and then calling the "GetType()" method they all return 's'. So I think they are being type casted.

So maybe the question now is, how do I prevent this type casting from happening.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnGuRuSO is offline Offline
11 posts
since Oct 2008
Mar 30th, 2009
0

Re: C++ instanceof

The vector manipulates the objects as StaffMembers, but in spite of that, they are still objects of the class you created them as, and GetType() should work fine.
If GetType() always returns 's', I wonder if you've forgotten to make it public virtual in the base class?
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Mar 30th, 2009
0

Re: C++ instanceof

>>I think the problem is I am adding it to a Vector of StaffMember objects. Staff Member is the base class, so would case the derived classes into StaffMember objects?

Simple:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Base
  6. {
  7. public:
  8. Base() {x = 0;}
  9. virtual void Display()= 0;
  10. protected:
  11. int x;
  12. };
  13.  
  14. class Derived1 : public Base
  15. {
  16. public:
  17. Derived1() { x = 1;}
  18. virtual void Display() { cout << "This is Derived1\n";}
  19. };
  20.  
  21. class Derived2 : public Base
  22. {
  23. public:
  24. Derived2() {x = 2;}
  25. virtual void Display() { cout << "This is Derived2\n";}
  26. };
  27.  
  28. int main () {
  29. vector<Base*> theList;
  30. Derived1 * pD1 = new Derived1;
  31. theList.push_back(pD1);
  32. Derived2 * pD2 = new Derived2;
  33. theList.push_back(pD2);
  34.  
  35. vector<Base*>::iterator it;
  36. for( it = theList.begin(); it != theList.end(); it++)
  37. (*it)->Display();
  38.  
  39. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,952 posts
since Aug 2005
Mar 30th, 2009
0

Re: C++ instanceof

No compiler to prove this works, but idea behind should be valid.
C++ Syntax (Toggle Plain Text)
  1. class staffmember //declared as abstract base class
  2. {
  3. private:
  4. int ID;
  5. string name;
  6. char type;
  7. double wage;
  8.  
  9. public:
  10. virtual getID() = 0;
  11. virtual string getName() = 0;
  12. virtual char getType() = 0;
  13. virtual double getWage() = 0;
  14. };
  15.  
  16. class Manager : public staffmember
  17. {
  18. public:
  19. Manager(){type = 'M';}
  20. //mutator functions here, left out of this example
  21.  
  22. //the following accessor functions are all overridden virtual functions
  23. int getID() {return ID;}
  24. string getName() {return name;}
  25. char getType() {return type;}
  26. double getWage() {return wage;}
  27. };
  28.  
  29. //similar for Casual class here
  30.  
  31. //declare vector of staffmember pointers
  32. vector<staffmember *> employees;
  33.  
  34. //declare a Manager object
  35. Manager m;
  36.  
  37. //fill variables of m as needed here
  38. //add m to employees
  39. employees.push_back(&m);
  40.  
  41. //declare a Casual object
  42. Casual c;
  43. //fill variables of c as needed
  44. //add c to employees;
  45. employees.push_back(&c);
  46.  
  47. //display employees
  48. int len = employees.size();
  49. for(int i = 0; i < len; ++i)
  50. {
  51. cout << "employee with index " << i << ": " << endl;
  52. employees[i]->getID();
  53. cout << endl;
  54. employees[i]->getName();
  55. cout << endl;
  56. employees[i]->getType();
  57. cout << endl;
  58. employees[i]->getwage();
  59. cout << endl << endl;
  60. }
Ancient Dragons example is more succinct, and use of iterators is classy, but I don't like the (*it)-> syntax so I used the []. Oh, and he can type faster than I can!
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 31st, 2009
0

Re: C++ instanceof

Thanks for posting up the code Ancient Dragon and Lerner. I had something very similar to what you two have written.

MrSpigot was right, I didn't set my getType() function to virtual and as a result every Casual/Manager object returned 's' instead of 'm'/'c'.

So there we go, everything is working now and I thank you.

Angus
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnGuRuSO is offline Offline
11 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: CInternetSession API -- How to pass authentication to C# page
Next Thread in C++ Forum Timeline: c++ 2 dimentional array- airplane seating





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC