| Introduction to OOP | Chapter 11: Static and Dynamic Behavior: | next | previous | audio | real | text |
In other languages (C++, Delphi, C#) the programmer must indicate which methods are dynamically bound and which are statically type. In C#, for example, this is done using the virtual keyword.
class Animal {
public:
virtual void speak () { cout << "Animal Speak !\n"; }
void reply () { cout << "Animal Reply !\n"; }
};
class Dog : Animal {
public:
override void speak () { cout << "woof !\n"; }
void reply () { cout << "woof again!\n"; }
};
class Bird : Animal {
public:
virtual void speak () { cout << "tweet !\n"; }
};
| Animal a; Dog b; b.speak(); woof ! a = b; a.speak(); woof ! Bird c; c.speak(); tweet ! a = c; a.speak(); tweet ! |