C++_polymorphism - Overview of C+ Polymorphism Two main...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ Polymorphism 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 modules 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ Polymorphism 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 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 C++ Polymorphism, Continued
Background image of page 2
CSE 332: C++ Polymorphism Static vs. Dynamic Type The type of a variable is known statically (at compile time), based on its declaration int i; int * p; Fish f; Mammal m; Fish * fp = &f; However, actual types of objects aliased by references & pointers to base classes vary dynamically (at run-time) Fish f; Mammal m; Animal * ap = &f; ap = &m; Animal & ar = get_animal(); Animal Fish Mammal A base class and its derived classes form a set of types type( *ap ) { Animal , Fish , Mammal } typeset( *fp ) typeset( *ap ) Each type set is open More subclasses can be added
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/03/2010 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 13

C++_polymorphism - Overview of C+ Polymorphism Two main...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online