1.

Solve : Help with Objects and Inheritance?

Answer»

I have an object CALLED Comparable as my Base Class. Then I have two derived classes from this class called, CmpInt and CmpString.

In CmpInt (short for Comparable Integer) there is a method that GETS the classes integer value called value. The method is called geti().

In CmpString (short for Comparable String) there is a method that gets the classes string value called value. the method name is gets().

I want to create a loop that runs through an array of Comparable Objects and grab this data (value).

How do I check what functions it has? I want to do something like:

Code: [Select]for(int i=1; i<=sz; i++)
{
//I do not know how to write this object checking
if (((CmpInt *)A[i]))//If it is a CmpInt callgeti
{
COUT<<"Object is CmpInt use geti()"<<endl;
cout << ((CmpInt *)A[i])->geti() <<endl;
}
else //It is a CmpString call method gets()
{
cout<<"It not a CmpInt, it needs to call gets()"<<endl;
cout << ((CmpString *)A[i])->gets() <<endl;
}

I need to know what it is to call the right method. How do I do this?



Discussion

No Comment Found