SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Overview of C++ Polymorphism
• Two main kinds of types in C++: native and user-defined
   – “User” defined types: declared classes, structs, unions
       • including types provided by the C++ standard libraries
   – Native types are “built in” to the C++ language itself: int, long, float, …
   – A typedef creates a new type name for another type (type aliasing)

• Public inheritance creates sub-types
   – Inheritance only applies to user-defined classes (and structs)
   – A publicly derived class is-a subtype of its base class
   – Known as “inheritance polymorphism”


• Template parameters also induce a subtype relation
   – Known as “interface polymorphism”
   – We’ll cover how this works in depth, in later sessions

• Liskov Substitution Principle (for both kinds of polymorphism)
   – if S is a subtype of T, then wherever you need a T you can use an S

  CSE 332: C++ Polymorphism
C++ Polymorphism, Continued
• Inheritance polymorphism depends on public virtual
  member functions in C++
   – Base class declares a member function virtual
   – Derived class overrides the base class’s definition of the function


• Private or protected inheritance creates a form of
  encapsulation
   – Does not create a substitutable sub-type
   – A privately derived class wraps its base class
   – The class form of the Adapter Pattern uses this technique




CSE 332: C++ Polymorphism
Static vs. Dynamic Type
• The type of a variable is
  known statically (at compile                     Animal
  time), based on its declaration
   int i; int * p;
   Fish f; Mammal m;
                                            Fish            Mammal
   Fish * fp = &f;

                                   • A base class and its derived
• However, actual types of           classes form a set of types
  objects aliased by references
                                      type(*ap) ∈ {Animal, Fish,
  & pointers to base classes
                                                          Mammal}
  vary dynamically (at run-time)
                                      typeset(*fp) ⊂ typeset(*ap)
   Fish f; Mammal m;
   Animal * ap = &f;               • Each type set is open
   ap = &m;                           – More subclasses can be added
   Animal & ar =
     get_animal();

  CSE 332: C++ Polymorphism
Forms of Inheritance
• Derived class inherits from base class
• Public Inheritance (“is a”)
  – Public part of base class remains public
  – Protected part of base class remains protected
• Protected Inheritance (“contains a”)
  – Public part of base class becomes protected
  – Protected part of base class remains protected
• Private Inheritance (“contains a”)
  – Public part of base class becomes private
  – Protected part of base class becomes private

CSE 332: C++ Polymorphism
Public, Protected, Private Inheritance
class A {               • Class A declares 3 variables
public:                     – i is public to all users of class A
   int i;                   – j is protected. It can only be used by methods
protected:                    in class A or its derived classes (+ friends)
   int j;                   – k is private. It can only be used by methods in
private:                      class A (+ friends)
   int k;
};
                        • Class B uses public inheritance from A
Class B : public A {        – i remains public to all users of class B
// ...                      – j remains protected. It can be used by methods
};                            in class B or its derived classes
Class C : protected A {
// ...
                        •   Class C uses protected inheritance from A
};
Class D : private A {       – i becomes protected in C, so the only users of
// ...
                              class C that can access i are the methods of class C
};                          – j remains protected. It can be used by methods
                              in class C or its derived classes

                        • Class D uses private inheritance from A
                            – i and j become private in D, so only methods of
                              class D can access them.
  CSE 332: C++ Polymorphism
Class and Member Construction Order
class A {                         • In the main function, the B
public:
   A(int i) :m_i(i) {
                                    constructor is called on object b
     cout << "A“ << endl;}           – Passes in integer values 2 and 3
   ~A() {cout<<"~A"<<endl;}       • B constructor calls A constructor
private:
   int m_i;
                                     – passes value 2 to A constructor via
};
                                       base/member initialization list
class B : public A {              • A constructor initializes m_i
public:                              – with the passed value 2
   B(int i, int j)
     : A(i), m_j(j) {             • Body of A constructor runs
     cout << “B” << endl;}           – Outputs “A”
   ~B() {cout << “~B” << endl;}
private:
                                  • B constructor initializes m_j
    int m_j;                         – with passed value 3
};                                • Body of B constructor runs
int main (int, char *[]) {
    B b(2,3);
                                     – outputs “B”
    return 0;
};



