Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.

Slides:



Advertisements
Similar presentations
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 18 Templates, Part II. From Last Time: What is a Template? This is the “official” specification for a template. It says that to define a template.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Templates.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Computer Science II Exam I Review Monday, February 6, 2006.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Pointers Applications
Chapter 15: Operator Overloading
IT PUTS THE ++ IN C++ Object Oriented Programming.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
'C' Bitwise Operators Relevance to 'C' of bitwise applications Syntax and expressions Example getter and setter functions Eratothene's prime number sieve.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
TK1924 Program Design & Problem Solving Session 2011/2012
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Andy Wang Object Oriented Programming in C++ COP 3330
Classes C++ representation of an object
Chapter 13: Overloading and Templates
2 Chapter Classes & Objects.
Template Classes and Functions
University of Central Florida COP 3330 Object Oriented Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Compilation and Debugging
Compilation and Debugging
University of Central Florida COP 3330 Object Oriented Programming
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Object Oriented Programming COP3330 / CGS5409
CISC/CMPE320 - Prof. McLeod
C Structures, Unions, Bit Manipulations and Enumerations
Name: Rubaisha Rajpoot
Andy Wang Object Oriented Programming in C++ COP 3330
Classes C++ representation of an object
Templates Generic Programming.
COP 3330 Object-oriented Programming in C++
C++ Object Oriented 1.
Presentation transcript:

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 work in a similar manner to the function template.  To make a class into a template, prefix the class definition with the syntax: template

 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

 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.  mples/templates/simplelist3/ 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 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.

 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:  mples/templates/tlist/ mples/templates/tlist/

  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 ◦ cpp : a main program that uses the sort function.  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) ◦ 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) ◦ cpp : sample main program that uses the PFArrayBak template. Illustrates class features with an instantiation using type string.

 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.

 Bitwise AND Operator: & & =

 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.

 Bitwise OR Operator: | | =

 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.

 Bitwise Exclusive OR Operator: ^ ^

 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

CHEESE = 1 // SAUSAGE= 2 // PEPPERONI = 4 // HAM = 8 // MUSHROOMS= 16 // OLIVES = 32 // ONIONS = 64 // PEPPERS = 128 //

 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

 Setting bits //Initial state OR //CHEESE bit OR //PEPPERONI bit OR //ONIONS bit //Final result

 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.

 Unsetting bits NOT //ONIONS bit //Inverse of ONIONS bit //Initial state AND //Inverse of ONIONS bit //ONIONS bit unset

 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.

 Toggling bits //Initial state XOR //ONIONS bit //ONIONS bit toggled "off" //Initial state XOR //ONIONS bit //ONIONS bit toggled "on"