CISC/CMPE320 - Prof. McLeod

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (23 left – as of last night). Are you able.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Java Primer 1: Types, Classes and Operators
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155: Meet your TA.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
8 Pointers.
Pointers and References
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
C++ Simple Data Types Simple types Integral Floating
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Windows Programming Lecture 02.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
Built-In (a.k.a. Native) Types in C++
Pointers and Pointer-Based Strings
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 TA names and s have been added to the course web site.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
C Structures, Unions, Bit Manipulations and Enumerations
Overloading functions
QUIZ.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CISC/CMPE320 - Prof. McLeod
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 5/8/2019 CISC/CMPE320 TA names and contact information have been added to the Instructor page of the course web site. Reminders: Please fill out the “Programming and Teamwork Background” survey in onQ (117 out of 139 so far). Survey will close next Friday. No tutorial next week – it will start in week 3. You will then know your assigned team and who your mentor is. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Today C++ Types and their behaviour, Cont.: TestTypes Demo auto keyword and decltype() function Literal Suffixes Enums Constants Casting in C++ References and Pointers Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod TestTypes Demo Your compiler will determine the amount of memory allocated to the numeric types. Use the sizeof() function to find out. Do integers wrap around as they do in Java? See the demo… Note that there is some variation on these types depending on the compiler you are using. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Use of auto C++11 Instead of stating a type during declaration, you can use auto instead. The type for your variable is inferred from the rest of the declaration. Not useful for normal declarations, but: More useful in for/each loops, and When obtaining a return value from a templated function, when the returned type is not obvious. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Use of auto, Cont. C++14 Now you can use auto as a return type in a function header, and with: Lambda function return types. This is called “return type deduction”… Fall 2018 CISC/CMPE320 - Prof. McLeod

Use of the decltype() Function You supply an argument typed using auto and then the return from this function can be used to type another variable. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Literal Suffixes C++11 C++14 Suffixes can be appended to literals (strings or numbers) to modify their type. C++11 allowed the user to create suffixes. C++14 has added some to the standard library: "s", for creating the various std::basic_string types. "h", "min", "s", "ms", "us", "ns", for creating the corresponding std::chrono::duration time intervals. "if", "i", "il", for creating the corresponding std::complex<float>, std::complex<double> and std::complex<long double> imaginary numbers. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Literal Suffixes, Cont. For example: auto aTime = 60s; aTime will be of type chrono::seconds. Fall 2018 CISC/CMPE320 - Prof. McLeod

Aside - Divide by Zero Example How does Eclipse/GCC handle a divide by zero? Check out TestZeroDivide.cpp. How does Java handle a divide by zero? Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Enumerated Types Useful in supplying cases for switch statements, or building a bunch of constants Example: enum MonthLengths = {JAN = 31, FEB = 28, MAR = 31, APR = 30, MAY = 31, JUN = 30, JUL = 31, AUG = 31, SEP = 30, OCT = 31, NOV = 30, DEC = 31}; The members of the enum have integer values and appear as named constants. If you do not assign values, they are given incremental values starting from zero. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Constants Prefix the type declaration with the keyword const. By convention, use all capital letters for the name of the constant. For example: const double CM_IN_INCH(2.54); You must assign a value at the time of declaration. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod The void Type Used to indicate that a function does not return anything. Aside - can also be used to declare a pointer to anything: void* Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Casting Numeric Types In C++ you can cast between types in six different ways: (type)expresssion type(expression) static_cast<type>(expression) const_cast<type>(expression) dynamic_cast<type>(expression) reinterpret_cast<type>(expression) Where type is the desired type and expression is what you are casting. C style – don’t use! Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Aside - C Style Casts These are crude and too powerful. For example: The cast will remove any const property. The cast does not check to see if the cast is even possible or if it makes sense at all (like casting a string to an int). Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod static_cast Casting Best, general purpose technique for atomic types. For example: int aVal = static_cast<int>(4.8); aVal contains 4 – casting truncates, not rounds. You don’t always have to cast. For example an int can always be stored in a double because of implicit casting or “type coercion”: double dVal(5); Fall 2018 CISC/CMPE320 - Prof. McLeod

References and Pointers, First Pass… Pointers are not references and references are not pointers! A reference is an “alias for its initializer”. [From “C++ Gotchas” by Stephen C. Dewhurst] The alias must refer to something. You cannot have a null reference or a reference to void or even a reference to a reference. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Pointers A pointer is a variable that stores a memory address. It occupies memory (a reference may not occupy any memory). (How much memory?) If the memory address was obtained using the & operator (“address of”) on another variable, then the pointer indirectly references the value contained by the other variable. Accessing this value through the pointer (using de-referencing) is called indirection. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Pointers, Cont. In Java, pointers are implicit – you have no control over them. In C and C++ pointers are explicit, you have complete control! Pointers can be NULL (or better yet, use nullptr) You can even have such a thing as void* as a pointer type. (Kinda gross…) C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod & and * Operators LHS: When creating a type, & gives you a reference to a type, * gives you a pointer to a variable of that type. RHS: In an expression, & is the “address of” operator yielding the memory address of a variable. * is “de-referencing”. When used with a pointer it yields the value of the variable being pointed to. Fall 2018 CISC/CMPE320 - Prof. McLeod

References and Pointers, Cont. This gets even more interesting when moving things in and out of functions. Later… Pointers tend to be overused in C++ code – try to use a reference first. See TestSimplePointers.cpp Fall 2018 CISC/CMPE320 - Prof. McLeod

Pointers, Review Questions I can have many pointers aliased to a fundamental type variable, does this mean that an int is an Object, for example? What is stored in a pointer? What does it mean to “de-reference” a pointer, and how do you do it? If I add one to a pointer (pointer arithmetic), then how many bytes are added to the memory address and how does the system know? Fall 2018 CISC/CMPE320 - Prof. McLeod

Pointers, Review Questions Cont. I can use pointer arithmetic to access memory positions away from the original pointer: Can I access any memory position I want? Can I access and change any memory position I want? Is this a good idea? How much memory does a pointer occupy? How do you get the address of where the pointer is stored? Am I using 32 bit pointers on a 64 bit machine? Why? Fall 2018 CISC/CMPE320 - Prof. McLeod

Aside – Declaring Multiple Variables This is when you do something like: int x, y, z; But this does not work with operators. For example: int* x, y; x is a pointer, but y is an int Also, auto does not work with multiple declarations. Fall 2018 CISC/CMPE320 - Prof. McLeod

Aside - typedef Keyword If you prefix a type with the typedef keyword, then you can create a synonym for the type. For example: typedef long double dprecision; Now, you can use dprecision instead of using long double for a type. Fall 2018 CISC/CMPE320 - Prof. McLeod