Implementing Abstractions

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

QUIZ Friends class Y;

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

CS 230 Programming Languages

The C++ Object Lifecycle. EECS 211 Winter 2019

Dynamic Memory Allocation

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

And Even More and More C++ Fundamentals of Computer Science

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Pointers, Dynamic Data, and Reference Types

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Pointers II. Class 31

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

Pointers and Terminal Control

CS201 Some Important Definitions

Dynamic Allocation in C

Midterm Review. PIC 10B Spring 2018

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

04-17 Discussion Notes

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

05-01 Discussion Notes

Launchpad Lecture -10

StackVsHeap SPL/2010 SPL/20

CSE 303: Concepts and Tools for Software Development

Classes and Pointers: Some Peculiarities


Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Assignment 1: grid. Due November 20, 11:59 PM Introduction

COMP6771 Advanced C++ Programming

CS1150 Principles of Computer Science Methods

Dynamic Data Structures

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

Chapter 10 Introduction to Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

Written by John Bell for CS 342, Spring 2018

+ Abstract Data Types

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Object-Oriented Programming

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Object Oriented Programming COP3330 / CGS5409

Exam 3 Chapters 7 & 9

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

CSC 1214: Object-Oriented Programming

CS24 Week 3 Lecture 1

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?


Dynamic Allocation in C

C++ for Java Programmers

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Lecture08: Scope and Lexical Address

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

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

CS-201 Introduction to Programming with Java

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Programming Abstractions

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CSCI 262 Data Structures. The Big 3. Copy Constructor. Destructor. Assignment Operator 3/4/ The Big 3

Lecture 18 Tao Wang 1

Project Compiler. CS031 TA Help Session November 28, 2011

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

G Programming Languages - Fall 2012

CS1150 Principles of Computer Science Methods

Memory management COSC346

An Introduction to C++

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Name, Scope, and Binding. Outline [1]

COMP 2355 Introduction to Systems Programming

Short Notes of CS201

Homework #3 CS2255 Fall 2012

Object-oriented Programming. Object-oriented Programming

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

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

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

CS201 - Introduction to Programming Glossary By

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Inheritance. Transitivity

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Programming, numerics and optimization

COMP6771 Advanced C++ Programming

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Fundamental Concepts and Definitions

MIDI plugins with JUCE

Today's Topics. CISC 458 Winter J.R. Cordy

C++ : Object Oriented Features. What makes C++ Object Oriented

Object-Oriented Programming, Iouliia Skliarova

Smart Pointers. Some slides from Internet

Intermediate Programming, Spring 2017*

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Advances in Programming Languages: Regions

Supporting Class / C++ Lecture Notes

Transcription:

Implementing Abstractions

Pointers A pointer is a C++ variable that stores the address of an object. Given a pointer to an object, we can get back the original object. Can then read the object's value. Can then write the object's value. Think of a pointer as a URL for the object.

Pointers Setting up a pointer requires two steps: Declare the pointer variable. Initialize the pointer variable to refer to some object. These are all separate steps, and forgetting any one can result in hard-tofind bugs. Once the pointer is set up, we can use it to read and write the object it refers to.

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n;

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n;

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n;

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 137 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 42 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 2718 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 2718 *ptr1 = 2718; *ptr2 = *ptr1; ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 2718 2718 *ptr1 = 2718; *ptr2 = *ptr1; m = 160; 2 ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 160 2718 *ptr1 = 2718; *ptr2 = *ptr1; m = 160; 2 ptr1 = ptr2; *ptr1 = m / n; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 160 2718 *ptr1 = 2718; *ptr2 = *ptr1; m = 160; 2 ptr1 = ptr2; ptr1 = ptr2; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 160 2718 *ptr1 = 2718; *ptr2 = *ptr1; m = 160; 2 ptr1 = ptr2; ptr1 = ptr2; ptr1 ptr2

Pointers, Visually int m = 137; int n = 42; int* ptr1 = &m; int* ptr2 = &n; m n 160 2718 *ptr1 = 2718; *ptr2 = *ptr1; m = 160; 2 ptr1 = ptr2; ptr1 = ptr2; ptr1 ptr2 Assigning Assigning one one pointer pointer to to another another changes changes which which object object is is being being pointed pointed at. at. It It does does not not change change the the value value of of the the pointee. pointee.

Why would we ever want to do this?

Allocating Multiple Objects One of the most important applications of pointers is dynamic memory allocation, the ability to construct brand-new objects at runtime. To allocate an array of n objects of type T, use the syntax new T[n] This returns a pointer to the array of elements you have just allocated.

