Download presentation
Presentation is loading. Please wait.
1
Lesson 26 Miscellaneous Topics
CMSC 202 Lesson 26 Miscellaneous Topics
2
Warmup Decide which of the following are legal statements: int a = 7;
const int b = 6; int * const p1 = & a; int * const p2 = & b; const int * p3 = & a; const int * p4 = & b; const int * const p5 = & a; const int * const p6 = & a; p1 = & a; p3 = & a; p5 = & a;
3
Announcements Last “regular” class day Review on Tuesday Homework
Bring Review Questions! Project 4 Coming Soon! Project 5 Regrades ONLY if ABSOLUTELY necessary ONLY if it will affect your course grade
4
Today Overload the dereferencing operator
Weirdness of the * operator! Bit-wise operators (important for 341) Binary representation Binary addition Bit-masking ~&, |, ^, << and >>
5
Disclaimer The material I am about to present is an advanced concept from 341 The 341 book (Weiss) actually has it WRONG! Short write-up with some good code Linked from the Slides webpage Topic: Overloading Pointer Dereferencing Overloading Conversion Operator Oooo, Aaaah
6
Pointer Dereferencing
Problem: Imagine we want to create a templated Sort What if we have a collection of pointers? template < class T > void MySort( vector<T> &collection ) { /* code that sorts collection has something like: */ if (collection.at(i) < collection.at(j)) swap(collection.at(i), collection.at(j)); } // In main… vector<int*> vec; srand(0); for (int i = 0; i < 1000; ++i) vec.push_back(new int(rand())); Solution: We already saw auto_ptr Roll our own Pointer<T> class What happens when we compare two items of type int* ?
7
Our Pointer<T> class
template <class T> class Pointer { public: Pointer(T *rhs = NULL ) : pointee(rhs) {} bool operator<( const Pointer & rhs ) const return *pointee < *rhs.pointee; } private: T* pointee; }; What if we want to print the pointee? What if we want to change its value?
8
Overloading Pointer Dereferencing
template <class T> class Pointer { public: Pointer(T *rhs = NULL ) : pointee(rhs) {} bool operator<( const Pointer & rhs ) const return *pointee < *rhs.pointee; } // Pointer dereferencing operator const T operator * () const return *pointee; private: T* pointee; };
9
Using the Pointer Dereference
template <class T> ostream& operator <<(ostream &sout, Pointer<T> p) { sout << *p << endl; return sout; } Dereferencing a class that overloads the pointer dereferencing operator – calls that method! “Smart” pointers
10
Overloading Conversion Operator
template <class T> class Pointer { public: Pointer(T *rhs = NULL ) : pointee(rhs) {} bool operator<( const Pointer & rhs ) const return *pointee < *rhs.pointee; } // Conversion operator operator const T * () const return pointee; private: T* pointee; }; This looks very similar… What’s the difference? Position of the word ‘operator’ Operator name is: const T*
11
Differences… // Pointer dereferencing operator
const T operator * () const { return *pointee; } // Conversion operator operator const T * () const return pointee; Dereferencing Returns an object of type T (after dereferencing the data member!) Conversion Converts something of type Pointer into something of type const T* (before dereferencing the data member!)
12
Final Notes about * If both dereferencing and conversion are overloaded… Dereferencing operator takes precidence (put in some cout statements to verify this!) Conversion operator Can be used to convert between ANY two types! Cool! Good examples in below material Additional Resources ANSI/ISO C++ Professional Programmer's Handbook C++ Annotations Version 6.1.2 C/C++ Pointers
13
8 0 3 610 Decimal Numbers = 800010 + 3010 + 610 = 803610 Humans
Represent everything in decimal, 1 -> 10 Base 10 notation Each position is a power of 10 Base 10: count by 10’s 8 * 103 0 * 102 3 * 101 6 * 100 = =
14
Binary Numbers Computers Represent everything in binary, 1’s and 0’s Base 2 notation Each position is a power of 2 Base 2: count by 2’s 1 * 23 0 * 22 1 * 21 1 * 20 = = 1110
15
Binary Numbers Usually represented in sets of 4 digits
4, 8, 16, 32, etc. Bit Binary digit Byte Collection 8-bits Integers stored in 4 bytes or 32 bits 32-bits Can represent up to values Two other common programming formats Octal – base 8 Has digits 0->7 Hexadecimal – base 16 Has digits 0->9 and A->F
16
Binary Representations
What decimal equivalent are the following binary numbers? 0001 0100 1000 1001 1100 1111 0101
17
We leave off base subscript if the context is clear…
Binary Addition Just like decimal addition Except == 102 Carry a 1 when you add two or more 1’s Let’s try a simple one… In decimal? 1 1 1001 9 + 0011 + 3 1 1 12 We leave off base subscript if the context is clear…
18
Binary in C++ Why do we care? That’s great, but why do we REALLY care?
Binary describes size of data Integer stored in 32-bits, limited to ~5 billion values (or 232-1) - ~2.7 billion -> ~2.7 billion That’s great, but why do we REALLY care? Assume lots of boolean values… Can use each bit to represent a separate value! Compress data, optimize data access Get at “raw” data Look for these again in Hash Tables!
19
Bit-wise Operators A 1010 B 1100 ~A 0101 ~B 0011 A & B 1000 A & A
Operate on each bit individually…. ~ Bit-wise not 1 becomes 0 0 becomes 1 & Bit-wise logical and 1 if both 1 0 otherwise | Bit-wise logical or 1 if either or both 1 A 1010 B 1100 ~A 0101 ~B 0011 A & B 1000 A & A A | B 1110 A | A
20
More Bit-wise Operators
^ Bit-wise exclusive or 1 if either but not both 1 0 otherwise << N Bit-wise left shift Moves all bits to the left N places Shifts on a zero on the right Left-most bit(s) discarded >> N Bit-wise right shift Moves all bits to the right N places Shifts on a zero on the left Right-most bit(s) discarded A 1010 B 1100 A ^ B 0110 A ^ A 0000 A << 1 0100 A >> 2 0010 B << 1 1000 B >> 2 0011
21
Bit-wise Compound Assignment
&= & and assign |= | and assign ^= ^ and assign <<= << and assign >>= >> and assign
22
Bit Masking So, have a bunch of boolean values to store
Often called “flags” Need two things: Variable to store value in Bit-mask to “retrieve” or “set” the value Use characters – unsigned value char flags = 0; // binary: 0000 char flag4 = 8; // binary: 1000 char flag3 = 4; // binary: 0100 char flag2 = 2; // binary: 0010 char flag1 = 1; // binary: 0001
23
Bit Masking Operations
// Set flag1 to “true” flags = flags | flag1; // 0000 | 0001 // Set flag1 to “false” flags = flags & ~flag1; // 0001 & 1110 // Set several flags to “true” flags = flags | flag1 | flag3; // 0000 | 0001 | 0100 // Set all flags to “false” flags = flags ^ flags; // 0101 ^ 0101 // Set to a specific value flags = 11; // 1011 // Set all flags to “true” flags = flags | ~flags; // 1011 | 0100
24
Practice Convert the following decimal digits into binary
7, 5 Add them together using binary addition Check your result using decimal What about these two numbers? 9, 13 Use only 4-bits to represent these numbers What about negative numbers?
25
Challenge Use bit-wise operators to implement binary addition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.