Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Advertisements

Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
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.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
1 Introduction to Object Oriented Programming Chapter 10.
Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Recursion ● Recursion is a computer programming technique that allows programmers to divide problems into smaller problems of the same type. ● You can.
Recursion Powerful Tool
Motivation for Generic Programming in C++
Pointer to an Object Can define a pointer to an object:
C ++ MULTIPLE CHOICE QUESTION
Procedural and Object-Oriented Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Programming with ANSI C ++
Pointers and Linked Lists
Pointers and Linked Lists
Andy Wang Object Oriented Programming in C++ COP 3330
CS 215 Final Review Ismail abumuhfouz Fall 2014.
C++ Templates.
Chapter 18 Introduction to Custom Templates
C++ INTERVIEW QUESTIONS
CIS 200 Test 01 Review.
Introduction to C++ Systems Programming.
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 45
Andy Wang Object Oriented Programming in C++ COP 3330
Exceptions, Templates, and the Standard Template Library (STL)
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Why exception handling in C++?
Introduction to Classes
Chapter 5 Classes.
Indexer AKEEL AHMED.
Object Oriented Programming COP3330 / CGS5409
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Learning Objectives What else in C++ Bitwise operator
Introduction to Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 9 Classes: A Deeper Look, Part 1
Pointers and Linked Lists
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Exceptions and Templates
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Pointers & Dynamic Data Structures
Exceptions, Templates, and the Standard Template Library (STL)
Introduction to Data Structure
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Classes C++ representation of an object
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Review for Midterm 3.
More C++ Classes Systems Programming.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Final Review Andy Wang Object Oriented Programming in C++ COP 3330

Bitwise Operators Built-in operators that allow accessing and manipulating of individual bits Necessary because smallest variables that can be created are at least 1 byte Accessing individual bits can be useful for making more efficient algorithms, or using less storage space

The Bitwise Operators Bitwise &. Bitwise | Bitwise ^ (exclusive OR) Performs the & operation on individual bits (1 is true, 0 is false) Bitwise | Performs the | operation on individual bits Bitwise ^ (exclusive OR) XOR is true if there is exactly one true and one false Complement ~ Reverses the bits of a variable (1 -> 0, 0 -> 1)

The Bitwise Operators << (left shift) >> (right shift) Shifts the bits of a variable to the left >> (right shift) Shifts the bits of a variable to the right

Accessing Individual Bits Understand the concept of a bit mask and how to create one Understand how to do these basic operations on a single bit from a variable, without changing the other bits stored in the variable Set a bit to 1 Unset a bit (set to 0) Flip a bit to its opposite Query a bit (find out what it is—1 or 0) You should be able to identify or derive how the above operations are done on a single bit, with a combination of an appropriate operation and a bit mask

Templates Function templates Functions that can work with multiple parameter types Compiler builds a separate function for each needed type An easy way to create overloaded functions without writing individual version Operations performed inside the function need to be valid for types used in the calls

Class Templates Extension of function template idea Allows creation of a generic class, where the type of item stored can vary Again, operations used inside the class need to be valid for any type used to instantiate the class Each member function written as a function template

Template Declarations and Object Instantiations Use keyword template, along with template parameters in angle brackets <> template<class T> template<typename T> Examples stack<double> myStack; stack<int> stack2(10):

Data Structures Basic data structure types Stack—first in last out structure Queue—first in first out structure Vector—storage of a list using array-based storage Linked-list—storage of a list in a linear format using self- referential objects A self-referential object contains data, along with one+ pointers, which point to other objects of the same type Each node of a linked list stores a piece of data, and points to the next node in the list Tree—non-linear storage of a set of data using self- referential objects Each node in a tree has 2+ pointers to other nodes Binary tree is good for soring and searching

Data Structures Implementing basic data structures Encapsulation of data structure inside an object means details can be handled internally Outside access through simpler interface Often involves pointers and dynamic memory allocation Templates are commonly used, to make data structures more general Some structures can be implemented with others Through inheritance or composition A stack can be implemented with a linked list of a vector

Exception Handling A method of error-handling Good for processing errors that must be handled in places other than where they occurred Good for handling errors from libraries or other components Should not be used for general program control (confusing to the reader)

Syntax try { } Label used for a block that encloses an area where exception might be thrown throw Used to throw an exception Can throw an item like a variable or an object throw DivideByZeroException() Also used to build a throw list void function(int x) thorw (ThisException, ThatException)

Syntax catch { } The catch blocks immediately follow the try blocks Can have more than one Each catch can take one parameter, indicating the type of exception Special catch block catch(…) { } Will catch any thrown exception Useful for a default case—a “catch all”

Recursion Recursive function Recursion vs. iteration A function that calls itself Recursion vs. iteration Some example algorithms Factorial, Fibonnaci, GCD, sorting, binary search

Miscellaneous Conditional compilation #define SYMBOL #ifdef SYMBOL Brings the symbol into existence #ifdef SYMBOL Do something #ifndef SYMBOL #endif

Code Writing Test Format Specifications Main program Sample execution output Your job Write the declarations and definition files

Example Main Program #include <iostream> #include “flex.h” using namespace std; int main() { Flex a, b(“Merry”), c(“Christmas”); cout << a << ‘,’ << b << ‘,’ << c << endl; b.cat(a); cout << b << endl; b.cat(c); c.cat(c); c.cat(c); cout << c << endl; return 0; }

Example Output * *,*Merry*,*Christmas* *Merry * *Merry Christmas* *ChristmasChristmasChristmasChristmas*

Specifications Objects of class Flex allow a variable length string to be maintained When the constructor for Flex is provided a c-string as a parameter, the Flex object created will have that string value. If no parameter is provided, a default string consisting of exactly one space should be created. Flex should have an overload of the output operator that will display the string surrounded by stars Flex also has a void function, cat, having one reference parameter of type Flex. The function cat should append the string in that parameter to the end of the Flex object invoking cat

Specifications There is no established bound on the size of a Flex object, so dynamic storage allocation should be used All Flex member data is private Show the content in flex.h and flex.cpp It is okay to use the cstring library functions