CS24 Week 3 Lecture 1

Similar documents
CS24 Week 4 Lecture 1

CSE 303: Concepts and Tools for Software Development

Common Misunderstandings from Exam 1 Material

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

EL2310 Scientific Programming

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

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

Intermediate Programming, Spring 2017*

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Chapter 1: Object-Oriented Programming Using C++

CS11 Intro C++ Spring 2018 Lecture 1

Pointers and References

CS24 Week 4 Lecture 2

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Pointers, Dynamic Data, and Reference Types

Fast Introduction to Object Oriented Programming and C++

Inheritance, and Polymorphism.

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

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Interview Questions of C++

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

Object-Oriented Principles and Practice / C++

EL2310 Scientific Programming

Vector and Free Store (Vectors and Arrays)

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

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

EL2310 Scientific Programming

Short Notes of CS201

Lecture 15a Persistent Memory & Shared Pointers

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

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

CS201 - Introduction to Programming Glossary By

Memory and Pointers written by Cathy Saxton

CSE 333 Lecture 9 - intro to C++

CSC1322 Object-Oriented Programming Concepts

Lecture 14: more class, C++ streams

CS11 Introduction to C++ Fall Lecture 1

Object-Oriented Programming for Scientific Computing

CS

Classes in C++98 and C++11

CS3157: Advanced Programming. Outline

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

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS93SI Handout 04 Spring 2006 Apr Review Answers

Introduction Of Classes ( OOPS )

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

CPSC 427: Object-Oriented Programming

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Lecture on pointers, references, and arrays and vectors

Computer Programming

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

COMP6771 Advanced C++ Programming

Fundamentals of Programming. Lecture 19 Hamed Rasifard

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

An Introduction to C++

! 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)

Vector and Free Store (Pointers and Memory Allocation)

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

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Introducing C++ to Java Programmers

Engineering Tools III: OOP in C++

Object Reference and Memory Allocation. Questions:

! 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)

Documentation. Programming / Documentation Slide 42

CS11 Advanced C++ Fall Lecture 3

PIC 10A. Lecture 17: Classes III, overloading

PIC 10A Objects/Classes

CMSC 132: Object-Oriented Programming II

CPSC 427: Object-Oriented Programming

CS 247: Software Engineering Principles. ADT Design

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

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

Bruce Merry. IOI Training Dec 2013

Homework 4. Any questions?

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Objects and streams and files CS427: Elements of Software Engineering

CS201 Some Important Definitions

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

COMP322 - Introduction to C++

Linked List using a Sentinel

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

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Programming, numerics and optimization

Come and join us at WebLyceum

Introduction to Object-Oriented Programming with C++

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

ALL ABOUT POINTERS C/C++ POINTERS

Object-Oriented Programming

C++ Primer for CS175

QUIZ Friends class Y;

CSE 333 Lecture smart pointers

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

04-19 Discussion Notes

Implementing an ADT with a Class

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

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

CSC 222: Computer Programming II. Spring 2004

Transcription:

CS24 Week 3 Lecture 1 Kyle Dewey

Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time)

Key Minor Points

const

Motivation A lot of bugs are rooted in unexpected state changes Something unexpectedly changes a variable s value A read-only operation wasn t read-only We would like a way to guarantee that state cannot change

