Getting Deeper into. C++ Semantics. EECS 230 Winter 2018

Similar documents
CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Lab Instructor : Jean Lai

CSE 303: Concepts and Tools for Software Development

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lecture 04 FUNCTIONS AND ARRAYS

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Fast Introduction to Object Oriented Programming and C++

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

Study Guide for Test 2

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Computer Programming

CSE 230 Intermediate Programming in C and C++ Functions

Ch. 10: Name Control

Ch 6. Functions. Example: function calls function

6.096 Introduction to C++

CSCE 314 Programming Languages. Type System

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

QUIZ. What is wrong with this code that uses default arguments?

CAP Fundamentos de Programação Estruturada Aula 4. Lubia Vinhas

2. Functions I: Passing by Value

Computer Science II Lecture 2 Strings, Vectors and Recursion

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Topics. Functions. Functions

CS558 Programming Languages

Programmazione. Prof. Marco Bertini

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Syntax Errors; Static Semantics

10. Functions (Part 2)

PIC 10A Objects/Classes

CS11 Intro C++ Spring 2018 Lecture 3

Chapter 4 Computation

4. Structure of a C++ program

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Friend Functions and Friend Classes

Methods: A Deeper Look

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Fundamental Concepts and Definitions

CSI33 Data Structures

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

More loops Ch

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Lesson 13 - Vectors Dynamic Data Storage

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Weeks 6&7: Procedures and Parameter Passing

Your First C++ Program. September 1, 2010

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Getting started with C++ (Part 2)

Recap: Functions as first-class values

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Global & Local Identifiers

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Organizing Programs and Data 2. Lecture 7 Hartmut Kaiser hkaiser/fall_2012/csc1254.

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

CE221 Programming in C++ Part 1 Introduction

CS24 Week 3 Lecture 1

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

EL6483: Brief Overview of C Programming Language

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

G Programming Languages - Fall 2012

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

10. Functions II. Stepwise Refinement. Example Problem. Stepwise Refinement. A simple technique to solve complex problems

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

W3101: Programming Languages C++ Ramana Isukapalli

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Object-Oriented Principles and Practice / C++

Chapter 4. Computation. Bjarne Stroustrup.

Computer Science II Lecture 1 Introduction and Background

Lecture 7. Log into Linux New documents posted to course webpage

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Functions and Recursion

Chapter 7 - Notes User-Defined Functions II

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CMSC 330: Organization of Programming Languages

CS302 - Data Structures using C++

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Chapter 3: Modules. Starting Out with Programming Logic & Design. Second Edition. by Tony Gaddis

C++ Primer for CS175

Arrays in C++ Instructor: Andy Abreu

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Transcription:

Getting Deeper into C++ Semantics EECS 230 Winter 2018

2 The plan On language technicalities Declarations definitions headers scope Functions declarations & definitions formal and actual parameters pass by value, reference, and const reference Namespaces using declarations

3 Language technicalities C++ is a necessary evil:

3 Language technicalities C++ is a necessary evil: Evil: because language details can distract from our main task: programming

3 Language technicalities C++ is a necessary evil: Evil: because language details can distract from our main task: programming Necessary: because you can t program without a language

3 Language technicalities C++ is a necessary evil: Evil: because language details can distract from our main task: programming Necessary: because you can t program without a language This week we learn grammar and vocabulary

4 Technicalities Like in natural language (e.g., English): Don t get hung up on minor issues There s more than one way to say everything Most design and programmimg concepts are universal, but technicalities are particular

5 Declarations A declaration introduces a name into a scope (region of code): gives a type for the named object sometimes includes an initializer must come before use

5 Declarations A declaration introduces a name into a scope (region of code): gives a type for the named object sometimes includes an initializer must come before use Examples: int a = 7; int b; vector<string> c; double my_sqrt(double);

