OutlineName VisibilityPolymorphic DerivationPS2 Craps Game RevisitedOutline Visibility Polymorphic Derivation CrapsCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 10October 5, 2010CPSC 427a 1/32Outline Visibility Polymorphic Derivation CrapsName VisibilityPolymorphic DerivationPS2 Craps Game RevisitedCPSC 427a 2/32Outline Visibility Polymorphic Derivation CrapsName visibilityCPSC 427a 3/32Outline Visibility Polymorphic Derivation CrapsPrivate derivation (default)class B : A { ... }; specifies private derivation of B from A.A class member inherited from A become private in B.Like other private members, it is inaccessible outside of B.If public in A, it can be accessed from within A or B or via aninstance of A, but not via an instance of B.If private in A, it can only be accessed from within A.It cannot even be accessed from within B.CPSC 427a 4/32Outline Visibility Polymorphic Derivation CrapsPrivate derivation exampleExample:class A {private: int x;public: int y;};class B : A {... f() {... x++; ...} // privacy violation};//-------- outside of class definitions --------A a; B b;a.x // privacy violationa.y // okb.x // privacy violationb.y // privacy violationCPSC 427a 5/32Outline Visibility Polymorphic Derivation CrapsPublic derivationclass B : public A { ... }; specifies public derivation of Bfrom A.A class member inherited from A retains its privacy status from A.If public in A, it can be accessed from within B and also viainstances of A or B.If private in A, it can only be accessed from within A.It cannot even be accessed from within B.CPSC 427a 6/32Outline Visibility Polymorphic Derivation CrapsPublic derivation exampleExample:class A {private: int x;public: int y;};class B : public A {... f() {... x++; ...} // privacy violation};//-------- outside of class definitions --------A a; B b;a.x // privacy violationa.y // okb.x // privacy violationb.y // okCPSC 427a 7/32Outline Visibility Polymorphic Derivation CrapsThe protected keywordprotected is a privacy status between public and private.Protected class members are inaccessible from outside the class(like private) but accessible within a derived class (like public).Example:class A {protected: int z;};class B : A {... f() {... z++; ...} // ok};CPSC 427a 8/32Outline Visibility Polymorphic Derivation CrapsProtected derivationclass B : protected A { ... }; specifies protectedderivation of B from A.A public or protected class member inherited from A becomesprotected in B.If public in A, it can be accessed from within B and also viainstances of A but not via instances of B.If protected in A, it can be accessed from within A or B but notfrom outside.If private in A, it can only be accessed from within A.It cannot be accessed from within B.CPSC 427a 9/32Outline Visibility Polymorphic Derivation CrapsPrivacy summaryClass AKind of Derivationpublic protected privatepublic public protected privateprotected protected protected privateprivate invisible invisible invisibleVisibility in derived class B.CPSC 427a 10/32Outline Visibility Polymorphic Derivation CrapsPolymorphic DerivationCPSC 427a 11/32Outline Visibility Polymorphic Derivation CrapsPolymorphism and Type HierarchiesConsider following simple type hierarchy:class B { public: int f(); ... };class U : B { int f(); ... };class V : B { int f(); ... };We have a base class B and derived classes U and V.Declare B* bp; U* up = new U; V* vp = new V.Can write bp = up; or bp = vp;.Why does this make sense?*up has an embedded instance of B.*vp has an embedded instance of B.Relationships: A U is a B (and more). A V is a B (and more).CPSC 427a 12/32Outline Visibility Polymorphic Derivation CrapsPolymorphic pointersRecall:class B { public: int f(); ... };class U : B { int f(); ... };class V : B { int f(); ... };B* bp;bp can point to objects of type B, type U, or type V.Say bp is a polymorphic pointer.Want bp->f() to refer to U::f() if bp contains a U pointer.Want bp->f() to refer to V::f() if bp contains a V pointer.In this example, bp->f() always refers to B::f().CPSC 427a 13/32Outline Visibility Polymorphic Derivation CrapsVirtual functionsSolution: Polymorphic derivationclass B { public: virtual int f(); ... };class U : B { virtual int f(); ... };class V : B { virtual int f(); ... };B* bp;A virtual function is dispatched at run time to the class of theactual object.bp->f() refers to U::f() if bp points to a U.bp->f() refers to V::f() if bp points to a V.bp->f() refers to B::f() if bp points to a B.Here, the type refers to the allocation type.CPSC 427a 14/32Outline Visibility Polymorphic Derivation CrapsUnions and type tagsWe can regard bp as a pointer to the union of types U and V.To know which of U::f() or V::f() to use for the call bp->f()requires runtime type tags.If a class has virtual functions, the compiler adds a type tag fieldto each object.This takes space at run time.The compiler also generates a vtable to use in dispatching calls onvirtual functions.CPSC 427a 15/32Outline Visibility Polymorphic Derivation CrapsVirtual destructorsConsider delete bp;, where bp points to a U but has type B*.The U destructor will not be called unless destructor B::~B() isdeclared to be virtual.Note: The base class destructor is always called, whether or not itis virtual.In this way, destructors are different from other member methods.Conclusion: If a derived class has a non-empty destructor, the baseclass destructor should be declared virtual.CPSC 427a 16/32Outline Visibility Polymorphic Derivation CrapsUses of polymorphismSome uses of polymorphism:ITo define an extensible set of representations for a class.ITo allow containers to store mixtures of different but relatedtypes of objects.ITo support run-time variability of within a restricted set ofrelated types.CPSC 427a 17/32Outline Visibility Polymorphic Derivation CrapsMultiple representationsMight want different representations for an object.Example: A point in the plane can be represented by eitherCartesian or Polar coordinates.A Point base class can provide abstract operations on points.E.g., virtual int quadrant() const returns the quadrant of*this.For Cartesian coordinates, quadrant is determined by the signs ofthe x and y coordinates of the point.For polar coordinates, quadrant is determined by the angle θ.Both Cartesian and Polar derived classes should contain amethod for int quadrant() const.CPSC 427a 18/32Outline Visibility Polymorphic Derivation CrapsHeterogeneous containersOne might wish to have a stack of
View Full Document