| | |
C++ instanceof
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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
From Company.h
From payroll.cpp (The client)
Full listing
http:// rapidshare.com/files/215333186/payRoll.rar.html
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++;
}
•
•
•
•
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);
};
•
•
•
•
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
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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.
Klatu Barada Nikto
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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.
So somehow I must figure out the type of the object.
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.
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.
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
>>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:
Simple:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; class Base { public: Base() {x = 0;} virtual void Display()= 0; protected: int x; }; class Derived1 : public Base { public: Derived1() { x = 1;} virtual void Display() { cout << "This is Derived1\n";} }; class Derived2 : public Base { public: Derived2() {x = 2;} virtual void Display() { cout << "This is Derived2\n";} }; int main () { vector<Base*> theList; Derived1 * pD1 = new Derived1; theList.push_back(pD1); Derived2 * pD2 = new Derived2; theList.push_back(pD2); vector<Base*>::iterator it; for( it = theList.begin(); it != theList.end(); it++) (*it)->Display(); }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
No compiler to prove this works, but idea behind should be valid. 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!
C++ Syntax (Toggle Plain Text)
class staffmember //declared as abstract base class { private: int ID; string name; char type; double wage; public: virtual getID() = 0; virtual string getName() = 0; virtual char getType() = 0; virtual double getWage() = 0; }; class Manager : public staffmember { public: Manager(){type = 'M';} //mutator functions here, left out of this example //the following accessor functions are all overridden virtual functions int getID() {return ID;} string getName() {return name;} char getType() {return type;} double getWage() {return wage;} }; //similar for Casual class here //declare vector of staffmember pointers vector<staffmember *> employees; //declare a Manager object Manager m; //fill variables of m as needed here //add m to employees employees.push_back(&m); //declare a Casual object Casual c; //fill variables of c as needed //add c to employees; employees.push_back(&c); //display employees int len = employees.size(); for(int i = 0; i < len; ++i) { cout << "employee with index " << i << ": " << endl; employees[i]->getID(); cout << endl; employees[i]->getName(); cout << endl; employees[i]->getType(); cout << endl; employees[i]->getwage(); cout << endl << endl; }
Klatu Barada Nikto
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- PHP version of Java's instanceof (PHP)
- problem in java cobe (Java)
- Documents (Java)
- Saving and opeing a JTable (Java)
- JMF question: question about the unrealized player (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
- Help with casting (Java)
Other Threads in the C++ Forum
- Previous Thread: CInternetSession API -- How to pass authentication to C# page
- Next Thread: c++ 2 dimentional array- airplane seating
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates text text-file tree url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






