Lecture 14 Miscellaneous Topics II: Enuerations, Pointers to Functions (& Prelim Review)

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Lecture 12 Destructors “Absolute C++” Chapters 10.3, 15.2.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Data Types Declarations Expressions Data storage C++ Basics.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Exception Handling How to handle the runtime errors.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
User-Written Functions
Chapter 7 Pointers and C-Strings
Selection (also known as Branching) Jumail Bin Taliba by
C++, OBJECT ORIENTED PROGRAMMING
Pointers and Pointer-Based Strings
C Basics.
Lecture 6 C++ Programming
Pointers and References
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Standard Version of Starting Out with C++, 4th Edition
Pointer Basics Psst… over there.
CS410 – Software Engineering Lecture #3: C++ Basics I
Miscellaneous Topics I
Presentation transcript:

Lecture 14 Miscellaneous Topics II: Enuerations, Pointers to Functions (& Prelim Review)

Enumerations Sometimes, when programming, we need to deal with a limited range of constants: A finite set of colors Result codes It is useful to define a set of constants which can be used in place of the actual integer values: increases readability of code protects you against integer values changing We can do this with constants // Define error codes const short cNoError = 0; const short cBadArg = 1; const short cBadResult = 2; const short cUnknownErr = 3;

Enumerations This is fine, but it means that all functions which deal with these errors simply return (or take as arguments) a short type. If we used an enumeration, we can define a new data type as well as defining constant names. The syntax would look like this: // Define error codes enum RonsError { cNoError = 0, // Values are optional, default is 0 cBadArg, // If a value is not present, cBadResult, // assign previous value + 1 cUnknownErr };

Enumerations Consider the following: enum RonsError { cNoError = 0, // Values are optional, default initial cBadArg, // value is 0 cBadResult, // If a value is not present, cUnknownErr // assign previous value + 1 }; int main() { RonsError rerr = RonsFunction(); // An arbitrary function if (rerr != cNoError) cerr << “Ooooops, Error:“ << rerr << endl; else cout << “No error” << endl; }

Enumerations int main() { RonsError rerr = RonsFunction(); // An arbitrary function if (rerr != cNoError) cerr << “Ooooops, Error:“ << rerr << endl; else cout << “No error” << endl; } The variable rerr is not treated as an integer type. If I try to assign an integer value directly to it, I will get a compile time error. Although I could use a cast to force a value into the enumeration variable. This gives us some protection against accidentally assigning raw integer values to a variable of type RonsError.

Demonstration #1 Enumerations

Pointers to Functions What is a pointer to a function? A pointer just like any other Data pointed at by the pointer is actually machine code for the function pointed at. How is it declared? // Define a pointer to a function int (*f)(int start,int stop); This declares a variable f which is a pointer to a function that returns an int and takes two ints as parameters. Now, just like any other pointer, the declaration does no allocation. So, in this case, f points at nothing and any attempt to dereference it will have very spectacular side effects! You cannot dynamically allocate memory for function pointers.

Pointers to Functions You can only set pointer-to-function variables equal to pointers to existing functions. How do you do that? Consider the following code: int SimpleAdd(int arg1,int arg2) { return arg1 + arg2; } int main() { int (*f)(int start,int stop); f = SimpleAdd; // f now points at the function “SimpleAdd” // What can we do with it now? }

Pointers to Functions We can call it! How do you call a function when you have a pointer to it? Consider the following: int SimpleAdd(int arg1,int arg2) { return arg1 + arg2; } int main() { int (*f)(int start,int stop); f = SimpleAdd; int x = (*f)(3,4); // Call the function pointed at by f int y = f(3,4); cout << “x is : “ << x << endl; }

Demonstration #1 Function Pointers

Pointers to Functions OK, interesting concept. But what use is it? Most frequently used to allow a programmer to pass a function to another function. Suppose I am writing a function which contains variables which need to be acted on. Suppose that I want to be able to have multiple ways to act on those variables. A function pointer as a parameter is a good solution. Quick Case Study: The Project SALSA environment has multiple layers: VCSAPI (server communication, file downloads) File Delivery Layer (versioning logic, GUI code) Runway (front end)

Pointers to Functions Whenever we download a file we want to provide a friendly progress bar. The problem is that the file download actually happens in the VCSAPI which has no GUI code in it at all! The solution is that the VCSAPI call which actually downloads a file take a function pointer as a parameter : typedef short (*VCSAPI_ProgressCallback)(short); VCSAPI_Error VCSAPI_FileGet(VCSAPI_Server server, VCSAPI_FileRecord *fileRecPtr, VCSAPI_ProgressCallback prog, short callbackInterval, char *localPath, int *serverRC,char **serverMSG) The FDL (which manages all of the GUI code for versioning dialogs, etc.) can pass a pointer to a function which updates a graphical progress bar.

Lecture 14 Final Thoughts