Dynamic Memory Allocation int* ptr; ptr = new int; *ptr = 137;

Dynamic Memory Allocation int* ptr; ptr = new int;? *ptr = 137; ptr

Dynamic Memory Allocation int* ptr; ptr = new int[5];? *ptr = 137; ptr

Dynamic Memory Allocation int* ptr; ptr = new int[5];? *ptr = 137; ptr???????????????

Dynamic Memory Allocation int* ptr; ptr = new int[5]; *ptr = 137; ptr???????????????

Dynamic Memory Allocation int* ptr; ptr = new int[5]; ptr[0] = 137; ptr???????????????

Dynamic Memory Allocation int* ptr; ptr = new int[5]; ptr[0] = 137; ptr 137????????????

Dynamic Memory Allocation int* ptr; ptr = new int[5]; ptr[0] = 137; ptr[2] = 42; ptr 137????????????

Dynamic Memory Allocation int* ptr; ptr = new int[5]; ptr[0] = 137; ptr[2] = 42; ptr 137??? 42??????

Notes on Dynamic Arrays Arrays in C++ do not know their own size. You must store this separately. Arrays in C++ do not have boundschecking. You must make sure not to read off the end of the array. Arrays in C++ cannot be resized.

Cleaning Up Unlike other languages like Java, in C++, you are responsible for deallocating any memory allocated with new[]. You can deallocate memory with the delete[] operator: delete[] ptr; This destroys the array pointed at by the given pointer, not the pointer itself.

Cleaning Up Unlike other languages like Java, in C++, you are responsible for deallocating any memory allocated with new[]. You can deallocate memory with the delete[] operator: delete[] ptr; This destroys the array pointed at by the given pointer, not the pointer itself. ptr 137 42 42

Cleaning Up Unlike other languages like Java, in C++, you are responsible for deallocating any memory allocated with new[]. You can deallocate memory with the delete[] operator: delete[] ptr; This destroys the array pointed at by the given pointer, not the pointer itself. ptr 137 42 42

Cleaning Up Unlike other languages like Java, in C++, you are responsible for deallocating any memory allocated with new[]. You can deallocate memory with the delete[] operator: delete[] ptr; This destroys the array pointed at by the given pointer, not the pointer itself. ptr???

Words of Caution C++ has few of the safety features present in Java. All of the following result in undefined behavior in C++: Reading or writing through a pointer that you haven't initialized. Reading or writing through a pointer to memory that you have deallocated. Reading off the end of an array. Treating a non-array like an array.

Implementing Stack

Implementing Stack Last time, we saw how to implement RandomBag in terms of Vector. We could also implement Stack in terms of Vector. What if we wanted to implement the Stack without relying on any other collections? Let's build the stack directly!

Storing Values Right now, if we need to store multiple values, we can Declare a whole bunch of variables, Use a collections class, or Dynamically allocate space.

Storing Values Right now, if we need to store multiple values, we can Declare a whole bunch of variables, Use a collections class, or Dynamically allocate space.

Storing Values Right now, if we need to store multiple values, we can Declare a whole bunch of variables, Use a collections class, or Dynamically allocate space.

An Initial Idea A bounded stack. Allocate a fixed-size array for elements. Add elements to the array when they're pushed. Remove elements from the array when they're popped. Report an error if we exceed the size of the array.

An Initial Idea element array allocated length logical length 4 0

An Initial Idea 137 element array allocated length logical length 4 1

An Initial Idea 137 42 element array allocated length logical length 4 2

An Initial Idea 137 42 2718 element array allocated length logical length 4 3

An Initial Idea 137 42 2718 512 element array allocated length logical length 4 4

An Initial Idea 137 42 2718 element array allocated length logical length 4 3

An Initial Idea 137 42 element array allocated length logical length 4 2

An Initial Idea 137 42 161 element array allocated length logical length 4 3

An Initial Idea 137 42 161 314 element array allocated length logical length 4 4

Let's Code it Up!

Constructors A constructor is a special member function used to set up the class before it is used. The constructor is automatically called when the object is created. Syntax: The constructor for a class named ClassName has signature ClassName(args);

Destructors A destructor is a special member function responsible for cleaning up an object's memory. Automatically called when a local variable goes out of scope. Automatically called if you delete a pointer to an object. Syntax: The constructor for a class named ClassName has signature ~ClassName();