introduction to polymorphism in object-oriented programmingcbright/talks/polymorphism.pdf ·...

49
Introduction to Polymorphism in Object-Oriented Programming University of Windsor March 12, 2020 Live Polling: pollev.com/curtisbright681 1/37

Upload: others

Post on 17-Jul-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Introduction to Polymorphism inObject-Oriented Programming

University of Windsor

March 12, 2020

Live Polling: pollev.com/curtisbright681

1/37

Page 2: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Roadmap

In this lecture we’ll design a simple object-oriented toolkit fordisplaying graphics in a terminal. Along the way we will cover:

I EncapsulationI Classes, Variables, Methods, Information hiding

I InheritanceI Superclasses, Subclasses, Substitution principle

I PolymorphismI Method overloading, Method overriding, Dynamic binding

2/37

Page 3: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Encapsulation

encapsulate (verb):to enclose in or as if in a capsule

3/37

Page 4: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Encapsulation

encapsulate (verb):to enclose in or as if in a capsule

3/37

Page 5: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Classes

There are different types or classes of objects. For example, “shape”,“triangle”, “box”, and “text box” are possible classes.

Hello!

In C++, a class may be defined using the class keyword:

class Shape{};

4/37

Page 6: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Classes

There are different types or classes of objects. For example, “shape”,“triangle”, “box”, and “text box” are possible classes.

Hello!

In C++, a class may be defined using the class keyword:

class Shape{};

4/37

Page 7: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Variables

Some properties of objects differ between two objects of the sameclass. For example, the heights and widths of a shape. These arevariables of the shape.

class Shape{

int height, width;};

5/37

Page 8: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Variables

Some properties of objects differ between two objects of the sameclass. For example, the heights and widths of a shape. These arevariables of the shape.

class Shape{

int height, width;};

5/37

Page 9: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Methods

Objects may also perform actions—such as updating their variables.

class Shape{

int height, width;// Initialize height and widthvoid init(int h, int w){

height = h;width = w;

}};

6/37

Page 10: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Methods

Objects may also perform actions—such as updating their variables.

class Shape{

int height, width;// Initialize height and widthvoid init(int h, int w){

height = h;width = w;

}};

6/37

Page 11: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Information hiding

By default an object’s variables and methods can only be accessedfrom within the object’s methods.

Using the public keyword you can make variables and methodsaccessible from outside the object.

7/37

Page 12: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Example

class Shape{

int height, width;public:

void init(int h, int w){

height = h;width = w;

}};

8/37

Page 13: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Consider the following Account class:

class Account{

int balance; // Balance of accountpublic:

int getBalance(); // Return balance of accountvoid deposit(int amount); // Add to balance

}

Assuming that A is of type Account, which of the following is acorrect way of updating A?A. balance += 5;

B. A.balance += 5;

C. deposit(5);D. A.deposit(5);E. A.getBalance() += 5;

9/37

Page 14: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Inheritance

inheritance (noun):a physical or mental characteristic inherited from yourparents, or the process by which this happens

10/37

Page 15: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Inheritance

inheritance (noun):a physical or mental characteristic inherited from yourparents, or the process by which this happens

10/37

Page 16: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Class hierarchy

Certain classes like shapes can be organized into a hierarchy:

Shape

Triangle Box

Text Box

11/37

Page 17: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Superclasses and subclasses

A more generic class is called a superclass, while a more specializedclass is called a subclass.

Shape

Triangle Box

Superclass

Subclasses

Example: Triangle and Box are subclasses of Shape.

12/37

Page 18: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

What is the most natural relationship between the classes Account,ChequingAccount, and SavingsAccount?

A. Account and ChequingAccount are superclasses ofSavingsAccount.

B. Account and ChequingAccount are subclasses ofSavingsAccount.

C. SavingsAccount and ChequingAccount are superclasses ofAccount.

D. SavingsAccount and ChequingAccount are subclasses ofAccount.

13/37

Page 19: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Defining subclasses

To define a new subclass of a given class, the colon operator is used:

