// Compare two Animals for equality; used in Recipe 14.9 // (for completeness, you should also define operator!=) inline bool operator==(const Animal& lhs, const Animal& rhs) { return lhs.name( ) == rhs.name( ) && lhs.species( ) == rhs.species( ) && lhs.dateOfBirth( ) == rhs.dateOfBirth( ) && lhs.veterinarian( ) == rhs.veterinarian( ) && lhs.trainer( ) == rhs.trainer( ); }
int main(){ //To demonstrate polymorphism I'll create a pointer of type Animal and use it to access the subclass animals Animal *p = new Bird(); p->name(); p->does(); Animal *t = new Dog(); t->name(); t->does(); delete p; delete t; //This demonstrates overriding of the pure virtual functions in the abstract class Animal system("PAUSE"); return 0; }
int main() { Animal *p = new Bird; Animal *p1 = new Dog; p->name(); p->does(); p1->name(); p1->does(); system("pause"); }
int main() { Animal *animal = new Animal; Parrot *parrot = new Parrot; animal->name((char *)"wolf"); parrot->name((char *)"crunk"); animal->say(); parrot->say(); parrot->learn((char *)"puppa"); parrot->replay(); }
void GenioState::handleAnswer() { string input; if (gameContext->hasNext()) { Animal* animal = gameContext->next(); cout << "O animal que voce pensou " << animal->skill() << endl; cin >> input; if (input == string("sim")) { cout << "O animal que voce pensou e o " << animal->name() << "?" << endl; cin >> input; if (input == string("sim")) { gameContext->setState(gameContext->getWinnerState()); } }
int main() { Animal* sheep = new Sheep; Animal* cow = new Cow; cout << "The " << sheep->name() << " says: " << sheep->speak() << endl; cout << "The " << cow->name() << " says: " << cow->speak() << endl; *sheep = *cow; // slicing! cout << "The " << sheep->name() << " says: " << sheep->speak() << endl; cout << "The " << cow->name() << " says: " << cow->speak() << endl; cout << "You can make the sheep speak like a cow, but it will always be a sheep at heart." << endl; cout << "In other words: you can copy/move base class values without \"slicing\" derived data," << endl; cout << "but you cannot avoid slicing the vtable." << endl; delete sheep; delete cow; return 0; }