CS304: Lecture 6 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

CS304: Lecture 6

Description:

Well, automatic isn't the way computers work. We have to overload the operator ... Using a function template is trivial; Calling the function as usual does the trick ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 30
Provided by: matthe49
Category:

less

Transcript and Presenter's Notes

Title: CS304: Lecture 6


1
CS304 Lecture 6
  • OOP Polymorphism Templates
  • Deitel, Ch. 13 14
  • http//www.cis.uab.edu/cs304

2
Definitions
  • Polymorphism Program in general rather than in
    the specific
  • Deal with objects in the same class hierarchy in
    the same manner as the base class
  • Example From UabEmployee class
  • Theme Extensibility!

3
What Good is Polymorphism?
  • Treating objects as its base class allows
    grouping of derived and base types
  • Imagine an array of UabEmployees

4
Using Derived Types
  • It is possible to use functions from base classes
  • BaseclassfunctionName()
  • We can do this, because Derivedclass is-a
    Baseclass

5
An Inheritance issue What happens here?
  • class b public a
  • public
  • void a1()
  • cout ltlt "b1\n"
  • int main()
  • a test new b
  • test-gta1()
  • b test2
  • test2.a1()
  • class a
  • public
  • void a1()
  • cout ltlt "a1\n"
  • void a2()
  • cout ltlt "a2\n"

6
Why on Earth does it do that?!?
  • test is, in the previous example, is a base-class
    pointer.
  • Invoking the base function is perfectly legal,
    due to the is-a relationship
  • Cant really assign a base-class object to a
    derived class pointer
  • We want a way to specify that the function should
    be dynamically bound as opposed to statically
    bound
  • Dynamic binding allows a program to choose the
    correct implementation of a function at runtime

7
Virtual functions
  • The virtual keyword can let a derived class
    override the base classs behavior through a base
    class pointer
  • class a
  • public
  • virtual void a1()
  • cout ltlt "a1\n"
  • virtual void a2()
  • cout ltlt "a2\n"

8
Type Fields and switch Statements
  • Take, as example, a shape class. If each member
    (and derived member) has an attribute called
    shapeType, we can use this attribute to choose
    a function call at runtime
  • Whats the problem with this, from a software
    engineering standpoint?

9
Segue into Abstract Classes/Pure Virtuals
  • Classes which are never actually instantiated are
    abstract classes.
  • These are usually used as base classes in
    inheritance, so they are also called abstract
    base classes.
  • Abstract classes are incomplete. Derived classes
    fill in the missing pieces.
  • Classes that are able to be instantiated are
    called concrete classes.

10
Pure Virtuals
  • An abstract class has at least one pure virtual
    function, which is a virtual function with no
    implementation.
  • virtual void draw() const 0
  • 0 is often called the pure specifier
  • Virtual functions are placeholders
  • Abstract functions are also placeholders in a
    sense
  • Pointers to abstract functions point to derived
    objects

11
Virtual Destructors
  • What happens if we have a derived object that
    defines its own non-virtual destructor, but is
    destroyed through a base-class pointer?
  • Problem solved declaring the base class to have a
    virtual destructor
  • virtual classname()

12
  • Templates
  • Deitel, Ch. 14
  • http//www.cis.uab.edu/cs304

13
Generic Programming
  • Templates are Cs method of allowing generic
    programming.
  • This is the method of allowing a single code
    segment an entire range of overloaded functions,
    or an entire range of related classes.

14
Function vs. Class Templates Examples
  • Function A sort function! Imagine writing just
    one sort that will accept an arbitrary container
    type, as well as an arbitrary data type.
  • Class A stack class. Such a stack could be
    instantiated for any type, so it could hold any
    data type with one implementation. (How would
    this be different from a similar stack in Java?)

15
Function Templates
  • When overloading functions, its often
    repetitive, resulting in identical (or similar
    functions)
  • Identical implementations can be abstracted to a
    template, where the types can be inferred from
    calls to the function.

16
Example Add
  • Initially, unrelated implementations
  • int add(int x, int y) return x y
  • unsigned int add (uint x, uint y) return x y
  • char add(char x, char y) return x y
  • float add(float x, float y) return x y
  • double add(double x, double y) return x y
  • mytype add(mytype x, mytype y) return x y

17
Problems with Add
  • This takes SIX function definitions to do the
    same exact things with the six types given
  • Also, notice If we want to add more user
    defined types, we have to add even more!

18
Add An Improvement Through Templates
  • Template lt typename Type gt
  • Type add(Type x, Type y)
  • return x y
  • //Note The type template parameter Type is
    usually abbreviated as T

19
How Does the Compiler Handle This?
  • In this case, there is very little intelligence
    behind the actual implemenation of templates.

20
Templates with User Defined Types
  • How can we automatically use a user defined type
    in the add function?
  • Well, automatic isnt the way computers work. We
    have to overload the operator
  • This is the second reason that operator
    overloading is so important in C!
  • No overloading results in a program that wont
    compile

21
Overloading Function Templates
  • What if almost all of the functions we want to
    overload are identical, but one or two are quite
    different? (Practical application Summing an
    array of numbers)
  • Specifying a non-templatized function will
    override the corresponding function created by
    the template

22
Class Template
  • Lets expand on the idea of a stack template that
    can take any type. Here is the usual
    declaration

23
  • class stack int d int index int
    sizestack() d new int10 size 10 index
    -1 stack() delete d void push(int
    val)
  • int peek()
  • int pop()

24
  • template lttypename Tgt class stack T d int
    index int sizestack() d new T10 size
    10 index -1 stack() delete d void
    push(T val)
  • T peek()
  • T pop()

25
Using a Template Class in a Program
  • Using a function template is trivial Calling the
    function as usual does the trick
  • Instantiating a templated class is not the same,
    because the compiler wont know which type to use
    to instantiate. This must be given at
    declaration.
  • int main() Stack ltdoublegt doubleStack

26
Nontype Parameters
  • Nontypes can be used with templates in order to
    make compile-time information useful in a classs
    definition.
  • Example A stack class that has a statically
    allocated block of memory of a specific size, in
    order to reduce the cost of dynamic allocation
  • template lttypename T, int elementsgt
  • Stackltdouble, 100gt HundredSlotStack
  • Instantiate a 100-element stack

27
Notes on Templates and
  • Inheritance
  • A class template can be derived from
  • a class-template specialization
  • a nontemplate class
  • A class-template specialization can be derived
    from
  • a class-template specialization ???
  • A nontemplate class can be derived from
  • a class-template specialization

28
Notes on Templates and
  • Friends
  • It is possible to use template classes,
    class-template specializations, and template
    functions as friends
  • Please see 14.7 in the book for further
    information, but it works as you would expect.

29
Notes on Templates and
  • Static Members
  • Recall that class-template specializations make
    copies of the class, and basically substitutes
    the typename in for the typename variable used in
    the class
  • This implies that static variables are shared
    between all objects of a class-template
    specialization, but not all class-template
    specializations, i.e., each specialization has
    its own copy
Write a Comment
User Comments (0)
About PowerShow.com