class Triangle : public Shape{};

class Box : public Shape{};

14/37

Page 20: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Inheritance

The methods of a superclass are automatically inherited by any ofits subclasses.

In other words, a subclass has the variables and methods of thesuperclass it was derived from—but the reverse is not true.

15/37

Page 21: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Inheritance

Caveat: By default, variables and methods of a superclass are notvisible to subclasses. They can be made visible (only to subclasses)by using the protected keyword.

class Shape{protected:

int height, width;public:

...};

16/37

Page 22: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Inheritance

Caveat: By default, variables and methods of a superclass are notvisible to subclasses. They can be made visible (only to subclasses)by using the protected keyword.

class Shape{protected:

int height, width;public:

...};

16/37

Page 23: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Substitution principle

Since a subclass is a special case of a superclass, you can always usea subclass to stand in for a superclass if necessary.

17/37

Page 24: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

In this class hierarchy which classes could not stand in for a Dog?

Animal

Cat

Calico

Dog

Poodle

A. AnimalB. Cat, CalicoC. Animal, Cat, CalicoD. Cat, Calico, PoodleE. Animal, Cat, Calico, Poodle

18/37

Page 25: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Example

class Box : public Shape{public:

// Draw visual depiction of the box to the standard outputvoid draw(){

for(int i=0; i<height; i++)cout << string(width, '*') << endl;

}};

class Triangle : public Shape{public:

// Draw visual depiction of the triangle to the standard outputvoid draw(){

for(int i=1; i<=height; i++)cout << string(i*width/height, '*') << endl;

}};

19/37

Page 26: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Example

int main(){

Triangle t;t.init(5,5);t.draw();

Box b;b.init(10,10);b.draw();

}

20/37

Page 27: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

If b is a Box and s is a Shape which line has a problem?

A. Box b2 = b;

B. Shape s2 = s;

C. Box b2 = s;

D. Shape s2 = b;

21/37

Page 28: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Polymorphism

22/37

Page 29: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Polymorphism

polymorphism (noun):the condition of occurring in several different forms

23/37

Page 30: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Polymorphism

polymorphism (noun):the condition of occurring in several different forms

23/37

Page 31: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overloading

Two methods of a class can share the same name, so long as thenumber of parameters or parameter types are different.

24/37

Page 32: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overloading

class Shape{

...void init(int h, int w){

height = h;width = w;

}void init(int hw){

height = hw;width = hw;

}}

int main(){

Box b;b.init(10);b.draw();b.init(5,5);b.draw();

}

25/37

Page 33: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overloading

class Shape{

...void init(int h, int w){

height = h;width = w;

}void init(int hw){

height = hw;width = hw;

}}

int main(){

Box b;b.init(10);b.draw();b.init(5,5);b.draw();

}

25/37

Page 34: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Consider the following code:

string mystery(int a) { return "A"; }string mystery(string a) { return "B"; }string mystery(string a, int b) { return "C"; }string mystery(int a, string b) { return "D"; }

What does mystery(1, "A") return?

A. "A"B. "B"C. "C"D. "D"

26/37

Page 35: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overriding

A subclass can override a method of a superclass.

The new method has the same name and parameters as theoverridden method but can have a different implementation.

27/37

Page 36: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overriding

class TextBox : public Box{

string text;public:

void setText(string s){

text = s;}void draw() // Overridden{

for(int i=0; i<height; i++){ if(i==height/2)

cout << text << endl;else

cout << string(width, '*') << endl;}

}};

28/37

Page 37: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overriding

class Box : public Shape{

...void draw(){ ... }

};

class TextBox : public Box{

...void draw(){ ... }

};

int main(){

Box b;b.init(5);b.draw();TextBox tb;tb.init(5);tb.setText("Hello");tb.draw();

}

29/37

Page 38: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Method overriding

class Box : public Shape{

...void draw(){ ... }

};

class TextBox : public Box{

...void draw(){ ... }

};

int main(){

Box b;b.init(5);b.draw();TextBox tb;tb.init(5);tb.setText("Hello");tb.draw();

}

