41
Inheritance: Polymorphism and Virtual Functions Lecture 22 Sections 14.5 - 14.6 Robb T. Koether Hampden-Sydney College Mon, Mar 15, 2010 Robb T. Koether (Hampden-Sydney College) Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 1 / 39

Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Inheritance: Polymorphism and Virtual FunctionsLecture 22

Sections 14.5 - 14.6

Robb T. Koether

Hampden-Sydney College

Mon, Mar 15, 2010

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 1 / 39

Page 2: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Outline

1 Access Modes

2 Polymorphism

3 Abstract Classes

4 Assignment

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 2 / 39

Page 3: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Outline

1 Access Modes

2 Polymorphism

3 Abstract Classes

4 Assignment

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 3 / 39

Page 4: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Access to Inherited Members

The three access modes.I PublicI ProtectedI Private

Base-class members and derived-class members have their ownmember-access modes (public, protected, private).The inheritance relation itself has a separate access mode (public,protected, private).

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 4 / 39

Page 5: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Access to Inherited Members

We will normally use public inheritance.Public and protected members of the base class will be accessiblefrom within the scope of the derived classes.Private members of the base class are accessible only from withinthe base class.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 5 / 39

Page 6: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Public Inheritance Accessibility

Example (Public Inheritance)class A{

public: int funcA;protected: int memA;

}class B : public A{

public: int funcB;protected: int memB;

}int main(){

A objA;B objB;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 6 / 39

Page 7: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Public Inheritance Accessibility

From From FromObject Scope of A Scope of B Global ScopefuncA() Yes Yes NofuncB() No Yes NoobjA.funcA() Yes Yes YesobjB.funcA() Yes Yes YesobjB.funcB() Yes Yes YesmemA Yes Yes NomemB No Yes NoobjA.memA Yes Yes NoobjB.memA Yes Yes NoobjB.memB No Yes No

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 7 / 39

Page 8: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Access to Inherited Members

We will not use private inheritance.Private inheritance limits public access by way of class B objectsto the public members of class B.However, from within the scope of class B, there is still access tothe class A public and protected members.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 8 / 39

Page 9: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Private Inheritance Accessibility

Example (Private Inheritance)class A{

public: int funcA();protected: int memA;

}class B : private A{

public: int funcB();protected: int memB;

}int main(){

A objA;B objB;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 9 / 39

Page 10: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Public Inheritance Accessibility

From From FromObject Scope of A Scope of B Global ScopefuncA() Yes Yes NofuncB() No Yes NoobjA.funcA() Yes Yes YesobjB.funcA() Yes Yes NoobjB.funcB() Yes Yes YesmemA Yes Yes NomemB No Yes NoobjA.memA Yes Yes NoobjB.memA Yes Yes NoobjB.memB No Yes No

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 10 / 39

Page 11: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Outline

1 Access Modes

2 Polymorphism

3 Abstract Classes

4 Assignment

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 11 / 39

Page 12: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Polymorphism

Definition (Polymorphism)Polymorphism is the ability of an object of one type to function as anobject of a different type. The “actual” type of the object may not bedetermined until run time. This is called late binding or dynamicbinding.

A function that specifies a base-class object in its parameter listmay accept a derived-class object in its place.Polymorphism works because the derived-class object IS-Abase-class object.We have already seen this used in the constructors.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 12 / 39

Page 13: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Polymorphism and Passing by Value

If a function passes the base-class object by value, then thederived-class object is considered to be an object of the baseclass.Why does this happen?

This happens because the base-class copy constructor was usedto create the local object.The local object loses its derived-class data members andfunctions.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 13 / 39

Page 14: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Polymorphism and Passing by Value

If a function passes the base-class object by value, then thederived-class object is considered to be an object of the baseclass.Why does this happen?This happens because the base-class copy constructor was usedto create the local object.

The local object loses its derived-class data members andfunctions.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 13 / 39

Page 15: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Polymorphism and Passing by Value

If a function passes the base-class object by value, then thederived-class object is considered to be an object of the baseclass.Why does this happen?This happens because the base-class copy constructor was usedto create the local object.The local object loses its derived-class data members andfunctions.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 13 / 39

Page 16: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Polymorphism)int main(){

Man man("John");Woman woman("Jane");Describe(man);Describe(woman);

}

void Describe(Person p){

cout << p << endl; // Is p a Man, a Woman?return;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 14 / 39

Page 17: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Demonstration

Run the program DescribePeople.cpp to see what happens.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 15 / 39

Page 18: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Polymorphism and Passing by Reference

If the function passes the base-class object by reference, then thederived-class object is considered to be an object of the derivedclass.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 16 / 39

Page 19: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Polymorphism)int main(){

Man man("John");Woman woman("Jane");describe(man);describe(woman);

}

void describe(Person& p){

cout << p << endl; // Is p a Man, a Woman?return;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 17 / 39

Page 20: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Demonstration

In the program DescribePeople.cpp, make the describe()function parameter a reference parameter.Run the program to see what happens.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 18 / 39

Page 21: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Outline

1 Access Modes

2 Polymorphism

3 Abstract Classes

4 Assignment

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 19 / 39

Page 22: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Virtual Functions

When base class and a derived class have distinct functions of thesame name, how does the compiler know which one to invoke?If the base-class function is virtual, then the computer will invokethe member function of that name that is closest to the class of theinvoking object.Write the keyword virtual at the beginning of the functionprototype.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 20 / 39

Page 23: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Virtual Functions)class Person{

virtual void output(ostream& out) constout << name << ’ ’ << sex;

};

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 21 / 39

Page 24: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Virtual Functions)int main(){

Man man("John");Woman woman("Jane");describe(man);describe(woman);

}

