Polymorphism & Virtual Methods, Lecture notes of Object Oriented Programming

Polymorphism in C++. ○ Polymorphism in C++ is supported through: - virtual methods AND. - pointers to objects OR reference variables/ parameters.

Typology: Lecture notes

2022/2023

Uploaded on 02/28/2023

ekaksha
ekaksha 🇺🇸

4.4

(30)

268 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Polymorphism & Virtual Methods
Week 6
Gaddis:15.6-15.8
CS 5301
Fall 2013
Jill Seaman
2
Polymorphism
!The Greek word poly means many, and the
Greek word morphism means form.
!So, polymorphism means 'many forms'.
!In object-oriented programming (OOP),
polymorphism refers to
-identically named (and redefined) methods
-that have different behavior depending on the
(specific derived) type of object that they are called
on.
3
Example of polymorphism?
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
Example of polymorphism?, part 2
void f (Animal a) {
a.speak();
}
int main() {
Cat c;
Dog d;
f(c);
f(d);
}
string name1 = “Steve Jobs”;
cout << “Name” << name1 << endl;
!IF the output is “meow bark”, this (function f)
is an example of polymorphism.
-The behavior of a in f would depend on its
specific (derived type).
!IF the output is “none none”, it’s not
polymorphism.
pf3
pf4

Partial preview of the text

Download Polymorphism & Virtual Methods and more Lecture notes Object Oriented Programming in PDF only on Docsity!

1

Polymorphism & Virtual Methods

Week 6

Gaddis:15.6-15.

CS 5301

Fall 2013

Jill Seaman

2

Polymorphism

! The Greek word poly means many, and the

Greek word morphism means form.

! So, polymorphism means 'many forms'.

! In object-oriented programming (OOP),

polymorphism refers to

- identically named (and redefined) methods

- that have different behavior depending on the

(specific derived) type of object that they are called

on.

3

Example of polymorphism?

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

Example of polymorphism?, part 2

void f (Animal a) { a.speak(); } int main() { Cat c; Dog d; f(c); f(d); } string name1 = “Steve Jobs”; cout << “Name” << name1 << endl;

! IF the output is “meow bark”, this (function f)

is an example of polymorphism.

- The behavior of a in f would depend on its

specific (derived type).

! IF the output is “none none”, it’s not

polymorphism.

5

Polymorphism in C++

! Polymorphism in C++ is supported through:

- virtual methods AND

- pointers to objects OR reference variables/

parameters.

! without these, C++ determines which method to

invoke at compile time (using the variable type).

! when virtual methods and pointer/references are

used together, C++ determines which method to

invoke at run time (using the specific type of the

instance currently referenced by the variable).

cout << “Name” << name1 << endl; 6

Virtual methods

! Virtual member function: function in a base class

that expects to be redefined in derived class

! Function defined with key word virtual:

! Supports dynamic binding: functions bound at

run time to function that they call

! Without virtual member functions, C++ uses

static (compile time) binding

cout << “Name” << name1 << endl; virtual^ void^ Y()^ {...} 7

Example virtual methods

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

Virtual methods

! In compile-time binding, the data type of the

pointer resolves which method is invoked.

! In run-time binding, the type of the object

pointed to resolves which method is invoked.

string name1 = “Steve Jobs”; cout << “Name” << name1 << endl; void f (Animal &a) { a.speak(); } int main() { Cat c; Dog d; f(c); f(d); }

! Assuming speak is virtual,

and a is passed by

reference, the output is:

meow bark

13

Example: Abstract Class

! An abstract class may not be used as an

argument type, as a function return type,or as

the type of an explicit conversion.

! Pointers and references to an abstract class

may be declared.

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

Example: Abstract Class

! Pure virtual functions are inherited as pure

virtual functions.

! Or else:

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

Heterogeneous collections

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

Heterogeneous collections

! Driver:

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