


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Polymorphism in C++. ○ Polymorphism in C++ is supported through: - virtual methods AND. - pointers to objects OR reference variables/ parameters.
Typology: Lecture notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1
2
3
class Animal { private: ... public: void speak() { cout << “none ”; } }; class Cat : public Animal { private: ... public: void speak() { cout << “meow “; } }; class Dog : public Animal { private: ... public: void speak() { cout << “bark “; } }; string name1 = “Steve Jobs”; cout << “Name” << name1 << endl; 4
void f (Animal a) { a.speak(); } int main() { Cat c; Dog d; f(c); f(d); } string name1 = “Steve Jobs”; cout << “Name” << name1 << endl;
5
cout << “Name” << name1 << endl; 6
cout << “Name” << name1 << endl; virtual^ void^ Y()^ {...} 7
string name1 = “Steve Jobs”; cout << “Name” << name1 << endl; class Animal { public: virtual void speak(); int age(); }; class Cat : public Animal { public: virtual void speak(); //redefining a virtual int age(); //redefining a normal function }; int main() { Cat morris; Animal *pA = &morris; pA -> age(); // Animal::age() is invoked (base) (not virtual) pA -> speak(); // Cat::speak() is invoked (derived) ... } 8
string name1 = “Steve Jobs”; cout << “Name” << name1 << endl; void f (Animal &a) { a.speak(); } int main() { Cat c; Dog d; f(c); f(d); }
meow bark
13
cout << “Name” << name1 << endl; class CShape { public: CShape ( ) { } virtual void vDraw ( ) const = 0; // pure virtual method }; CShape CShape1; // Error: object of abstract class CShape* pCShape; // Ok CShape CShapeFun(); // Error: return type void vg(CShape); // Error: argument type CShape& rCShapeFun(CShape&); // Ok 14
cout << “Name” << name1 << endl; class CAbstractCircle : public CShape { private: int m_iRadius; public: void vRotate (int) {} // CAbstractCircle ::vDraw() is a pure virtual function }; class CCircle : public CShape { private: int m_iRadius; public: void vRotate (int) {} void vDraw(); //define here or in impl file }; 15
string name1 = “Steve Jobs”; cout << “Name” << name1 << endl; class Animal { private: string name; public: Animal(string n) {name = n;} virtual void speak() = 0; }; class Cat : public Animal { public: Cat(string n) : Animal(n) { }; void speak() {cout << "meow "; } }; class Dog : public Animal { public: Dog(string n) : Animal(n) { }; void speak() {cout << "bark "; } }; class Pig : public Animal { public: Pig(string n) : Animal(n) { }; void speak() {cout << "oink "; } }; 16
string name1 = “Steve Jobs”; cout << “Name” << name1 << endl; int main() { Animal* animals[ ] = { new Cat("Charlie"), new Cat("Scamp"), new Dog("Penny"), new Cat("Libby"), new Cat("Patches"), new Dog("Milo"), new Pig("Wilbur") }; for (int i=0; i< 7; i++) { animals[i]->speak(); } } meow meow bark meow meow bark oink