38
Inheritance: Polymorphism and Virtual Functions Robb T. Koether Access Modes Polymorphism Abstract Classes Assignment Inheritance: Polymorphism and Virtual Functions Lecture 23 Sections 14.5 - 14.6 Robb T. Koether Hampden-Sydney College Wed, Mar 18, 2009

Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Inheritance: Polymorphism and VirtualFunctionsLecture 23

Sections 14.5 - 14.6

Robb T. Koether

Hampden-Sydney College

Wed, Mar 18, 2009

Page 2: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Outline

1 Access Modes

2 Polymorphism

3 Abstract Classes

4 Assignment

Page 3: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Access to Inherited Members

The three access modes.PublicProtectedPrivate

Base-class members and derived-class members havetheir own member-access modes (public, protected,private).The inheritance relation itself has a separate accessmode (public, protected, private).

Page 4: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Access to Inherited Members

We will normally use public inheritance.Public and protected members of the base class will beaccessible from within the scope of the derived classes.Only public members of the base class will beaccessible from outside the scope of the derivedclasses.Private members of the base class are accessible onlyfrom within the base class.

Page 5: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Public Inheritance Accessibility

Example (Public Inheritance)class A{

public: funcA();protected: int aMem;

}class B : public A{

public: funcB();protected: int bMem;

}int main(){

A aObj;B bObj;

}

Suppose we have the above.

Page 6: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Public Inheritance Accessibility

From From From FromScope of A Scope of B Global Scope Global Scope

funcA() funcA() aObj.funcA() bObj.funcA()aMem aMem aObj.aMem bObj.aMemfuncB() funcB() bObj.funcB()bMem bMem bObj.bMem

Page 7: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Access to Inherited Members

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

Page 8: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Private Inheritance Accessibility

Example (Private Inheritance)class A{

public: funcA();protected: int aMem;

}class B : private A{

public: funcB();protected: int bMem;

}int main(){

A aObj;B bObj;

}

Suppose we have the above.

Page 9: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Private Inheritance Accessibility

From From From FromScope of A Scope of B Global Scope Global Scope

funcA() funcA() aObj.funcA() bObj.funcA()aMem aMem aObj.aMem bObj.aMemfuncB() funcB() bObj.funcB()bMem bMem bObj.bMem

Page 10: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Polymorphism

Definition (Polymorphism)Polymorphism is the ability of an object of one type tofunction as an object of a different type. The “actual” type ofthe object may not be determined until run time. This iscalled late binding or dynamic binding.

A function that specifies a base-class object in itsparameter list may accept a derived-class object in itsplace.Polymorphism works because the derived-class objectIS-A base-class object.We have already seen this used in the constructors.

Page 11: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Polymorphism

Example (Polymorphism)class Animal {...};class Mammal : public Animal {...};int main(){

Animal* mptr;...

mptr->output(cout); // Which output()?}

Page 12: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Polymorphism and Passing by Value

If a function passes the base-class object by value,then the derived-class object is considered to be anobject of the base class.Why does this happen?This happens because the base-class copy constructorwas used to create the local object.The local object loses its derived-class data membersand functions.

Page 13: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Polymorphism and Passing by Value

If a function passes the base-class object by value,then the derived-class object is considered to be anobject of the base class.Why does this happen?This happens because the base-class copy constructorwas used to create the local object.The local object loses its derived-class data membersand functions.

Page 14: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Polymorphism and Passing by Value

If a function passes the base-class object by value,then the derived-class object is considered to be anobject of the base class.Why does this happen?This happens because the base-class copy constructorwas used to create the local object.The local object loses its derived-class data membersand functions.

Page 15: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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;

}

Page 16: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Demonstration

Run the program DescribePeople.cpp to see whathappens.

Page 17: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Polymorphism and Passing by Reference

If the function passes the base-class object byreference, then the derived-class object is consideredto be an object of the derived class.

Page 18: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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;

}

Page 19: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Demonstration

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

Page 20: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Virtual Functions

When base class and a derived class have distinctfunctions of the same name, how does the compilerknow which one to invoke?If the base-class function is virtual, then the computerwill invoke the member function of that name that isclosest to the class of the invoking object.Write the keyword virtual at the beginning of thefunction prototype.

Page 21: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Example

Example (Virtual Functions)class Person{

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

};

Page 22: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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;

}

Page 23: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Demonstration

In the file person.h, make output() a virtualfunction.Run the program DescribePeople.cpp to see whathappens

Page 24: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Virtual Functions and Value Parameters

What happens when the function is virtual and theparameter is a value parameter?

Page 25: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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;

}

Page 26: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Demonstration

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

Page 27: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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 classhierarchy.The function must be instantiated (sooner or later) inthe derived classes.

Page 28: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Pure Virtual Functions

This is done whenthe function must be implemented at some level in thehierarchy,but there is not enough information at the top (base)level to implement it there.

Example?

Page 29: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Abstract Classes

Definition (Abstract Class)An abstract class is a class that contains a pure virtualfunction. No object of an abstract class may be instantiated.

Function parameters of an abstract class type must bepassed by reference.Why?

Page 30: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Example

Example (Pure Virtual Functions)class Person{

virtual voidoutput(ostream& out) const = 0;

};

Page 31: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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;

}

Page 32: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Demonstration

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

Page 33: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Demonstration

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

Page 34: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Example

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

Page 35: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Example

Example (Abstract Class)

Shape

Circle Rectangle Triangle

Page 36: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Example

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

Page 37: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

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;

Page 38: Inheritance: Polymorphism and Virtual Functionspeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures/Spring 2009/L… · A aObj; B bObj;} Suppose we have the above. Inheritance: Polymorphism

Inheritance:Polymorphism

and VirtualFunctions

Robb T.Koether

AccessModes

Polymorphism

AbstractClasses

Assignment

Assignment

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