void describe(Person& p){

cout << p << endl; // What will happen?return;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 22 / 39

Page 25: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Demonstration

In the file person.h, make output() a virtual function.Run the program DescribePeople.cpp to see what happens.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 23 / 39

Page 26: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Virtual Functions and Value Parameters

What happens when the function is virtual and the parameter is avalue parameter?

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 24 / 39

Page 27: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Virtual Functions)int main(){

Man man("John");Woman woman("Jane");describe(man);describe(woman);

}

void describe(Person p){

cout << p << endl; // What will happen?return;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 25 / 39

Page 28: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Demonstration

In the program DescribePeople.cpp, make the functionparameter a value parameter.Run the program to see what happens.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 26 / 39

Page 29: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Pure Virtual Functions

A function may be designated as a pure virtual function.Write

virtual function(parameters) = 0;

The function is not instantiated at this level in the class hierarchy.The function must be instantiated (sooner or later) in the derivedclasses.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 27 / 39

Page 30: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Pure Virtual Functions

This is done whenI the function must be implemented at some level in the hierarchy,I but there is not enough information at the top (base) level to

implement it there.

Example?

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 28 / 39

Page 31: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Abstract Classes

Definition (Abstract Class)An abstract class is a class that contains a pure virtual function. Noobject of an abstract class may be instantiated.

Function parameters of an abstract class type must be passed byreference.Why?

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 29 / 39

Page 32: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Pure Virtual Functions)class Person{

virtual void output(ostream& out) const = 0;};

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 30 / 39

Page 33: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Pure Virtual Functions)int main(){

Man man("John");Woman woman("Jane");describe(man);describe(woman);

}

void describe(Person& p){

cout << p << endl; // What will happen?return;

}

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 31 / 39

Page 34: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Demonstration

In the file person.h, make the output() function pure virtual.In DescribePeople.cpp, make the function parameter areference parameter.Run the program to see what happens.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 32 / 39

Page 35: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Demonstration

In DescribePeople.cpp, make the function parameter a valueparameter.Run the program to see what happens.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 33 / 39

Page 36: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Abstract Class)Circles, squares, and triangles are shapes.Create a Shape class as a base class.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 34 / 39

Page 37: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Abstract Class)

Shape

Circle Rectangle Triangle

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 35 / 39

Page 38: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Abstract Class)Each shape has an area and a perimeter.However, we cannot find the area or perimeter until we know theparticular shape.Therefore, Shape should be an abstract class.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 36 / 39

Page 39: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Example

Example (Abstract Class)

Shapevirtual area() = 0;virtual peri() = 0;

Circlearea() = πr2;peri() = 2πr;

Rectanglearea() = bh;

peri() = 2(b + h);

Trianglearea() = (1/2)bh;peri() = a + b + c;

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 37 / 39

Page 40: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Outline

1 Access Modes

2 Polymorphism

3 Abstract Classes

4 Assignment

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 38 / 39

Page 41: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual

Assignment

HomeworkRead Section 14.5, pages 810 - 822.Read Section 14.6, pages 823 - 827.

Robb T. Koether (Hampden-Sydney College)Inheritance: Polymorphism and Virtual Functions Mon, Mar 15, 2010 39 / 39