I am building a program requiring making some classes with inheritance. I can't seem to be able to access the members of the class objects. My code looks like this:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
class Animal{
public:
int number;
int legs;
Animal(){
number = rand()%9;
}
};
class Fish : public Animal{
public:
Fish () : Animal (){
legs = 0;
}
};
class Bird : public Animal{
public:
Bird () : Animal (){
legs = 2;
}
};
class Insect : public Animal{
public:
Insect () : Animal (){
legs = 6;
}
};
int main() {
Animal *a, *b, *x;
a = new Fish;
b = new Bird;
x = new Insect;
cout<< a.number << "\t" << a.legs << endl;
cout<< b.number << "\t" << b.legs << endl;
cout<< x.number << "\t" << x.legs << endl;
system("pause");
return 0;
}
Basically, all animals have a random number, and each animal type has a different number of legs. When running, I get an error saying that 'number' and 'legs' are not declared. Help?