Separate Compilation Model

Similar documents
Pointers, Dynamic Data, and Reference Types

Pointers II. Class 31

Array Elements as Function Parameters

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

a data type is Types

CIS 190: C/C++ Programming. Classes in C++

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

A brief introduction to C++

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

G52CPP C++ Programming Lecture 9

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Introduction to Programming Using Java (98-388)

G52CPP C++ Programming Lecture 13

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Array. Prepared By - Rifat Shahriyar

Programming Abstractions

CS201 Some Important Definitions

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

+ Abstract Data Types

Short Notes of CS201

Memory and Pointers written by Cathy Saxton

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CS201 - Introduction to Programming Glossary By

Introduction to C++ Introduction to C++ 1

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

Introduction to Programming (Java) 4/12

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

COMP 524 Spring 2018 Midterm Thursday, March 1

Chapter 6 Introduction to Defining Classes

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Introduction to C++ with content from

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

Dynamic Memory Allocation

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

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:

CSE 374 Programming Concepts & Tools

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

An Introduction to C++

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

Chapter 3 Function Overloading

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

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

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CE221 Programming in C++ Part 1 Introduction

Multimedia-Programmierung Übung 3

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

Dynamic arrays / C Strings

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CSE 303: Concepts and Tools for Software Development

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS 376b Computer Vision

Binding and Variables

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Lecture 2, September 4

Engineering Problem Solving with C++, Etter

Memory and C++ Pointers

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Linked Memory. Pointers Linked Lists. September 21, 2017 Cinda Heeren / Geoffrey Tien 1

Lecture 15a Persistent Memory & Shared Pointers

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming with C++ as a Second Language

Chapter 5 Names, Bindings, Type Checking, and Scopes

Pointers and References

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

CSI33 Data Structures

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

377 Student Guide to C++

C11: Garbage Collection and Constructors

Polymorphism. Arizona State University 1

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

Topic 9: Type Checking

Topic 9: Type Checking

Bruce Merry. IOI Training Dec 2013

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

AN OVERVIEW OF C++ 1

COEN244: Class & function templates

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

C10: Garbage Collection and Constructors

More class design with C++ Starting Savitch Chap. 11

LECTURE 5 Control Structures Part 2

CA31-1K DIS. Pointers. TA: You Lu

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Transcription:

Separate Compilation Model Recall: For a function call to compile, either the function s definition or declaration must appear previously in the same file. Goal: Compile only modules affected by recent changes. Technique: Separate implementation from interface. Files will #include header files for other modules. implementation file (foo.cpp) Function definitions Class method implementations (later in our course) header file (foo.h) Function declarations Class declarations (later in our course) 1

Example: arithmetic.cpp arithmetic.h trianglemath.cpp arithmetic.cpp may be used in MANY other modules arithmetic.h will be #included 2

Visual C++ Express Download Free: http://www.microsoft.com/express/downloads/#2010-visual-cpp WARNING: Not 100% compatible with g++ compiler, which we will use for testing! Press F5 to save, compile and run in debugger Debugger is very good! (Let s try it ) Also demonstrate SSH secure shell For Mac: CyberDuck? 3

Variables: Java Review Java gives us exactly 2 choices: 1. primitive int x; 2. reference to object Cat y; y = new Cat(); y = new Cat( Morris, 17.5); // default Cat 4

C++ 4 kinds of Variables 1. primitives essentially like Java 2. objects Cat y; // Instantiates a default Cat! Cat z( Morris, 17.5); These variables are not pointers and they are not references. They hold objects. They cannot be null. The object is destroyed automatically when the variable goes out of scope. 5

C++ 4 kinds of Variables 3. Pointers Similar to C Can point to a primitive, an object, or another pointer Can point to something on the Stack or on the Heap int x = 7; Cat y; // Remember, y IS a Cat! int *p = &x; Cat *q = &y; Cat **z = &q; 6

C++ 4 kinds of Variables 4. References The variable refers to something, but doesn t actually hold the value. Typically used for function parameters. Kind of like Java references, but different (see next slide ) Can refer to a primitive, an object, or a pointer. Can refer to something on the Stack or on the Heap. int x = 7; Cat y; int *p = &x; int &r = x; // r is a reference to a primitive Cat &s = y; // s is a reference to an object int *&t = p // t is a reference to a pointer Note: The symbol & is NOT being used as the address operator here! Note: The symbol = is NOT being used as the assignment operator here! 7

Reference Variables: C++ vs. Java Ways C++ references are different: You use & to declare them Can refer to primitives and pointers (not just objects) Can refer to something on the heap, but USUALLY refer to something on the stack You MUST initialize it when declared (no null reference possible) Once declared, you CANNOT change it refer to a different entity. (The entity may mutate, however.) Assignment operator copies data, does NOT move the reference elsewhere 8

Example: references.cpp What is the output? int a = 7; int &b = a; b = 4; cout << a << endl; int c = 6; b = c; b++; cout << a << b << c << endl; 9

Strings C-style strings (null-terminated char arrays) still available: char *x = hello ; Literals like hello are always C-style strings Much easier/safer to use C++ string class: string x( hello ); More convenient than Java String class because operators are overloaded (==, <=, >=, +, etc. Code example later ) Important: C++ string objects are mutable! 10

Memory Maps: Java vs. C++ Consider Java code: String x( hello ); String y; y = x; y = hello ; // local variable on stack, // object on the heap // local variable // aliasing // new object created, // reference y is moved Now let s look at the exact same code in C++ 11

Memory Maps: C++ string x( hello ); // object instantiated on the stack string y; // empty string instantiated // Remember: y IS the string! y = x; // COPYING THE DATA!!! // The existing string y is mutated Important Concept: In C++ the assignment operator copies the state from one object to another! y = hello ; // Mixing types! The assignment // operator has been overloaded // to do what we want here // Again, the string y is mutated. 12

Example: stringexample.cpp Review of memory map issues Assignments Using square brackets [] Comparisons (==, <, >, etc.) 13