Recitation Week 13 - PowerPoint PPT Presentation

About This Presentation
Title:

Recitation Week 13

Description:

Object Oriented Programming COP3330 / CGS5409 – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 26
Provided by: Michael4290
Learn more at: https://ww2.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Recitation Week 13


1
Recitation Week 13
  • Object Oriented Programming
  • COP3330 / CGS5409

2
Todays Recitation
  • Class Templates
  • Bitwise Operators

3
Class Templates
  • With class templates, we can make this class work
    with a variety of types, without resorting to
    altering and recompiling code.
  • The class template will work in a similar manner
    to the function template. 
  • To make a class into a template, prefix the class
    definition with the syntax
  • templatelt class T gt

4
Class Templates
  • templatelt class T gt
  • T in the above line is just a type parameter, and
    it can have any name.  Like a function parameter,
    it is a placeholder. 
  • When the class is instantiated, we fill in an
    appropriate type. Use this prefix also on the
    definitions of member functions, and the name of
    the class used in context with the scope
    resolution operator will be
  • classNamelt T gtmemberName

5
Class Template Example
  • Notice that in this SimpleList example, the type
    of the array is T -- the type parameter -- and
    this will be filled in when an object is
    created. 
  • http//www.cs.fsu.edu/myers/cop3330/examples/temp
    lates/simplelist3/

6
Class Template Example
  • Also notice that in the main program, we must
    include the actual definition file -- all the
    template function definitions, in addition to the
    class definition!
  • This is because the compiler creates a different
    version of the class for each type that is used
    when building objects. This means that either the
    entire class should be written in the header
    file,
  • OR that the .cpp file should be included, like
    this
  • include "simplelist3.cpp"

7
Class template implementation
  • Unlike function templates, overloading is NOT
    sufficient for implementing class templates,
    because classes don't have parameter lists.
  • Instead, we need the full instantiation syntax
    when declaring Listltintgt x // etc.
  • The compiler still does it by building multiple
    versions of the class, for each instantiated type
  • This means syntax like ListltTgtfunction() is
    necessary on definitions, since Listltintgt is a
    different class than Listltdoublegt, for example.

8
Another Class Template Example
  • The following is an example of a more complicated
    class that uses array-based storage (dynamic) to
    maintain a list of items.
  • This is a class template, so the type of item
    stored in the list can vary from object to
    object  
  • http//www.cs.fsu.edu/myers/cop3330/examples/temp
    lates/tlist/

9
Yet Another Class Template Example
  • http//www.cs.fsu.edu/myers/savitch3c/Ch16/
  • 16-01.cpp   a simple example of a function
    template.  In this case, it is a swap function,
    for swapping the contents of any two variables.
  • Sort  This example involves two files
  • sort.cpp   three template functions, making up a
    generic version of a selection sort algorithm for
    an array.  Contains a sort() function along with
    two helper functions
  • 16-02.cpp   a main program that uses the sort
    function.
  • 16-04.cpp   a template class called Pair, for
    storing any pair of values (of the same type). 
    Simple class with basic mutator and accessor
    functionality.
  • PFArray This template class example involves
    the following 3 files
  • pfarray.h   header file, declarations for
    PFArray class.  This class stores a dynamically
    allocated array (of type to be specified).
  • pfarray.cpp   implementation file for the class
    (member function definitions)
  • 16-07.cpp   sample main program that uses the
    PFArray template.  Creates one instantiation with
    int and one instantiation with string.
  • PFArrayBak   This template class (with
    inheritance) example involves the following 3
    files.
  • pfarraybak.h   header file for a class that is
    derived from the PFArray class above.  Allows
    storage of a backup copy of the array, with
    restore capability.
  • pfarraybak.cpp   implementation file for the
    class (member function definitions)
  • 16-10.cpp   sample main program that uses the
    PFArrayBak template.  Illustrates class features
    with an instantiation using type string.

