CISC/CMPE320 - Prof. McLeod

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Meet with your team later today in ELL321 at 6:30pm. Teams are listed on the Project page of the course.
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 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 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Bill Tucker Austin Community College COSC 1315
Data types Data types Basic types
Chap. 2. Types, Operators, and Expressions
Motivation and Overview
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
CISC124 Labs start this week in JEFF 155: Meet your TA.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Precedence and Associativity
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
7 Arrays.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
All the Operators 22-Nov-18.
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
All the Operators 4-Dec-18.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter-3 Operators.
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:
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CISC/CMPE320 - Prof. McLeod
7 Arrays.
Comp Org & Assembly Lang
All the Operators 6-Apr-19.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Herbert G. Mayer, PSU CS Status 7/19/2015
CMPE212 – Reminders Course Web Site:
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
C++ Programming Language Lecture 4 C++ Basics – Part II
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
CISC101 Reminders Assignment 3 due today.
OPERATORS in C Programming
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 2/18/2019 CISC/CMPE320 Teams are being assembled. Once this is complete, I will post the teams, assign the TAs and create Jira accounts and project spaces. Before you meet, there will be some lecture material on “Being Agile” and what you can do in your first team meeting. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Today C++ Types and their behaviour, Cont.: Arrays and C-strings Structs Passing “things” Membership Operators Bitwise Operations Boolean Expressions Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Last Time - Arrays To declare a variable to hold five integers: int anArray[5]; Or you can use an array initializer: int anArray[] = {2, 7, 3, 0, 1}; or int anArray[5] = {2, 7, 3, 0, 1}; Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Arrays, Cont. What is in an uninitialized array? Can I use pointer arithmetic with an array? Can I access values outside the array bounds? Continue with: ArrayExample.cpp vectors are much better to use than arrays – more about this class later… Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Arrays and Strings In C, you had to use char[] to store a string literal: char oldString[] = "Hello there!"; These “C-strings” end with the character '\0', also called the “null character”. Manipulating C-strings means manipulating arrays – generally a real pain… In C++ use the string class instead! Defined in the string library, and uses the std namespace. More about strings later… Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Winter 2014 CISC/CMPE320 2/18/2019 Structures A kind of class. Defined as an aggregate of other types. For example: struct address { string name; int streetNumber; string street; string city; string province; string postalCode; }; Note “;” Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Structures, Cont. See StructureDemo.cpp Demonstrate some other stuff too!: Passing by value. Passing by reference. Passing by constant reference. Passing a pointer. Passing a pointer to a constant. Passing a constant pointer. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Passing… Does it make any sense to pass by constant value? Do you understand the difference between a “pointer to const” and a “const pointer”? It does matter where you position the const when typing a pointer. The best technique to use, if you can, is to pass by constant reference. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Lots of consts: const int* ptr ptr points to an int* variable that is constant. int* const ptr ptr is a constant pointing to an int* variable that is not constant. const int* const ptr ptr is a constant pointing to an int* variable that is also constant. Fall 2018 CISC/CMPE320 - Prof. McLeod

Member Selection Operators The example also demonstrated the use of the two member selection operators: The “dot operator”: object.member De-referencing and membership: pointer->member The latter is the same as: (*pointer).member (“Members” are attributes or methods (or member functions)…) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Structures, Cont. A struct is a primitive object definition with no privacy for it’s members. Why use them in C++? (It was great to use the struct in this demo because it behaved like a very simple class!) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators Discussed in order of highest to lowest precedence. See: http://en.cppreference.com/w/cpp/language/operator_precedence Highest Precedence: Scope resolution (when defining only): class_name::member Or namespace_name::member Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators, Cont. Next Highest Precedence (level 2): Member selection . (see slide 19). Subscripting arrays: pointer[expr] ( ) when used with function calls or value constructors. Post increment or post decrement: var++ var—- Type identification: typeid(type or expr) Casts like static_cast<type>(expr) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Aside – typeid() Used to find out the types of pointers. More useful in a function: A trivial example: int aVal = 100; int* ptr_aVal = &aVal; cout << typeid(ptr_aVal).name() << endl; Displays: Pi Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators, Cont. Level 3 Precedence: sizeof(type) Pre-increment and pre-decrement. Complement: ~expr Not: !expr Negation: -expr Address of and dereference (& and *) The heap operators: new, delete and delete[] C – style cast: (type)expr Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators, Cont. Level 4: Member selection (and dereference) -> Level 5: Multiply, Divide, Modulo: *, /, % Level 6: Add, subtract: +, - Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators, Cont. Level 7: Bitwise shift left: expr << expr Bitwise shift right: expr >> expr Level 8: <, <=, >, >= Level 9: ==, != Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators, Cont. Levels 10 to 12: Bitwise AND: expr & expr Bitwise exclusive OR (or XOR): expr ^ expr Bitwise OR: expr | expr Levels 13 and 14: Logical AND: expr && expr Logical OR: expr || expr Level 15: Conditional Expression: expr ? expr : expr AND has precedence over OR Fall 2018 CISC/CMPE320 - Prof. McLeod

Operators, Cont. (Still!) Level 16: All assignment operators: = *=, /=, %=, +=, -=, <<=, >>=, &=, |=, ^= Level 17: Throw exception: throw expr Lowest Precedence!: Comma: , Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Notes on Precedence Prefix operators (~, !, -, &, *, pre-increment and pre-decrement) and assignment operators are right associative (right to left) – all others are left-associative (left to right). So: int a = b + c + d; Means int a = (b + c) + d; But, int x = y = z; Means int x = (y = z); (Yes, the assignment operator returns something!) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Precedence Notes, Cont. Use ( ) to control precedence. When in doubt, control precedence! If an expression gets too long, use intermediate variables to make it more readable. Fall 2018 CISC/CMPE320 - Prof. McLeod

Bit and Shift Operations These operate on all integer types and enums. Complement, ~, carries out a bitwise negation of each bit (1 becomes 0, 0 becomes 1). Binary &, | and ^: A B A&B A|B A^B 1 Fall 2018 CISC/CMPE320 - Prof. McLeod

Bit and Shift Operations, Cont. The left shift operator, <<, moves all bits in val to the left by n positions: val << n. Zeros are added to the least significant bits. This is the same as multiplying a number by 2n. Right shift, >>, moves all bits to the right. For unsigned integers zeros are added to the most significant bits – same as dividing by 2n. For signed integers, the result is not predicable as the sign bit may be duplicated. Fall 2018 CISC/CMPE320 - Prof. McLeod

Bitwise Operations, Examples Set the nth bit in a number and the rest are zeros: 1 << n To set the nth bit of any number, x, and not change the rest: x = x | 1 << n To check to see if the nth bit is set: if ((x & 1 << n) != 0)… Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Boolean Expressions We have seen the boolean operators already. Here are a few notes: Something like a < b < c will compile and run, but may not produce the desired result. Better to use: a < b && b < c Remember that & and | are bitwise operators, not logical ones. Fall 2018 CISC/CMPE320 - Prof. McLeod

Boolean Expressions, Cont. The && and || logical operators use “short circuit evaluation”: For && if the LHS is false then the RHS is not evaluated. For || if the LHS is true then the RHS is not evaluated. (Same as in Java.) Fall 2018 CISC/CMPE320 - Prof. McLeod

Boolean Expressions, Cont. Non zero integers are treated as being true, and zero is treated as being false. (Ouch!) So, you can use logical operators, && || and !, with integers. For example, the code: int x = 10; if (x) is the same as: if (x != 0) Fall 2018 CISC/CMPE320 - Prof. McLeod

Boolean Expressions, Cont. Also, this is legal syntax: if (x = 10) The assignment operator returns the value being assigned, which in this case is a true! But suppose x is 12 and you really meant to type ==… Ouch, again! Fall 2018 CISC/CMPE320 - Prof. McLeod

Boolean Expressions, Cont. See TestStuff.cpp. Applying ! to a non-zero integer returns false or zero. An if statement will treat a pointer by itself as an integer – it will be true unless it is nullptr. You can also test assignment statements since the assignment operator returns the value being assigned. Fall 2018 CISC/CMPE320 - Prof. McLeod