# COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Save this PDF as:

Size: px
Start display at page:

Download "COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26"

## Transcription

1 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26

2 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g. loops), but we haven t learned anything that would help us to develop clean and readable code. Most algorithms include steps that only require a cursory understanding at that level of discourse Of course, eventually, everything must be completely and unambiguously defined We achieve this via procedural abstraction: defining single actions that actually represent more complicated sequences of actions In some languages, we might call them methods. We won t be using that term yet. Brock University (Week 3) Procedural Abstraction 2 / 26

3 Procedures, and functions, and methods, oh my! Are we familiar with these terms? Do we know the difference between them? A procedure is a sequence of imperative instructions that can be collectively aliased (i.e. invoked) via a call to a single term or phrase A function is comparable to a procedure, but, upon completion, returns a value to the context that invoked it A method could be a function method, or a procedure method (often simply called function or method, respectively). What matters is that it s an action that s directly tied to an object/class Once we get to OO, methods will sometimes also be known as member functions Some schools of thought will dictate that procedures tied to objects should change state, while functions should ask questions of the objects In C (and C++, outside of methods), it s not unheard of to simply refer to functions. Brock University (Week 3) Procedural Abstraction 3 / 26

4 How do we start? A procedure is pretty simple to write. All you need is: [ m o d i f i e r ] ( r e t u r n t y p e ) name ( [ p a r a m e t e r s ] ) { s t a t e m e n t l i s t } e.g. v o i d wokka ( ) { i n t d e a l i e =8; cout<< G r o b b l e g r o b b l e g r o b b l e! <<e n d l ; } Why void? Because that s how you say you re not returning anything! Brock University (Week 3) Procedural Abstraction 4 / 26

5 Parameters Parameters allow you to modify the behaviour of an action. Parameters declared within the procedure header are also known as formal parameters; when you invoke the procedure, the arguments you provide are the actual parameters The actual parameters must match the formal parameters Of course, casting/coercion can help with this Unlike with some languages, you can t omit any, or provide them out of sequence Brock University (Week 3) Procedural Abstraction 5 / 26

6 Invoking procedures from other procedures Can we call one procedure from within another? Well, obviously we can, but there s something special we need to worry about that may not be a concern for some other languages We re going to need to try an example. Brock University (Week 3) Procedural Abstraction 6 / 26

7 Whu... what?!? There are two different kinds of compilers: one-pass, and two-pass. A two-pass compiler first goes through the source files, collecting tokens, and building up a table of references. It then does a second pass to connect the dots By comparison, a one-pass compiler only goes through each source file once, building up its tables as it goes. If it hasn t seen a term by the time it s needed, it simply doesn t exist So... what s the solution? Brock University (Week 3) Procedural Abstraction 7 / 26

8 Forward declarations Of course, more often than not, you could simply ensure that you declared every procedure before you needed it. But what would happen if you had two procedures, a and b, with a calling b, and b calling a? Clearly, we simply need to cut the cake before we serve it! We don t actually need both definitions simultaneously; we simply need to know their signatures The solution is a forward declaration All we need to do is to write the procedure s header, with a semicolon. We can provide the body later Let s revisit the example. Brock University (Week 3) Procedural Abstraction 8 / 26