  CSE 332: C++ Polymorphism
Class and Member Destruction Order
class A {                          •   B destructor called on object b in main
public:                            •   Body of B destructor runs
   A(int i) :m_i(i) {
                                        – outputs “~B”
     cout << "A“ << endl;}
   ~A() {cout<<"~A"<<endl;}        •   B destructor calls “destructor” of m_j
private:                                – int is a built-in type, so it’s a no-op
   int m_i;                        •   B destructor calls A destructor
};                                 •   Body of A destructor runs
class B : public A {
                                        – outputs “~A”
public:
   B(int i, int j) :A(i), m_j(j) { •   A destructor calls “destructor” of m_i
     cout << “B” << endl;}              – again a no-op
   ~B() {cout << “~B” << endl;}    •   Compare orders of construction and
private:                               destruction of base, members, body
    int m_j;                            – at the level of each class, order of steps
};                                        is reversed in constructor vs. destructor
int main (int, char *[]) {              – ctor: base class, members, body
    B b(2,3);                           – dtor: body, members, base class
    return 0;
};




  CSE 332: C++ Polymorphism
Virtual Functions
class A {                         • Used to support polymorphism
public:
   A () {cout<<" A";}
                                    with pointers and references
   virtual ~A () {cout<<" ~A";}   • Declared virtual in a base class
   virtual f(int);
};
                                  • Can overridde in derived class
                                      – Overriding only happens when
class B : public A {                    signatures are the same
public:                               – Otherwise it just overloads the
   B () :A() {cout<<" B";}              function or operator name
   virtual ~B() {cout<<" ~B";}            • More about overloading next lecture
   virtual f(int) override; //C++11
};
                                 •    Ensures derived class function
                                      definition is resolved dynamically
int main (int, char *[]) {            – E.g., that destructors farther down
   // prints "A B"                      the hierarchy get called
   A *ap = new B;
                                • Use final (C++11) to prevent
   // prints "~B ~A" : would only overriding of a virtual method
   // print "~A" if non-virtual
   delete ap;                   • Use override (C++11) in
   return 0;                      derived class to ensure that the
};                                signatures match (error if not)
   CSE 332: C++ Polymorphism
Virtual Functions
class A {
public:                            •   Only matter with pointer or reference
   void x() {cout<<"A::x";};            – Calls on object itself resolved statically
   virtual void y() {cout<<"A::y";};    – E.g., b.y();
};
                                  •    Look first at pointer/reference type
class B : public A {                    – If non-virtual there, resolve statically
public:                                      • E.g., ap->x();
   void x() {cout<<"B::x";};            – If virtual there, resolve dynamically
   virtual void y() {cout<<"B::y";};         • E.g., ap->y();
};                                 •   Note that virtual keyword need not be
                                       repeated in derived classes
int main () {
                                        – But it’s good style to do so
   B b;
   A *ap = &b; B *bp = &b;        •    Caller can force static resolution of a
   b.x (); // prints "B::x"            virtual function via scope operator
   b.y (); // prints "B::y"             – E.g., ap->A::y(); prints “A::y”
   bp->x (); // prints "B::x"
   bp->y (); // prints "B::y"
   ap->x (); // prints "A::x"
   ap->y (); // prints "B::y"
   return 0;
};

  CSE 332: C++ Polymorphism
Potential Problem: Class Slicing
• Catch derived exception types by reference
• Also pass derived types by reference
• Otherwise a temporary variable is created
  – Loses original exception’s “dynamic type”
  – Results in “the class slicing problem” where only the
    base class parts and not derived class parts copy




  CSE 332: C++ Polymorphism
Pure Virtual Functions
class A {                  • A is an abstract (base) class
public:
   virtual void x() = 0;
                              – Similar to an interface in Java
   virtual void y() = 0;      – Declares pure virtual functions (=0)
};                            – May also have non-virtual methods, as well
                                as virtual methods that are not pure virtual
class B : public A {
public:
   virtual void x();       • Derived classes override pure virtual
};                           methods
                              – B overrides x(), C overrides y()
class C : public B {
public:
   virtual void y();       • Can’t instantiate an abstract class
};
                              – class that declares pure virtual functions
int main () {                 – or inherits ones that are not overridden
   A * ap = new C;            – A and B are abstract, can create a C
   ap->x ();
   ap->y ();
   delete ap;              • Can still have a pointer or reference to
   return 0;                 an abstract class type
};
                              – Useful for polymorphism
 CSE 332: C++ Polymorphism
Design with Pure Virtual Functions
• Pure virtual functions let us                   Animal
  specify interfaces                             move()=0
  appropriately
   – But let us defer                     Fish             Mammal
     implementation decisions            move()            move()
     until later (subclasses)            swim()            walk()


• As the type hierarchy is                                          Bird
  extended, pure virtual
  functions are replaced                    Sparrow           Penguin
   – By virtual functions that fill in       move()            move()
                                             walk()           waddle()
     (and may override) the
                                              fly()            swim()
     implementation details
   – Key idea: refinement

  CSE 332: C++ Polymorphism
Summary: Tips on Inheritance Polymorphism
• A key tension
    – Push common code and variables up into base classes
    – Make base classes as general as possible
•   Use abstract base classes to declare interfaces
•   Use public inheritance to make sets of polymorphic types
•   Use private or protected inheritance only for encapsulation
•   Inheritance polymorphism depends on dynamic typing
    – Use a base-class pointer (or reference) if you want inheritance
      polymorphism of the objects pointed to (or referenced)
    – Use virtual member functions for dynamic overriding
• Even though you don’t have to, label each inherited virtual
  (and pure virtual) method “virtual” in derived classes
• Use final (C++11) to prevent overriding of a virtual method
• Use override (C++11) to make sure signatures match

    CSE 332: C++ Polymorphism

Weitere ähnliche Inhalte

Was ist angesagt?

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
harshaltambe
 

Was ist angesagt? (17)

Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
class and objects
class and objectsclass and objects
class and objects
 
Oop l2
Oop l2Oop l2
Oop l2
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 

Andere mochten auch

Inline function
Inline functionInline function
Inline function
Tech_MX
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
023henil
 

Andere mochten auch (20)

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
polymorphism
polymorphism polymorphism
polymorphism
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
A simple way for polymorphism and structured programming - Go interfaces
A simple way for polymorphism and structured programming - Go interfacesA simple way for polymorphism and structured programming - Go interfaces
A simple way for polymorphism and structured programming - Go interfaces
 
java concept
java conceptjava concept
java concept
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Inline function
Inline functionInline function
Inline function
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
ICT C++
ICT C++ ICT C++
ICT C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Ähnlich wie C++ polymorphism

Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
c++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfc++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdf
nisarmca
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
C++ programming intro
C++ programming introC++ programming intro
C++ programming intro
marklaloo
 

Ähnlich wie C++ polymorphism (20)

Presentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).pptPresentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Basics of objective c
Basics of objective cBasics of objective c
Basics of objective c
 
c++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfc++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdf
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
C++ programming intro
C++ programming introC++ programming intro
C++ programming intro
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Unit iv
Unit ivUnit iv
Unit iv
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 

C++ polymorphism

  • 1. Overview of C++ Polymorphism • Two main kinds of types in C++: native and user-defined – “User” defined types: declared classes, structs, unions • including types provided by the C++ standard libraries – Native types are “built in” to the C++ language itself: int, long, float, … – A typedef creates a new type name for another type (type aliasing) • Public inheritance creates sub-types – Inheritance only applies to user-defined classes (and structs) – A publicly derived class is-a subtype of its base class – Known as “inheritance polymorphism” • Template parameters also induce a subtype relation – Known as “interface polymorphism” – We’ll cover how this works in depth, in later sessions • Liskov Substitution Principle (for both kinds of polymorphism) – if S is a subtype of T, then wherever you need a T you can use an S CSE 332: C++ Polymorphism
  • 2. C++ Polymorphism, Continued • Inheritance polymorphism depends on public virtual member functions in C++ – Base class declares a member function virtual – Derived class overrides the base class’s definition of the function • Private or protected inheritance creates a form of encapsulation – Does not create a substitutable sub-type – A privately derived class wraps its base class – The class form of the Adapter Pattern uses this technique CSE 332: C++ Polymorphism
  • 3. Static vs. Dynamic Type • The type of a variable is known statically (at compile Animal time), based on its declaration int i; int * p; Fish f; Mammal m; Fish Mammal Fish * fp = &f; • A base class and its derived • However, actual types of classes form a set of types objects aliased by references type(*ap) ∈ {Animal, Fish, & pointers to base classes Mammal} vary dynamically (at run-time) typeset(*fp) ⊂ typeset(*ap) Fish f; Mammal m; Animal * ap = &f; • Each type set is open ap = &m; – More subclasses can be added Animal & ar = get_animal(); CSE 332: C++ Polymorphism
  • 4. Forms of Inheritance • Derived class inherits from base class • Public Inheritance (“is a”) – Public part of base class remains public – Protected part of base class remains protected • Protected Inheritance (“contains a”) – Public part of base class becomes protected – Protected part of base class remains protected • Private Inheritance (“contains a”) – Public part of base class becomes private – Protected part of base class becomes private CSE 332: C++ Polymorphism
  • 5. Public, Protected, Private Inheritance class A { • Class A declares 3 variables public: – i is public to all users of class A int i; – j is protected. It can only be used by methods protected: in class A or its derived classes (+ friends) int j; – k is private. It can only be used by methods in private: class A (+ friends) int k; }; • Class B uses public inheritance from A Class B : public A { – i remains public to all users of class B // ... – j remains protected. It can be used by methods }; in class B or its derived classes Class C : protected A { // ... • Class C uses protected inheritance from A }; Class D : private A { – i becomes protected in C, so the only users of // ... class C that can access i are the methods of class C }; – j remains protected. It can be used by methods in class C or its derived classes • Class D uses private inheritance from A – i and j become private in D, so only methods of class D can access them. CSE 332: C++ Polymorphism
  • 6. Class and Member Construction Order class A { • In the main function, the B public: A(int i) :m_i(i) { constructor is called on object b cout << "A“ << endl;} – Passes in integer values 2 and 3 ~A() {cout<<"~A"<<endl;} • B constructor calls A constructor private: int m_i; – passes value 2 to A constructor via }; base/member initialization list class B : public A { • A constructor initializes m_i public: – with the passed value 2 B(int i, int j) : A(i), m_j(j) { • Body of A constructor runs cout << “B” << endl;} – Outputs “A” ~B() {cout << “~B” << endl;} private: • B constructor initializes m_j int m_j; – with passed value 3 }; • Body of B constructor runs int main (int, char *[]) { B b(2,3); – outputs “B” return 0; }; CSE 332: C++ Polymorphism
  • 7. Class and Member Destruction Order class A { • B destructor called on object b in main public: • Body of B destructor runs A(int i) :m_i(i) { – outputs “~B” cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} • B destructor calls “destructor” of m_j private: – int is a built-in type, so it’s a no-op int m_i; • B destructor calls A destructor }; • Body of A destructor runs class B : public A { – outputs “~A” public: B(int i, int j) :A(i), m_j(j) { • A destructor calls “destructor” of m_i cout << “B” << endl;} – again a no-op ~B() {cout << “~B” << endl;} • Compare orders of construction and private: destruction of base, members, body int m_j; – at the level of each class, order of steps }; is reversed in constructor vs. destructor int main (int, char *[]) { – ctor: base class, members, body B b(2,3); – dtor: body, members, base class return 0; }; CSE 332: C++ Polymorphism
  • 8. Virtual Functions class A { • Used to support polymorphism public: A () {cout<<" A";} with pointers and references virtual ~A () {cout<<" ~A";} • Declared virtual in a base class virtual f(int); }; • Can overridde in derived class – Overriding only happens when class B : public A { signatures are the same public: – Otherwise it just overloads the B () :A() {cout<<" B";} function or operator name virtual ~B() {cout<<" ~B";} • More about overloading next lecture virtual f(int) override; //C++11 }; • Ensures derived class function definition is resolved dynamically int main (int, char *[]) { – E.g., that destructors farther down // prints "A B" the hierarchy get called A *ap = new B; • Use final (C++11) to prevent // prints "~B ~A" : would only overriding of a virtual method // print "~A" if non-virtual delete ap; • Use override (C++11) in return 0; derived class to ensure that the }; signatures match (error if not) CSE 332: C++ Polymorphism
  • 9. Virtual Functions class A { public: • Only matter with pointer or reference void x() {cout<<"A::x";}; – Calls on object itself resolved statically virtual void y() {cout<<"A::y";}; – E.g., b.y(); }; • Look first at pointer/reference type class B : public A { – If non-virtual there, resolve statically public: • E.g., ap->x(); void x() {cout<<"B::x";}; – If virtual there, resolve dynamically virtual void y() {cout<<"B::y";}; • E.g., ap->y(); }; • Note that virtual keyword need not be repeated in derived classes int main () { – But it’s good style to do so B b; A *ap = &b; B *bp = &b; • Caller can force static resolution of a b.x (); // prints "B::x" virtual function via scope operator b.y (); // prints "B::y" – E.g., ap->A::y(); prints “A::y” bp->x (); // prints "B::x" bp->y (); // prints "B::y" ap->x (); // prints "A::x" ap->y (); // prints "B::y" return 0; }; CSE 332: C++ Polymorphism
  • 10. Potential Problem: Class Slicing • Catch derived exception types by reference • Also pass derived types by reference • Otherwise a temporary variable is created – Loses original exception’s “dynamic type” – Results in “the class slicing problem” where only the base class parts and not derived class parts copy CSE 332: C++ Polymorphism
  • 11. Pure Virtual Functions class A { • A is an abstract (base) class public: virtual void x() = 0; – Similar to an interface in Java virtual void y() = 0; – Declares pure virtual functions (=0) }; – May also have non-virtual methods, as well as virtual methods that are not pure virtual class B : public A { public: virtual void x(); • Derived classes override pure virtual }; methods – B overrides x(), C overrides y() class C : public B { public: virtual void y(); • Can’t instantiate an abstract class }; – class that declares pure virtual functions int main () { – or inherits ones that are not overridden A * ap = new C; – A and B are abstract, can create a C ap->x (); ap->y (); delete ap; • Can still have a pointer or reference to return 0; an abstract class type }; – Useful for polymorphism CSE 332: C++ Polymorphism
  • 12. Design with Pure Virtual Functions • Pure virtual functions let us Animal specify interfaces move()=0 appropriately – But let us defer Fish Mammal implementation decisions move() move() until later (subclasses) swim() walk() • As the type hierarchy is Bird extended, pure virtual functions are replaced Sparrow Penguin – By virtual functions that fill in move() move() walk() waddle() (and may override) the fly() swim() implementation details – Key idea: refinement CSE 332: C++ Polymorphism
  • 13. Summary: Tips on Inheritance Polymorphism • A key tension – Push common code and variables up into base classes – Make base classes as general as possible • Use abstract base classes to declare interfaces • Use public inheritance to make sets of polymorphic types • Use private or protected inheritance only for encapsulation • Inheritance polymorphism depends on dynamic typing – Use a base-class pointer (or reference) if you want inheritance polymorphism of the objects pointed to (or referenced) – Use virtual member functions for dynamic overriding • Even though you don’t have to, label each inherited virtual (and pure virtual) method “virtual” in derived classes • Use final (C++11) to prevent overriding of a virtual method • Use override (C++11) to make sure signatures match CSE 332: C++ Polymorphism