10
Bitwise Operators
11
Bitwise Operators
  • Bitwise AND Operator
  • The bitwise AND operator () compares each bit of
    the first operand to the corresponding bit of the
    second operand.
  • If both bits are 1, the corresponding result bit
    is set to 1. Otherwise, the corresponding result
    bit is set to 0.

12
Bitwise Operators
  • Bitwise AND Operator
  • 01001000
  • 10111000
  • --------
  • 00001000

13
Bitwise Operators
  • Bitwise OR Operator
  • The bitwise inclusive OR operator () compares
    each bit of its first operand to the
    corresponding bit of its second operand.
  • If either bit is 1, the corresponding result bit
    is set to 1. Otherwise, the corresponding result
    bit is set to 0.

14
Bitwise Operators
  • Bitwise OR Operator
  • 01001000
  • 10111000
  • --------
  • 11111000

15
Bitwise Operators
  • Bitwise Exclusive OR Operator
  • The bitwise exclusive OR operator () compares
    each bit of its first operand to the
    corresponding bit of its second operand.
  • If one bit is 0 and the other bit is 1, the
    corresponding result bit is set to 1. Otherwise,
    the corresponding result bit is set to 0.

16
Bitwise Operators
  • Bitwise Exclusive OR Operator
  • 01110010
  • 10101010
  • --------
  • 11011000

17
Bitwise Operators
  • Unary NOT Operator
  • The bitwise NOT, or complement, is a unary
    operation that performs logical negation on each
    bit, forming the ones' complement of the given
    binary value. Digits which were 0 become 1, and
    vice versa

18
Flags and Bitmasks Ordering Pizza
  • CHEESE 1 //00000001
  • SAUSAGE 2 //00000010
  • PEPPERONI 4 //00000100
  • HAM 8 //00001000
  • MUSHROOMS 16 //00010000
  • OLIVES 32 //00100000
  • ONIONS 64 //01000000
  • PEPPERS 128 //10000000

19
Flags and Bitmasks Ordering Pizza
  • Setting bits
  • To set a bit, we use the OR operator, like so
  • PizzaFlags PizzaFlags OR CHEESE OR PEPPERONI OR
    ONIONS
  • Using "OR" may seem counter-intuitive at first
    doesn't "and" mean to put two things together?
    but you must remember that it only works this way
    when comparing values. When setting values, the
    "opposite" is true, in the sense that writing is
    the "opposite" of reading

20
Flags and Bitmasks Ordering Pizza
  • Setting bits
  • 00000000 //Initial state
  • OR 00000001 //CHEESE bit
  • OR 00000100 //PEPPERONI bit
  • OR 01000000 //ONIONS bit
  • ------------
  • 1000101 //Final result

21
Flags and Bitmasks Ordering Pizza
  • Unsetting bits
  • To unset a bit, you must combine two operators,
    AND and NOT. NOT will reverse the bits in the
    flag, and AND will unset the one 0 bit while
    leaving the others alone. Let's say that in the
    previous example, we wanted to unset the ONIONS
    bit.

22
Flags and Bitmasks Ordering Pizza
  • Unsetting bits
  • NOT 01000000 //ONIONS bit
  • ------------
  • 10111111 //Inverse of ONIONS bit
  • 01000101 //Initial state
  • AND 10111111 //Inverse of ONIONS bit
  • ------------
  • 00000101 //ONIONS bit unset

23
Flags and Bitmasks Ordering Pizza
  • Toggling bits
  • What if you wanted to flip a bit back and forth
    each time, without having to check its state?
    That's where the XOR operator comes in handy.
    Although this has no practical value in the above
    pizza example, you can use XOR to flip the ONIONS
    bit back and forth.

24
Flags and Bitmasks Ordering Pizza
  • Toggling bits
  • 01000101 //Initial state
  • XOR 01000000 //ONIONS bit
  • ------------
  • 00000101 //ONIONS bit toggled "off"
  • 00000101 //Initial state
  • XOR 01000000 //ONIONS bit
  • ------------
  • 00000101 //ONIONS bit toggled "on"

25
Questions?
Write a Comment
User Comments (0)
About PowerShow.com