9 Headers The only problem is that, for larger source files, starting with a couple dozen prototypes can be a bit inelegant. More importantly, once we get to using multiple files and linking, we won t want to have to copy all of them over to every connected file. We can write a header file, which is just a minimal source file that (if used appropriately) doesn t define any behaviours Primarily, we use them for function prototypes, but we can also define constants and types (once we get to that) We use a preprocessor directive (#include "filename.h") to include it Let s wait until the next slide for an example. Brock University (Week 3) Procedural Abstraction 9 / 26

10 Functions If a procedure has a void return type, then a function has... well, pretty much anything else We ve already defined what a function is. All it takes is to change the return type, and include a return statement Quick example time? Brock University (Week 3) Procedural Abstraction 10 / 26

11 return statements There are two odd points worth knowing about the return statement: If you neglect to include the return statement, even with a function that promised a particular return type, it ll still compile (and run) However, there s no legitimate reason to do this, and the program won t be as readable You can include a return statement, even for a procedure A common use for this is if you might want to preemptively leave a procedure, but don t want to indent the rest of the procedure for the else (Yes, it s example time again) Brock University (Week 3) Procedural Abstraction 11 / 26

12 Overloading functions If you want to define the same behaviour more than once, with the only difference being the types, then you can overload the function names Just declare and define more than one copy of the same name, with only the signatures varying Be very careful with these. Coercion can already match with different signatures; ambiguous calls may not compile at all (Are we tired of examples yet? Because... example time) Brock University (Week 3) Procedural Abstraction 12 / 26

13 Speaking of returns... You might sometimes need a function that can return more than one value. Tough Brock University (Week 3) Procedural Abstraction 13 / 26

14 No multiple returns? Unfortunately, C++ actually doesn t directly provide a mechanism for multiple returns. That said, we have multiple alternatives available to us (of varying levels of usefulness): Once we learn how to create records (e.g. struct), you could have a single return that can contain multiple values inside C++ provides a tuple in its standard library for effectively the same use In theory, you could allocate an array, and provide it as a parameter to the function The function would then modify the contents of the array to match the data to be returned Generally, this isn t going to be practical However, it s immensely common as a mechanism for providing a buffer (e.g. for receiving multiple bytes from an input stream) What if we could simply use a parameter as a return? Brock University (Week 3) Procedural Abstraction 14 / 26

15 Types of parameters For programming languages in general, there are different potential types of parameters: In Parameters for receiving arguments Out Parameters allows a procedure to return values directly through the parameter In/Out Parameters can be used either way Ada, for example, is very good for labelling these explicitly. But, how does this help us in C++? Brock University (Week 3) Procedural Abstraction 15 / 26

16 Values, pointers, and references First, let s briefly discuss the difference between pass-by-value and pass-by-reference. This is where we re particularly glad we re using C++ (even over C). A pointer is a special variable that contains an address as its stored value Pointers can be dereferenced to access (or modify) the value referenced by the pointer Other than still technically being a number (meaning you can perform arithmetic on it), pointers are mostly analogous to Java references C++ also provides actual references A reference allows two names (variables/parameters) to share the same place in memory Both pointers and references require accessing the address of the corresponding variable The two different techniques are best understood by demonstrating the difference. Brock University (Week 3) Procedural Abstraction 16 / 26

17 Usage Suggestions There s a school of thought that functions should only request values, and procedures should change state. A side effect occurs when a function changes the state of the system Side effects are sometimes discouraged for functions, if it can make it harder to predict state just from reading the source An idempotent function is one that always produces the same result when it receives the same parameters Idempotent functions can have side effects, so long as those side effects don t change the behaviour of successive calls e.g. adding a value to an array at a specific position On the other hand, a push into a stack is not Brock University (Week 3) Procedural Abstraction 17 / 26

18 Variadic functions Sometimes you may not know exactly how many parameters a function will have. Common examples include formatted print statements, or functions that select a single element from an arbitrary number of options (e.g. max). We re actually not going to get into this yet, because C++ introduced a better approach, but it relies on templates (which is a few weeks away) However, if you find yourself needing to do it in the meantime, you can always use C s vararg feature Brock University (Week 3) Procedural Abstraction 18 / 26

19 Constants We ve already talked about symbolic constants (#define) and const, but there s still one more term: constexpr A constexpr defines a compile-time constant Technically you can define constant values, but there s little reason to choose that over a const constexpr can also be used to define simple functions, so long as they can be fully-defined at compile-time You may need to explicitly tell the compiler to use the C++11 standard (or higher) e.g. g++ -std=c++11 -o out file.cpp Example time? Example time. Brock University (Week 3) Procedural Abstraction 19 / 26

20 Inline functions Recall how function invocation works: it requires allocating memory for the call frame, jumping to a different place in the code, etc. It s slower than simply having the equivalent instructions in-place. Actually, you can do that. Prepending the inline keyword before a function acts as a hint to the compiler that it may explicitly expand the the function, as though its lines had been written directly Typically, it ll run slightly faster, and take up slightly more space Brock University (Week 3) Procedural Abstraction 20 / 26

21 Scope revisited Now that we ve looked at functions and pointers, let s revisit scope and extent a bit. Let s review a sample (which is technically different from an example). Brock University (Week 3) Procedural Abstraction 21 / 26

22 Recursion Thankfully, there are pretty much no special considerations for writing recursive solutions in C/C++. However, we do remember what a recursive function is, and how it works, right? Because we ll need that later. Brock University (Week 3) Procedural Abstraction 22 / 26

23 Arrays We ll get to dynamic allocation and ragged arrays later, but there s no reason not to start using arrays. Allocate an array as such: int name[size]; You can pre-initialize them: int name[3]={val1,val2,val3}; You don t need to specify the size when pre-initializing Be careful relying on the sizeof function for array sizes You can do interesting things by casting one array type into another When declaring a variable-length automatic array as a parameter, you can only leave the number of rows blank 2D (or more) arrays simply require additional square brackets Note: Because of how arrays work, you can also reference them with a pointer of the base type. Up to you if we want to look at this together. Brock University (Week 3) Procedural Abstraction 23 / 26

24 Strings There are two different major string options available: C used a char array (or char pointer) C++ defines a class Both have their own string libraries. C++ strings are pretty good, but, on occasion, you ll be forced to use the older C-style. Brock University (Week 3) Procedural Abstraction 24 / 26

25 Command-line parameters Recall from the other week that the main function accepts two parameters: the number of command-line parameters, and a (C-style) string array containing the parameters The first (index 0) entry in the array will be the program name, including path Since they re strings, you ll need to do convert them if you want to accept numbers You can use the atoi (or atof, or atol) function provided by the cstdlib library Brock University (Week 3) Procedural Abstraction 25 / 26

26 Questions? Have we had enough examples? Because we can totally come up with more. Brock University (Week 3) Procedural Abstraction 26 / 26

### 6. Pointers, Structs, and Arrays. March 14 & 15, 2011

March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

### Short Notes of CS201

#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

### CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

### In Java we have the keyword null, which is the value of an uninitialized reference type

+ More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

### QUIZ. What are 3 differences between C and C++ const variables?

QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

### 6. Pointers, Structs, and Arrays. 1. Juli 2011

1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

### Chapter 1 Getting Started

Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

### These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

### Lab 9: Pointers and arrays

CMSC160 Intro to Algorithmic Design Blaheta Lab 9: Pointers and arrays 3 Nov 2011 As promised, we ll spend today working on using pointers and arrays, leading up to a game you ll write that heavily involves

### Memory and Pointers written by Cathy Saxton

Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.

### Intro. Scheme Basics. scm> 5 5. scm>

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

### MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

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

QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

### Functions and Recursion

Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function

### Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

### Chapter 7 Functions. Now consider a more advanced example:

Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done

### This section provides some reminders and some terminology with which you might not be familiar.

Chapter 3: Functions 3.1 Introduction The previous chapter assumed that all of your Bali code would be written inside a sole main function. But, as you have learned from previous programming courses, modularizing

### Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

### Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns What is an array? Pointers Memory issues The name of the array is actually a memory address. You can prove this by trying to print

### Introduction to C: Pointers

Introduction to C: Pointers Nils Moschüring PhD Student (LMU) Nils Moschüring PhD Student (LMU), Introduction to C: Pointers 1 1 Introduction 2 Pointers Basics Useful: Function

### Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

### Sixth lecture; classes, objects, reference operator.

Sixth lecture; classes, objects, reference operator. 1 Some notes on the administration of the class: From here on out, homework assignments should be a bit shorter, and labs a bit longer. My office hours

### CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 8 1.4 What is a Program Made of? 14 1.5 Input,

### Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

### 7.1 Optional Parameters

Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default

### calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

### G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

### Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

### Lecture 10: building large projects, beginning C++, C++ and structs

CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

### Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

### Chapter 2. Procedural Programming

Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

### Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

### Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 1 slide 2/43

### CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

### Object-Oriented Programming

- oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

### PIC 10A Objects/Classes

PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

### Tokens, Expressions and Control Structures

3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

### Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

### CS558 Programming Languages

CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

### Creating a C++ Program

Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

### the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Lecture 7 Lecture 7 So You Think You Know C++ Most of you are experienced Java programmers Both in 2110 and several upper-level courses If you saw C++, was likely in a systems course Java was based on

### Pointers, Arrays and Parameters

Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming

### Motivation was to facilitate development of systems software, especially OS development.

A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

### Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be

### The compiler is spewing error messages.

Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

### Interview Questions of C++

Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

### Reliable C++ development - session 1: From C to C++ (and some C++ features)

Reliable C++ development - session 1: From C to C++ (and some C++ features) Thibault CHOLEZ - thibault.cholez@loria.fr TELECOM Nancy - Université de Lorraine LORIA - INRIA Nancy Grand-Est From Nicolas

### Getting started with C++ (Part 2)

Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

### Lecture Notes on Memory Layout

Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

### Inheritance, Polymorphism and the Object Memory Model

Inheritance, Polymorphism and the Object Memory Model 1 how objects are stored in memory at runtime? compiler - operations such as access to a member of an object are compiled runtime - implementation

### CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

CS 220: Introduction to Parallel Computing Input/Output Lecture 7 Input/Output Most useful programs will provide some type of input or output Thus far, we ve prompted the user to enter their input directly

### C++ Primer for CS175

C++ Primer for CS175 Yuanchen Zhu September 10, 2014 This primer is pretty long and might scare you. Don t worry! To do the assignments you don t need to understand every point made here. However this

### Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

### Programming in C - Part 2

Programming in C - Part 2 CPSC 457 Mohammad Reza Zakerinasab May 11, 2016 These slides are forked from slides created by Mike Clark Where to find these slides and related source code? http://goo.gl/k1qixb

### Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other

### Lecture 16 CSE July 1992

Lecture 16 CSE 110 28 July 1992 1 Dynamic Memory Allocation We are finally going to learn how to allocate memory on the fly, getting more when we need it, a subject not usually covered in introductory

### Intermediate Programming, Spring 2017*

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

### FUNCTIONS POINTERS. Pointers. Functions

Functions Pointers FUNCTIONS C allows a block of code to be separated from the rest of the program and named. These blocks of code or modules are called functions. Functions can be passed information thru

### CS 161 Computer Security

Wagner Spring 2014 CS 161 Computer Security 1/27 Reasoning About Code Often functions make certain assumptions about their arguments, and it is the caller s responsibility to make sure those assumptions

### Ch. 11: References & the Copy-Constructor. - continued -

Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

### The first program: Little Crab

Chapter 2 The first program: Little Crab topics: concepts: writing code: movement, turning, reacting to the screen edges source code, method call, parameter, sequence, if-statement In the previous chapter,

### Variables and literals

Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

### CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

### pointers + memory double x; string a; int x; main overhead int y; main overhead

pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a;

### Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

### Final CSE 131B Spring 2004

Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

### 1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

### CS 11 C track: lecture 5

CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

### Goals of this Lecture

C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing

### Computer Systems Lecture 9

Computer Systems Lecture 9 CPU Registers in x86 CPU status flags EFLAG: The Flag register holds the CPU status flags The status flags are separate bits in EFLAG where information on important conditions

### News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, 107-111,

### CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

### Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Our programs are littered with function calls like f (x, 5). This is a way of passing information from the call site (where the code f(x,5) appears) to the function itself. The parameter passing mode tells

### Lecture 3. Lecture

True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design

### COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

### 09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

1 TYPE CHECKING AND CASTING Lecture 5 CS2110 Spring 2013 1 Type Checking 2 Java compiler checks to see if your code is legal Today: Explore how this works What is Java doing? Why What will Java do if it

### Data Types The ML Type System

7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

### Memory and C++ Pointers

Memory and C++ Pointers C++ objects and memory C++ primitive types and memory Note: primitive types = int, long, float, double, char, January 2010 Greg Mori 2 // Java code // in function, f int arr[];

### Lab # 02. Basic Elements of C++ _ Part1

Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

### Procedures, Parameters, Values and Variables. Steven R. Bagley

Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

### Java Bytecode (binary file)

Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

### The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs. Item 47: Use traits classes for information about types. The

### JAVA An overview for C++ programmers

JAVA An overview for C++ programmers Wagner Truppel wagner@cs.ucr.edu edu March 1st, 2004 The early history James Gosling, Sun Microsystems Not the usual start for a prog.. language Consumer electronics,

### CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 220: Introduction to Parallel Computing Beginning C Lecture 2 Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 2 Today s Schedule

### Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

C/C++ Notes C/C++ Coding Style Guidelines -0 Ray Mitchell C/C++ Notes 0 0 0 0 NOTE G. C/C++ Coding Style Guidelines. Introduction The C and C++ languages are free form, placing no significance on the column

### G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

### assembler Machine Code Object Files linker Executable File

CSCE A211 Programming Intro What is a Programming Language Assemblers, Compilers, Interpreters A compiler translates programs in high level languages into machine language that can be executed by the computer.

### PIC 10A. Review for Midterm I

PIC 10A Review for Midterm I Midterm I Friday, May 1, 2.00-2.50pm. Try to show up 5 min early so we can start on time. Exam will cover all material up to and including todays lecture. (Only topics that

### Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

Managing Memory (and low level Data Structures) Lectures 22, 23 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/spring_2015/csc1254.html Programming Principle of the Day Avoid Premature

### Object-Oriented Design (OOD) and C++

Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign

### Programming in C. Pointers and Arrays

Programming in C Pointers and Arrays NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Pointers and Arrays In C, there is a strong relationship

### CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CISC-124 20180122 Today we looked at casting, conditionals and loops. Casting Casting is a simple method for converting one type of number to another, when the original type cannot be simply assigned to

### Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: C and Unix Overview This course is about computer organization, but since most of our programming is

### Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

### Understand Execution of a Program

Understand Execution of a Program Prof. Zhang September 17, 2014 1 Program in Memory When you execute a program, the program (i.e., executable code) is loaded into the memory (i.e., main memory, RAM) in

### CS201 Latest Solved MCQs

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

### School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL