int main(){ Animal A = "Buffy"; // Animal A("Buffy"); int i(5); A.act(); A.move(); A.sound(); cout << "------------End of main" << endl; return 0; }
int main(){ Cat C("TOM"); Animal* ap = new Cat("Fluffy"); cout << "------------Using Animal pointer: " << endl; ap->act(); ap->move(); ap->sound(); cout << "------------End of main" << endl; delete ap; act(C); return 0; }
void Game::handleMovement(Weaponry& grenade, Animal& user) { if (ev.type == SDL_KEYDOWN) { switch(ev.key.keysym.sym) { case SDLK_LEFT: { // Spelaren ska inte kunna röra under avfyrning if (!grenade.isActivated()) { user.setDir(LEFT); user.move(); } break; } case SDLK_RIGHT: { if (!grenade.isActivated()) { user.setDir(RIGHT); user.move(); } break; } default: { break; } } } // När vi släpper mellanslagstangenten ska skottet avfyras else if (ev.type == SDL_KEYUP && grenade.isActivated()) { grenade.activated(false); grenade.getsThrown(true, user); grenade.setSpeed(SHEEP); } }
void AI::estimateAndMove(Animal& ai) { // Välj ut en lämplig position att röra sig till // (en gång per tur) if (!move_choices_made) { desired_direction = rand() % 2 ; // left/right ai.setDir(desired_direction); desired_steps = rand() % 5; move_choices_made = true; steps = 0; } // Fortsätt tills vi kommit till målet if ( steps < desired_steps ) { ++steps; ai.move(); } }
int main(){ Cat* c = new Cat("Fluffy"); Animal* ap = new Cat("Tom"); Animal& ar = *ap; cout << "------------Using Animal pointer: " << endl; c->act(); c->move(); c->sound(); cout << "------------Using Animal reference: " << endl; ar.act(); ar.move(); ar.sound(); cout << "------------Using Animal pointer: " << endl; ap->act(); ap->move(); ap->sound(); delete c; delete ap; // only the animal is deleted, oops!!! cout << "------------End of main" << endl; return 0; }
int main(){ int* p = new int(3); Cat* c = new Cat("Fluffy"); Animal* ap = new Cat("Tom"); Animal& ar = *ap; cout << "------------Using Animal pointer: " << endl; c->act(); c->move(); c->sound(); cout << "------------Using Animal reference: " << endl; ar.act(); ar.move(); ar.sound(); cout << "------------Using Animal pointer: " << endl; ap->act(); ap->move(); ap->sound(); delete c; delete ap; ! cout << "------------End of main" << endl; return 0; }
int main(){ Animal A; // will cause error because of abstract base class Cat C("Fluffy"); Animal& ar = C; Animal* ap = &C; /* cout << "------------No Cat, jut Animal reference: " << endl; A.act(); A.move(); A.sound(); will cause error because of abstract base class */ cout << "------------Using Cat reference: " << endl; C.act(); C.move(); C.sound(); cout << "------------Using Animal reference: " << endl; ar.act(); ar.move(); ar.sound(); cout << "------------Using Animal pointer: " << endl; ap->act(); ap->move(); ap->sound(); cout << "------------End of main" << endl; return 0; }
int main(){ Animal a; a.move(); a.climb(); return 0; }