Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- class Animal{
- public:
- void getFamily() { cout << "We are animals" << endl; }
- virtual void getClass() { cout << "I'm an Animal" << endl;} //Permits polymorphism
- };
- class Dog : public Animal{
- public:
- getClass() {cout << "I'm a dog" << endl;}
- };
- class GermanShepard : public Dog{
- public:
- void getClass() { cout << "Im a german Shepard" << endl; }
- void getDerived() { cout << "Im derived dog" << endl;}
- }
- void whatClassAreYou(Animal *animal){
- animal.getClass();
- /*THIS CLASS DO THE SAMETHING FROM THIS REFS INSIDE MAIN()
- Animal *animal = new Animal;
- Dog *dog = new Dog;
- animal->getClass();
- dog.->getClass();
- */
- }
- int main(){
- Animal *animal = new Animal;
- Dog *dog = new Dog;
- animal->getClass();
- dog.->getClass();
- //BOTH GONNA PRINT ANIMAL CLASS UNLESS THE ANIMAL CLASS METHOD getClass() is VIRTUAL!!!
- whatClassAreYou(animal);
- whatClassAreYou(dog);
- //BOTH GONNA PRINT ANIMAL CLASS UNLESS THE ANIMAL CLASS METHOD getClass() is VIRTUAL!!!
- Dog spot; //Dog object
- GermanShepard max; //GermanShepard object
- Animal* ptrDog = &spot;
- Animal* ptrGShepard = &max;
- ptrDog -> getFamily();
- ptrDog -> getClass();
- ptrGShepard -> getFamily();
- ptrGShepard -> getClass();
- return 0;
- }
Add Comment
Please, Sign In to add comment