Object Oriented Programming COP3330 / CGS5409 Class Templates - - PowerPoint PPT Presentation
Object Oriented Programming COP3330 / CGS5409 Class Templates - - PowerPoint PPT Presentation
Object Oriented Programming COP3330 / CGS5409 Class Templates Bitwise Operators 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
Class Templates Bitwise Operators
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: template< class T >
template< class T >
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
- f the class used in context with the scope
resolution operator will be: className< T >::memberName
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/exa
mples/templates/simplelist3/
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"
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: List<int> x; // etc.
The compiler still does it by building multiple
versions of the class, for each instantiated type
This means syntax like List<T>::function()
is necessary on definitions, since List<int> is a different class than List<double>, for 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
- bject:
http://www.cs.fsu.edu/~myers/cop3330/exa
mples/templates/tlist/
http://www.cs.fsu.edu/~myers/savitch3c++/Ch16/
16 16-01.c 1.cpp : a simple example of a function template. In this case, it is a sw swap function, for swapping the contents of any two variables.
Sort rt: This example involves two files
- sort.
t.cp cpp : three template functions, making up a generic version of a selection sort algorithm for an array. Contains a so sort rt() function along with two helper functions
- 16
16-02.cp cpp : a main program that uses the so sort rt function.
16 16-04.c 4.cpp : a template class called Pa Pair ir, for storing any pair of values (of the same type). Simple class with basic mutator and accessor functionality.
PFArra rray : This template class example involves the following 3 files
- pfar
farra ray.h : header file, declarations for PFArray class. This class stores a dynamically allocated array (of type to be specified).
- pfar
farra ray.cpp : implementation file for the class (member function definitions)
- 16
16-07.cp cpp : sample main program that uses the PFArray template. Creates one instantiation with int int and one instantiation with st strin ring.
PFArra rrayBak : This template class (with inheritance) example involves the following 3 files.
- pfar
farra raybak. k.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.
- pfar
farra raybak. k.cpp : implementation file for the class (member function definitions)
- 16
16-10.cp cpp : sample main program that uses the PFArrayBak template. Illustrates class features with an instantiation using type st stri ring ng.
Bit
itwis wise A AND D Ope perato tor: & &
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.
Bit
itwis wise A AND D Ope perato tor: & & 01001000 & 10111000 =
- 00001000
Bi
Bitwise OR OR Op Oper erator: |
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.
Bi
Bitwise OR OR Op Oper erator: | 01001000 | 10111000 =
- 11111000
Bit
itwis wise Exc Exclu lusive ive O OR Ope perator: ^ ^
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.
Bit
itwis wise Exc Exclu lusive ive O OR Ope perator: ^ ^ 01110010 ^ 10101010
- 11011000
Unary NOT
OT Op Oper erator: ~
The bitwise NOT, or complement, is a unary
- peration 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
CHEESE = 1 //00000001 SAUSAGE= 2 //00000010 PEPPERONI = 4 //00000100 HAM = 8 //00001000 MUSHROOMS= 16 //00010000 OLIVES = 32 //00100000 ONIONS = 64 //01000000 PEPPERS = 128 //10000000
Se
Setting b bit its
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
Settin
ting bit bits 00000000 //Initial state OR 00000001 //CHEESE bit OR 00000100 //PEPPERONI bit OR 01000000 //ONIONS bit
- 1000101
//Final result
Unsettin
ing bits bits
To unset a bit, you must combine two
- perators, AND and NOT. NOT will reverse
the bits in the flag, and AND will unset the
- ne 0 bit while leaving the others alone. Let's
say that in the previous example, we wanted to unset the ONIONS bit.
Unsettin
ing bits bits NOT 01000000 //ONIONS bit
- 10111111 //Inverse of ONIONS bit
01000101 //Initial state AND 10111111 //Inverse of ONIONS bit
- 00000101 //ONIONS bit unset
Togglin
ling bit 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.
Togglin
ling bit 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"