Introduction to OOP Chapter 11: Static and Dynamic Behavior: next previous audio real text

Documenting Method Binding

In many languages dynamic binding is the default. If a child class overrides a method in the parent, using the same type signature, then the selected method will be determined by the dynamic type.

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 !

Intro OOP, Chapter 11, Slide 12