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.

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
String Escape Sequences
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Beginning C++ Through Game Programming, Second Edition
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Course Web Site: Have you filled out the questionnaire.
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 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CPS120: Introduction to Computer Science Variables and Constants.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
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.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
Basic Types, Variables, Literals, Constants. What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet)
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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.
C++ Lesson 1.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155: Meet your TA.
CISC/CMPE320 - Prof. McLeod
C Basics.
CISC/CMPE320 - Prof. McLeod
Chapter 3: Understanding C# Language Fundamentals
C Structures, Unions, Bit Manipulations and Enumerations
Built-In (a.k.a. Native) Types in C++
C++ Data Types Data Type
Variables, Identifiers, Assignments, Input/Output
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC101 Reminders All assignments are now posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

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 to run a C++ console program in your IDE of choice? Today: –The “Philosophy” of C++ –C++ Types –C++ Atomic Types –Casting

Fall 2015CISC/CMPE320 - Prof. McLeod2 The “Philosophy” of C++ From “The Design and Evolution of C++” by (who else!) Bjarne Stroustrup, 1994: C++ is designed to be a statically typed, general- purpose language that is as efficient and portable as C. C++ is designed to directly and comprehensively support multiple programming styles (procedural programming, data abstraction, object-oriented programming, and generic programming). C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly.

Fall 2015CISC/CMPE320 - Prof. McLeod3 The “Philosophy” of C++, Cont. C++ is designed to be as compatible with C as possible, therefore providing a smooth transition from C. C++ avoids features that are platform specific or not general purpose. C++ does not incur overhead for features that are not used (the "zero-overhead principle"). C++ is designed to function without a sophisticated programming environment.

Fall 2015CISC/CMPE320 - Prof. McLeod4 ISO C++ and C The C++ standard library has incorporated the standard C library with just a few optimizations. C++ also has the “Standard Template Library”, or “STL”, that contains many pre-defined data structures, for example. Generally, well-written C code will run in C++. But some C “things” will not work in C++ (for example, in C you can use new or class as variable names, since they are not C keywords.)

Fall 2015CISC/CMPE320 - Prof. McLeod5 Running a C++ Program Source Code, *.cpp, *.h Machine Code, *.exe Linker Compiler O-Code, *.o CPU Library Code Source Code Editor

Running a C++ Program, Cont. Thankfully, an IDE simplifies this process for you. A project (a set of source code and resource files) is “built” – which consists of both compilation and linking. MinGW (“Minimalist GNU for Windows”), for example uses the g++.exe program for this. The IDE makes up the command line calls to this program. Building creates a *.o file and then a *.exe file. Then you run the *.exe program. Each stage of this process can unearth errors. Fall 2015CISC/CMPE320 - Prof. McLeod6

Who Reports the Errors? An Eclipse pre-compiler does a pretty good job of reporting syntax errors and warnings. Errors may prevent building. You can (but should not!) ignore warnings. Build errors are reported by the compiler and are seen in the console window. Run-time errors are reported by the OS, which is running the *.exe file. Fall 2015CISC/CMPE320 - Prof. McLeod7

Variable Types How many variable types do you have in C? (not counting structs)? You can have the atomic types, pointers and arrays of these types. Anything else? How many types can you have in C++? Infinitely many! Thousands defined in the STL. You can define as many as you want. Oh – the atomic types are still there too… Fall 2015CISC/CMPE320 - Prof. McLeod8

Fall 2015CISC/CMPE320 - Prof. McLeod9 Variable Declaration C++ is declarative and statically typed. ie. “Declare before you use” and “You can’t change the type of a variable, once declared”. Unlike C, you can declare variables anywhere, as long as you satisfy the above. Initialization is good practice, but is optional. Examples: int numStudents(40); // C++ style declare bool flagVal(true); double aNum;

Variable Declaration, Cont. This is what we are used to from Java: int numStudents = 40; bool flagVal = true; For a C++ “style” declaration, use: int numStudents(40); bool flagVal(true); Looks like you are invoking a constructor doesn’t it? Fall 2015CISC/CMPE320 - Prof. McLeod10

Fall 2015CISC/CMPE320 - Prof. McLeod11 Type Categories in C++ Fundamental types: –bool, char, integer types and floating point types Enumerated types. The void type. Pointer types (like int* ) Array types (like int[] ) Reference types (like int& ) Data structures (like vector ) and classes

Fall 2015CISC/CMPE320 - Prof. McLeod12 bool and char Types bool literal values: true and false char literal values: 'H' Escape characters: –\n newline –\t tab –\b backspace –\r carriage return –\a BEL –\? ? –\\ \ –\' ' –\" " –\0 ##octal –\x ##hex

Fall 2015CISC/CMPE320 - Prof. McLeod13 Integer Types Simplify things a bit (forget unsigned …): –short 2 bytes –int 4 bytes –long 4 bytes –long long 8 bytes To get a long long literal append an l or better yet, an L to the end of the number. What is “unsigned” anyways?

Fall 2015CISC/CMPE320 - Prof. McLeod14 Floating Point Types float 4 bytes double 8 bytes long double 12 bytes Literal floating point numbers have a decimal place and/or an exponent: 4.5, 2.3e-7, for example. A float literal has an f or F appended to it. A long double gives you easy access to extended precision numbers.

Fall 2015CISC/CMPE320 - Prof. McLeod15 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 2015CISC/CMPE320 - Prof. McLeod16 Aside - Divide by Zero Example How does Eclipse/GCC handle a divide by zero? Check out TestZeroDivide.cpp. Note: compilation errors and warnings, run-time errors. Note that the run-time error results from the crashing of the executable, and is reported by the OS, not Eclipse. How does Java handle a divide by zero?

Fall 2015CISC/CMPE320 - Prof. McLeod17 Enumerated Types Useful in supplying cases for switch statements. 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 2015CISC/CMPE320 - Prof. McLeod18 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 2015CISC/CMPE320 - Prof. McLeod19 The void Type Used to indicate that a function does not return anything.

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

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 or visa-versa). Fall 2015CISC/CMPE320 - Prof. McLeod21

Fall 2015CISC/CMPE320 - Prof. McLeod22 static_cast Casting Best, general purpose technique for atomic types. For example: int aVal = static_cast (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);

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 2015CISC/CMPE320 - Prof. McLeod23

Pointers A pointer is a variable that stores a memory address. It occupies memory (a reference may not occupy any 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 2015CISC/CMPE320 - Prof. McLeod24

Fall 2015CISC/CMPE320 - Prof. McLeod25 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 point to void. You can have such a thing as void*.

& and * Operators, So Far When creating a type, & gives you a reference to a type, * gives you a pointer to a variable of that type. 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 2015CISC/CMPE320 - Prof. McLeod26

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 2015CISC/CMPE320 - Prof. McLeod27

Fall 2015CISC/CMPE320 - Prof. McLeod28 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? So, 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 2015CISC/CMPE320 - Prof. McLeod29 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?

Fall 2015CISC/CMPE320 - Prof. McLeod30 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

Fall 2015CISC/CMPE320 - Prof. McLeod31 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.