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; }
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; }
void act(Animal& a){ a.act(); }