Please bear with me, this is my first post
So, I have this superclass called A.
And I've 2 subclasses A1 and A2.
A has attribute name.
A has function toString().
A1 inherits name, and has attribute hair.
A1 has a method toString() which goes:
string A1::toString(){
string stringA= A::toString();
stringstream info;
info<<stringA<<" "<<hair;
return info.str();
}
A2 is the same as A1, except that it has face instead of hair.
it's toString goes info<<stringA<<" "<<face;
In my main(),
vector<A*> vector;
Then, I have a function to add objects
void addA1 (vector<A*> &vector)
void addA2 (vector<A*> &vector)
both use push_back, and after testing, they work fine.
So here's the question:
I need to make another function called display().
but the tricky part is that I need to check the items in the vector, and use the correct toString() functions. Like if the first object in the vector is A1, then i'm supposed to call the toString() from A1, and if the second is A2, I call the toString() from A2.
Like so:
void display(vector<A*> &vector){
for(int c=0;c<vector.size();c++){ //traverse through vector
//If A1, use A1's toString() and display
//Else if A2, use A2's toString() and display
}
}
I was thinking it should be something like if(vector[c]==A1) or something but it gave me errors.
I'm stuck at this point.
Any help is much appreciated!