Example What is pointed to is constant The pointer itself is constant void foo(const char* const s) { s[0] = a ; // disallowed s = NULL; // disallowed }

References

Motivation Pointers allow us to indirectly refer to data, which is very powerful...but it s also very error-prone We want something in between

References These reference some other data directly References are indirect, but they behave as if they were direct Unlike pointers, references are not a distinct kind of data that lives in memory (more restricted) Trying to get the address of a reference gets the address of what it references

References Example 1 void swappointers(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } void swapref(int& x, int& y) { int temp = x; x = y; y = temp; }

References Example 2 struct point { int x; int y; }; void swap(struct point& p) { int temp = p.x; p.x = p.y; p.y = temp; } int addedpoint(const struct point& p) { return p.x + p.y; }

ADT Review What is the application level? What is the logical/abstract level? What is the implementation level?

Object-Oriented Programming (OOP)

Observation Life is filled with nouns (people, cars, table, projector...) These different nouns interact with each other (speak, accelerate, place object on, turn on) Often have a concept of internal state (thinking, speed, weight, bulb health)

Observation Code can often be modeled in the exact same way

Relationship to OOP Nouns - objects Creating an object - constructors Which interactions are possible - methods Performing an interaction - method calls Internal state - private state, or encapsulation

Constructors Car makecar(int color);

Constructors Car makecar(int color); Constructing an object Car c = makecar(green); c: Car object

Constructors makecar(int color); Methods void accelerateto(double mph); void brake(); double getspeed(); c: Car object

Constructors makecar(int color); Methods void accelerateto(double mph); void brake(); double getspeed(); Method call: c.accelerateto(15.5); c: Car object

Constructors makecar(int color); Methods void accelerateto(double mph); void brake(); double getspeed(); Private State: double speed = 15.5; c: Car object

Recall the Rectangle Width, height, finding area and perimeter What does the constructor look like? What sort of methods does it have? What kind of internal/private state does it have?

OOP for ADTs OOP is great for modeling ADTs. Why? Hint: why was C a suboptimal choice?

OOP for ADTs OOP is great for modeling ADTs. Why? Methods good for defining interfaces (logical level) Private state/encapsulation good for hiding implementation details C doesn t always make encapsulation easy

OOP in C++

Objects In order to make an object, we first need to define a class A class behaves like a sort of template for making objects With the previous example, we would need a Car class

Classes Hold the constructors, methods, and private state of the objects we want to make To make an object, we call a class constructor to get an instance of a class Class instances are synonymous with objects

Car Class Example

Rectangle Class Example

Creating Class Instances Can be made either on the stack or the heap On the stack: Car c(speed); On the heap: Car* c = new Car(speed); Both of these examples call the same constructor For the heap, can free with: delete c;

Constructors in C++

Constructors C++ lets us define multiple constructors for a class Each can be used to make a class instance

Default Constructor

Default Constructor A constructor of special mention is the nullary, AKA default constructor Takes no arguments Used when creating an array of objects on the stack: Car c[20]; Why?

Default Constructor A constructor of special mention is the nullary, AKA default constructor Takes no arguments Used when creating an array of objects on the stack: Car c[20]; Why? - how would you pass the arguments if it weren t this way?

Default Constructor What happens? class Foo { private: int x; };... Foo f;

Default Constructor All OK - the compiler generates a default constructor for you class Foo { private: int x; };... Foo f;

Default Constructor What happens? class Foo { private: int x; };... Foo f; f.x;

Default Constructor Undefined - f.x can be set, but not accessed class Foo { private: int x; };... Foo f; f.x;

Default Constructor What happens? class Foo { public: Foo(int y); private: int x; };... Foo f;

Default Constructor Compile-time error: compiler cannot generate a default constructor class Foo { public: Foo(int y); private: int x; };... Foo f;

Copy Constructor

Copy Constructor Used in contexts where we need to copy an object Declarations with initialization Function calls Car(const Car& other);

Copy Constructor What happens? class Foo { public: Foo(int y); private: int x; };... Foo a(1); Foo b = a; // copy constructor

Copy Constructor All ok - the compiler generates a default copy constructor that copies everything class Foo { public: Foo(int y); private: int x; };... Foo a(1); Foo b = a; // copy constructor

Default Copy Constructor Caveat: the copy performed is a shallow copy memory Before Copy Object 1

Default Copy Constructor Caveat: the copy performed is a shallow copy memory After Copy Object 1 Object 2

Default Copy Constructor If you want a deep copy, you must do it yourself with your own copy constructor memory Before Copy Object 1

Default Copy Constructor If you want a deep copy, you must do it yourself with your own copy constructor memory memory After Copy Object 1 Object 2

Destructors

Destructors Optionally, you can define a destructor for a class: Car::~Car() {} Destructors are called during deallocation When is this for something on the stack? When is this for something on the heap?

Destructors Optionally, you can define a destructor for a class: Car::~Car() {} Destructors are called during deallocation When is this for something on the stack? Return from scope that introduced it When is this for something on the heap? When delete is called on it

Destructors Useful for objects which dynamically allocate memory internally Why?

Destructors Useful for objects which dynamically allocate memory internally Why? - Allows for memory to be deallocated in synchronization with the object being deallocated

More Minor Points

#include

#include No longer correct to put.h after the filename for system-provided files Still expected for your own files // provided by system: #include <iostream> // provided by you: #include myfile.h

Namespaces

Motivation Every name (variable, function, struct) in C lives in the some distinct namespace Means we cannot define two variables with the same name at the same scope Global variable pain

Namespaces A way for the programmer to define custom namespaces In this class, you won t be defining your own, but you will be using existing ones Most notable: std for the standard library

Namespaces Need to fully specify the name of something For example, endl is defined in namespace std, so to use it we must say: std::endl

Namespaces Repeatedly typing out the namespace can be annoying, so we can instead say: using std::endl;...and then later simply say endl everywhere we would have said std::endl

Namespaces Sometimes we want everything from a namespace. For that, we can say: using namespace std;...to put everything in the std namespace in scope (no more need to prepend std:: to everything)

Terminal I/O

Terminal I/O Terminal input and output are modeled as streams that can be read from and written to cin: input stream cout: output stream cerr: error stream (often synonymous with the output stream)

Reading and Writing Can be done using >> and <<, respectively #include <iostream> using namespace std; int main() { int x; cin >> x; cout << "Saw: " << x << endl; return 0; }