int main() { Animal *animal = new Animal; Dog *dog = new Dog; GermanShepard *germanShepard= new GermanShepard; Cat *cat = new Cat; animal->getClass(); dog->getClass(); germanShepard->getClass(); cat->getClass(); whatClassAreYou(animal); whatClassAreYou(dog); whatClassAreYou(germanShepard); whatClassAreYou(cat); Dog spot; GermanShepard max; Cat mini; Animal* ptrDog = &spot; Animal* ptrGShepart = &max; Animal* ptrCat = &mini; ptrDog->getFamily(); ptrDog->getClass(); ptrGShepart->getFamily(); ptrGShepart->getClass(); ptrGShepart->makeSound(); ptrCat->getFamily(); ptrCat->getClass(); ptrCat->makeSound(); Animal* pCat = new Cat; Animal* pDog = new Dog; pCat->makeSound(); pDog->makeSound(); Car* stationWagon = new StationWagon(); stationWagon->getNumWheels(); cin.get(); return 0; }
int main(){ Animal *animal = new Animal; Dog *dog = new Dog; // If a method is marked virtual or not doesn't matter if we call the method // directly from the object animal->getClass(); dog->getClass(); // If getClass is not marked as virtual outside functions won't look for // overwritten methods in subclasses however whatClassAreYou(animal); whatClassAreYou(dog); Dog spot; GermanShepard max; // A base class can call derived class methods as long as they exist // in the base class Animal* ptrDog = &spot; Animal* ptrGShepard = &max; // Call the method not overwritten in the super class Animal ptrDog->getFamily(); // Since getClass was overwritten in Dog call the Dog version ptrDog->getClass(); // Call to the super class ptrGShepard->getFamily(); // Call to the overwritten GermanShepard version ptrGShepard->getClass(); return 0; }