29/37

Page 39: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Consider the following:

class Shape { int height, width; };class Box : public Shape { void draw() { ... } };class Triangle: public Shape { void draw() { ... } };

Method overriding is used in these simplified class definitions:A. TrueB. False

30/37

Page 40: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Consider the following:

class Box : public Shape{public:

void init(int hw) { ... }};

class TextBox: public Shape{public:

void init(int hw, string s) { ... }};

Method overriding is used in these simplified class definitions:A. TrueB. False

31/37

Page 41: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Dynamic binding

By default, C++ will determine which method implementation isused at compile time based on the object’s type.

However, a more specific choice could be made at run time becausemore information is known.

32/37

Page 42: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Dynamic binding

Declaring a method as virtual tells the compiler to use “dynamicbinding” (on the method in this class and any of its subclasses) andmake the choice at run time.

class Box : public Shape { ... virtual void draw() { ... } };class TextBox : public Box { ... void draw() { ... } };

void DrawBox(Box& b){

b.draw();}

Which draw method will be called in DrawBox?

Depends on the type of object passed to DrawBox!

with Box reference

with TextBox reference

33/37

Page 43: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Dynamic binding

Declaring a method as virtual tells the compiler to use “dynamicbinding” (on the method in this class and any of its subclasses) andmake the choice at run time.

class Box : public Shape { ... virtual void draw() { ... } };class TextBox : public Box { ... void draw() { ... } };

void DrawBox(Box& b){

b.draw();}

Which draw method will be called in DrawBox?

Depends on the type of object passed to DrawBox!

with Box reference

with TextBox reference

33/37

Page 44: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Dynamic binding

int main(){

Box b;b.init(5);DrawBox(b);

}

DrawBox will call the drawmethod of Box.

int main(){

TextBox tb;tb.init(5);tb.setText("Hello");DrawBox(tb);

}

DrawBox will call the draw methodof TextBox.

34/37

Page 45: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Consider the following:

class Shape { int height, width; }class Box : public Shape { ... virtual void draw() { ... } };class TextBox : public Box { ... void draw() { ... } };void DrawBox(Box& b) { b.draw(); }Shape s;TextBox tb;

What will be the result of running DrawBox(s) and, separately,DrawBox(tb)?

A. Box’s draw run in both cases.B. Box’s draw run in first case, TextBox’s draw run in second

case.C. TextBox’s draw run in both cases.D. Error in first case, TextBox’s draw in second case.E. Error in first case, Box’s draw in second case.

35/37

Page 46: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Consider the following:

class Shape { int height, width; }class Box : public Shape { ... virtual void draw() { ... } };class TextBox : public Box { ... void draw() { ... } };void DrawShape(Shape& s) { s.draw(); }

int main(){

Shape s;DrawShape(s);

}

What kind of error arises in this program?

A. Compile time errorB. Run time error

36/37

Page 47: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Summary

We discussed three kinds of polymorphism:

1. Method overloading:Methods/functions that have the same name but differentparameters.

2. Method overriding:Methods that have the same name and same parameters, butbelong to a superclass and subclass.

3. Dynamic binding:Method overriding of a function that has been declaredvirtual.

37/37

Page 48: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Summary

We discussed three kinds of polymorphism:

1. Method overloading:Methods/functions that have the same name but differentparameters.

2. Method overriding:Methods that have the same name and same parameters, butbelong to a superclass and subclass.

3. Dynamic binding:Method overriding of a function that has been declaredvirtual.

37/37

Page 49: Introduction to Polymorphism in Object-Oriented Programmingcbright/talks/polymorphism.pdf · 2020-03-12 · Introduction to Polymorphism in Object-Oriented Programming Created Date:

Summary

We discussed three kinds of polymorphism:

1. Method overloading:Methods/functions that have the same name but differentparameters.

2. Method overriding:Methods that have the same name and same parameters, butbelong to a superclass and subclass.

3. Dynamic binding:Method overriding of a function that has been declaredvirtual.

37/37