6 Headers Declarations are frequently introduced through headers: int main() { cout << "Hello, world!\n"; Error: unknown identifier cout

6 Headers Declarations are frequently introduced through headers: #include eecs230.h int main() { cout << "Hello, world!\n"; eecs230.h has declarations for cout, operator<<, etc.

7 Definitions A declaration that (also) fully specifies the declarandum is a definition.

7 Definitions A declaration that (also) fully specifies the declarandum is a definition. Examples: int a = 5;

7 Definitions A declaration that (also) fully specifies the declarandum is a definition. Examples: int a = 5; int b; // but why?

7 Definitions A declaration that (also) fully specifies the declarandum is a definition. Examples: int a = 5; int b; // but why? vector<double> v;

7 Definitions A declaration that (also) fully specifies the declarandum is a definition. Examples: int a = 5; int b; // but why? vector<double> v; double square(double x) { return x x;

7 Definitions A declaration that (also) fully specifies the declarandum is a definition. Examples: int a = 5; int b; // but why? vector<double> v; double square(double x) { return x x; struct Point { int x, y; ;

7 Definitions A declaration that (also) fully specifies the declarandum is a definition. Examples: int a = 5; int b; // but why? vector<double> v; double square(double x) { return x x; struct Point { int x, y; ; Examples of non-definition declarations: extern int b; double square(double); struct Point;

8 Declarations and definitions declarations definitions may be repeated yes no must come before use yes no

9 Why both? To refer to something, we need only its declaration We can hide its definition, or save it for later In large programs, declarations go in header files to ease sharing

10 Declaration example double my_sqrt(double input, double epsilon) {. int main() { my_sqrt(d, eps)

10 Declaration example int main() { my_sqrt(d, eps) // unknown identifier double my_sqrt(double input, double epsilon) {.

10 Declaration example double my_sqrt(double, double); int main() { my_sqrt(d, eps) double my_sqrt(double input, double epsilon) {.

11 Library declaration example In my_math.h: double my_sqrt(double, double); In my_math.cpp: #include my_math.h double my_sqrt(double input, double epsilon) { In some other.cpp source file: #include my_math.h int f() { my_sqrt(d, eps)

12 Scope A scope is a region of program text: global scope (outside any language construct) [namespace scope (outside everything but a namespace)] [class scope (inside a class or struct)] local scope (between { and braces; includes function scope) statement scope (loop variable in a for) They nest!

12 Scope A scope is a region of program text: global scope (outside any language construct) [namespace scope (outside everything but a namespace)] [class scope (inside a class or struct)] local scope (between { and braces; includes function scope) statement scope (loop variable in a for) They nest! Useful because: Declarations from outer scopes are visible in inner scopes Declarations from inner scopes are not visible in outer scopes (Exception: class stuff)

13 Scope example int number_of_bees = 0; void increase_bees(); // global scope visible everywhere // also global scope void buzz(int n) // buzz is global, n is local to buzz { if (number_of_bees > n) { cout << 'b'; for (int i = 0; // i has statement scope i < number_of_bees; ++i) cout << 'z'; increase_bees();

13 Scope example int number_of_bees = 0; void increase_bees(); // global scope visible everywhere // also global scope void buzz(int n) // buzz is global, n is local to buzz { if (number_of_bees > n) { cout << 'b'; for (int i = 0; // i has statement scope i < number_of_bees; ++i) cout << 'z'; increase_bees();

13 Scope example int number_of_bees = 0; void increase_bees(); // global scope visible everywhere // also global scope void buzz(int n) // buzz is global, n is local to buzz { if (number_of_bees > n) { cout << 'b'; for (int i = 0; // i has statement scope i < number_of_bees; ++i) cout << 'z'; increase_bees();

13 Scope example int number_of_bees = 0; void increase_bees(); // global scope visible everywhere // also global scope void buzz(int n) // buzz is global, n is local to buzz { if (number_of_bees > n) { cout << 'b'; for (int i = 0; // i has statement scope i < number_of_bees; ++i) cout << 'z'; increase_bees();

13 Scope example int number_of_bees = 0; void increase_bees(); // global scope visible everywhere // also global scope void buzz(int n) // buzz is global, n is local to buzz { if (number_of_bees > n) { cout << 'b'; for (int i = 0; // i has statement scope i < number_of_bees; ++i) cout << 'z'; increase_bees();

14 Local scope is local Variable names declared in different scopes refer to different objects: bool is_even(int n) { return n % 2 == 0; bool is_odd(int n) { return n % 2 == 1; void swap(int& a, int& b) { int n = a; a = b; b = n; There are three unrelated objects named n above

14 Local scope is local Variable names declared in different scopes refer to different objects: bool is_even(int n) { return n % 2 == 0; bool is_odd(int m) { return m % 2 == 1; void swap(int& a, int& b) { int n = a; a = b; b = n; There are three unrelated objects named n above

14 Local scope is local Variable names declared in different scopes refer to different objects: bool is_even(int n) { return n % 2 == 0; bool is_odd(int m) { return m % 2 == 1; void swap(int& a, int& b) { int temporary = a; a = b; b = temporary; There are three unrelated objects named n above

14 Local scope is local Variable names declared in different scopes refer to different objects: bool is_even(int n) { return n % 2 == 0; bool is_odd(int m) { return m % 2 == 1; void swap(int& n, int& m) { int temporary = n; n = m; m = temporary; There are three unrelated objects named n above

15 Guidelines for scope Declare variables in the smallest possible scope Global variables: In general: avoid if you can In EECS 230: avoid (because you can)

16 Recap: Why functions? Chunk a program into manageable pieces ( divide and conquer ) Map to our understanding of the problem domain Make the program easier to read Facilitate code reuse Ease testing and distribution of labor

17 Function syntax Declaration: return_type fun_name(type1 formal1, type2 formal2, ); (You can omit formal argument names in a declaration)

17 Function syntax Declaration: return_type fun_name(type1 formal1, type2 formal2, ); (You can omit formal argument names in a declaration) Definition: return_type fun_name(type1 formal1, type2 formal2, ) { stm; return expr;

18 Using a function If a function is defined thus: ret f(type1 formal1, type2 formal2, ) body (where body is a block or try-catch block)

18 Using a function If a function is defined thus: ret f(type1 formal1, type2 formal2, ) body (where body is a block or try-catch block) then calling it as f(actual1, actual2, ) means: 1. run its body with actual1 copied to formal1, actual2 copied to formal2, etc. 2. replace the function call with the value returned by the function

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2));

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << dist(1, 2, 4, -2) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(square(x1 x2) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(square(1 x2) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(square(1 4) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(square(-3) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(({ double f = -3; return f f; ) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(({ double f = -3; return -3-3; ) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(({ double f = -3; return 9; ) + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(9 + square(y1 y2)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(9 + square(4)); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(9 + 16); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return sqrt(25); ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << ({ double x1 = 1; double y1 = 2; double x2 = 4; double y2 = -2; return 5; ) << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { cout << 5 << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { // printed 5 cout << '\n';

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { // printed 5\n cout;

19 Example of using a function double square(double f) { return f f; double dist(double x1, double y1, double x2, double y2) { return sqrt(square(x1 x2) + square(y1 y2)); int main() { // printed 5\n

20 Parameter modes Ordinary parameters (e.g., f(int x, string s)) get a copy of the actual argument object: The callee can make changes, but the caller won t see them.

20 Parameter modes Ordinary parameters (e.g., f(int x, string s)) get a copy of the actual argument object: The callee can make changes, but the caller won t see them. Reference parameters (e.g., f(int& x, string& s)) get a reference to the actual argument object: The callee can make changes, and the caller will see them.

Parameter modes Ordinary parameters (e.g., f(int x, string s)) get a copy of the actual argument object: The callee can make changes, but the caller won t see them. Reference parameters (e.g., f(int& x, string& s)) get a reference to the actual argument object: The callee can make changes, and the caller will see them. Const reference parameters (e.g., f(const int& x, const string& s)) get an unchangeable reference to the actual argument object: The callee cannot make changes, but no copying is necessary. 20

21 Pass by copy vs. pass by reference int f (int a) { a = a + 1; return a; int g(int& a) { a = a + 1; return a; int main() { int x = 0; y = 0; cout << "f(x) = " << f(x) << '\n'; cout << "g(y) = " << g(y) << '\n'; cout << "x = " << x << '\n'; cout << "y = " << y << '\n'; What gets printed?

22 Parameter modes decision tree if (the function wants to change the parameter) { pass by reference else if (the parameter is small, like an int or double) { pass by copy (i.e., an ordinary parameter) else { pass by const reference

23 References aren t just for arguments vector<string> v; for (string s : v) for (string& s : v) for (const string& s : v) // copies each element // doesn t copy // and doesn t allow changes

24 Namespaces What if Anne and Bob both want a function swizzle(int)? namespace anne { void swizzle(int); namespace bob { void swizzle(int);

24 Namespaces What if Anne and Bob both want a function swizzle(int)? namespace anne { void swizzle(int); namespace bob { void swizzle(int); From outside, we can refer to anne::swizzle and bob::swizzle

25 using What if we almost always want anne::swizzle and are tired of typing anne:: over and over?

25 using What if we almost always want anne::swizzle and are tired of typing anne:: over and over? Two options: A using declaration brings one name into scope: using anne::swizzle;

25 using What if we almost always want anne::swizzle and are tired of typing anne:: over and over? Two options: A using declaration brings one name into scope: using anne::swizzle; A using directive brings a whole namespace into scope: using namespace anne;

25 using What if we almost always want anne::swizzle and are tired of typing anne:: over and over? Two options: A using declaration brings one name into scope: using anne::swizzle; A using directive brings a whole namespace into scope: using namespace anne; The C++ standard library is defined in namespace std, so ordinarily you d refer to std::cout, std::string, etc.

25 using What if we almost always want anne::swizzle and are tired of typing anne:: over and over? Two options: A using declaration brings one name into scope: using anne::swizzle; A using directive brings a whole namespace into scope: using namespace anne; The C++ standard library is defined in namespace std, so ordinarily you d refer to std::cout, std::string, etc. But eecs230.h contains a using namespace std; directive (Rude! Don t put using directives in headers.)

26 Next time Defining your own types!