Download presentation
Presentation is loading. Please wait.
Published byPeregrine Ryan Modified over 9 years ago
1
Object Oriented Programming COP3330 / CGS5409
2
Class Templates Bitwise Operators
3
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
4
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: className ::memberName
5
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/ http://www.cs.fsu.edu/~myers/cop3330/exa mples/templates/simplelist3/
6
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
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 x; // etc. The compiler still does it by building multiple versions of the class, for each instantiated type This means syntax like List ::function() is necessary on definitions, since List is a different class than List, for example.
8
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/exa mples/templates/tlist/ http://www.cs.fsu.edu/~myers/cop3330/exa mples/templates/tlist/
9
http://www.cs.fsu.edu/~myers/savitch3c++/Ch16/ 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.
11
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 AND Operator: & 01001000 & 10111000 = -------- 00001000
13
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 OR Operator: | 01001000 | 10111000 = -------- 11111000
15
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 Exclusive OR Operator: ^ 01110010 ^ 10101010 -------- 11011000
17
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
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
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
Setting bits 00000000 //Initial state OR 00000001 //CHEESE bit OR 00000100 //PEPPERONI bit OR 01000000 //ONIONS bit ------------ 1000101 //Final result
21
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
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
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
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"
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.