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

Similar documents
C++ Constructor Insanity

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

C++ Constructor Insanity

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

pointers & references

CS 11 C++ track: lecture 1

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 303: Concepts and Tools for Software Development

CSE 333 Lecture smart pointers

CS11 Introduction to C++ Fall Lecture 1

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

CS24 Week 3 Lecture 1

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

C++ Templates. CSE 333 Autumn 2018

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

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

CSE 333 Lecture 2 - arrays, memory, pointers

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

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

C++ Class Details, Heap

Intermediate Programming, Spring 2017*

CSE 333 Lecture 9 - intro to C++

PIC 10A Objects/Classes

CS 162 Intro to CS II. Structs vs. Classes

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS250 Final Review Questions

CE221 Programming in C++ Part 1 Introduction

Computer Programming

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Exercise 1.1 Hello world

Introduction to C++ Systems Programming

CSE 333 Lecture smart pointers

Ch 2 ADTs and C++ Classes

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

COMP 2355 Introduction to Systems Programming

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

Introduction to Classes

G52CPP C++ Programming Lecture 9

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Assumptions. History

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

Common Misunderstandings from Exam 1 Material

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

CS11 Intro C++ Spring 2018 Lecture 1

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Scope. Scope is such an important thing that we ll review what we know about scope now:

C++ Namespaces, Exceptions

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

Abstraction in Software Development


III. Classes (Chap. 3)

Memory and Pointers written by Cathy Saxton

PROGRAMMING IN C++ CVIČENÍ

CS 376b Computer Vision

Object-Oriented Principles and Practice / C++

COMP6771 Advanced C++ Programming

Unified Modeling Language a case study

COMP6771 Advanced C++ Programming

C++ For Science and Engineering Lecture 12

Week 3: Pointers (Part 2)

Introduction to C++ Introduction to C++ 1

PIC 10A. Lecture 15: User Defined Classes

Lab Instructor : Jean Lai

Abstract Data Types (ADT) and C++ Classes

Implementing an ADT with a Class

Programming Abstractions

Programmazione. Prof. Marco Bertini

SFU CMPT Topic: Classes

Constants, References

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Introducing C++ to Java Programmers

Functions and Recursion

COEN244: Class & function templates

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

CS250 Final Review Questions

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

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

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Polymorphism Part 1 1

Object Reference and Memory Allocation. Questions:

Program template-smart-pointers-again.cc

Chapter 2. Procedural Programming

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

EL2310 Scientific Programming

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

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

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

CSE 374 Programming Concepts & Tools

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Linked List using a Sentinel

Transcription:

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

Administrivia New C++ exercise out today, due Friday morning Section this week: C++, const / references / classes HW2 - how s it going? Questions?

Today s goals Useful C++ features - references, const Introducing C++ classes - defining, using them

Reminder: pointers C: a pointer is a variable containing an address - you can change its value to change what it is pointing to - a pointer can contain the address of a different variable int x = 5, y = 10; int *z = &x; *z += 1; // sets x to 6 x += 1; // sets x (and therefore *z) to 7 z = &y; // sets z to the address of y *z += 1; // sets y (and therefore *z) to 11 x 5 y 10 z? pointer.cc

Reminder: pointers C: a pointer is a variable containing an address - you can change its value to change what it is pointing to - a pointer can contain the address of a different variable int x = 5, y = 10; int *z = &x; *z += 1; // sets x to 6 x += 1; // sets x (and therefore *z) to 7 x 5 y 10 z = &y; // sets z to the address of y *z += 1; // sets y (and therefore *z) to 11 z 0xbfff2d4 pointer.cc

Reminder: pointers C: a pointer is a variable containing an address - you can change its value to change what it is pointing to - a pointer can contain the address of a different variable int x = 5, y = 10; int *z = &x; *z += 1; // sets x to 6 x += 1; // sets x (and therefore *z) to 7 x 6 y 10 z = &y; // sets z to the address of y *z += 1; // sets y (and therefore *z) to 11 z 0xbfff2d4 pointer.cc

Reminder: pointers C: a pointer is a variable containing an address - you can change its value to change what it is pointing to - a pointer can contain the address of a different variable int x = 5, y = 10; int *z = &x; *z += 1; // sets x to 6 x += 1; // sets x (and therefore *z) to 7 x 7 y 10 z = &y; // sets z to the address of y *z += 1; // sets y (and therefore *z) to 11 z 0xbfff2d4 pointer.cc

Reminder: pointers C: a pointer is a variable containing an address - you can change its value to change what it is pointing to - a pointer can contain the address of a different variable int x = 5, y = 10; int *z = &x; *z += 1; // sets x to 6 x += 1; // sets x (and therefore *z) to 7 x 7 y 10 z = &y; // sets z to the address of y *z += 1; // sets y (and therefore *z) to 11 z 0xbfff2d0 pointer.cc

Reminder: pointers C: a pointer is a variable containing an address - you can change its value to change what it is pointing to - a pointer can contain the address of a different variable int x = 5, y = 10; int *z = &x; *z += 1; // sets *z (and therefore x) to 6 x += 1; // sets x (and therefore *z) to 7 x 7 y 11 z = &y; // sets z to the address of y *z += 1; // sets *z (and therefore y) to 11 z 0xbfff2d0 pointer.cc

References C++: introduces references as part of the language - a reference is an alias for some other variable alias: another name that is bound to the aliased variable mutating a reference is mutating the referenced variable int x = 5, y = 10; int &z = x; // binds the name "z" to variable x z += 1; // sets z (and thus x) to 6 x += 1; // sets x (and thus z) to 7 z = y; // sets z (and thus x) to the value of y z += 1; // sets z (and thus x) to 11 x 5 y 10 reference1.cc

References C++: introduces references as part of the language - a reference is an alias for some other variable alias: another name that is bound to the aliased variable mutating a reference is mutating the referenced variable int x = 5, y = 10; int &z = x; // binds the name "z" to variable x z += 1; // sets z (and thus x) to 6 x += 1; // sets x (and thus z) to 7 z = y; // sets z (and thus x) to the value of y z += 1; // sets z (and thus x) to 11 x,z 5 y 10 reference1.cc

References C++: introduces references as part of the language - a reference is an alias for some other variable alias: another name that is bound to the aliased variable mutating a reference is mutating the referenced variable int x = 5, y = 10; int &z = x; // binds the name "z" to variable x z += 1; // sets z (and thus x) to 6 x += 1; // sets x (and thus z) to 7 z = y; // sets z (and thus x) to the value of y z += 1; // sets z (and thus x) to 11 x,z 6 y 10 reference1.cc

References C++: introduces references as part of the language - a reference is an alias for some other variable alias: another name that is bound to the aliased variable mutating a reference is mutating the referenced variable int x = 5, y = 10; int &z = x; // binds the name "z" to variable x z += 1; // sets z (and thus x) to 6 x += 1; // sets x (and thus z) to 7 z = y; // sets z (and thus x) to the value of y z += 1; // sets z (and thus x) to 11 x,z 7 y 10 reference1.cc

References C++: introduces references as part of the language - a reference is an alias for some other variable alias: another name that is bound to the aliased variable mutating a reference is mutating the referenced variable int x = 5, y = 10; int &z = x; // binds the name "z" to variable x z += 1; // sets z (and thus x) to 6 x += 1; // sets x (and thus z) to 7 z = y; // sets z (and thus x) to the value of y z += 1; // sets z (and thus x) to 11 x,z 10 y 10 reference1.cc

References C++: introduces references as part of the language - a reference is an alias for some other variable alias: another name that is bound to the aliased variable mutating a reference is mutating the referenced variable int x = 5, y = 10; int &z = x; // binds the name "z" to variable x z += 1; // sets z (and thus x) to 6 x += 1; // sets x (and thus z) to 7 z = y; // sets z (and thus x) to the value of y z += 1; // sets z (and thus x) to 11 x,z 11 y 10 reference1.cc

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (main) a 5 (main) b 10

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (main) a 5 (main) b 10

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (swap) tmp?? (main) a (swap) x (main) b (swap) y 5 10

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (swap) tmp 5 (main) a (swap) x (main) b (swap) y 5 10

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (swap) tmp 5 (main) a (swap) x (main) b (swap) y 10 10

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (swap) tmp 5 (main) a (swap) x (main) b (swap) y 10 5

Pass by reference C++ allows you to truly pass-by-reference - client passes in an argument with normal syntax function uses reference parameters with normal syntax modifying a reference parameter modifies the caller s argument void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; int a = 5, b = 10; swap(a, b); cout << "a: " << a << "; b: " << b << endl; passbyreference.cc (main) a 10 (main) b 5

const const: cannot be changed - used much more in C++ than in C void BrokenPrintSquare(const int &i) { i = i*i; // Compiler error here! std::cout << i << std::endl; int j = 2; BrokenPrintSquare(j); brokenpassbyrefconst.cc

const const s syntax is confusing int x = 5; // x is an int const int y = 6; // y is a (const int) y++; // compiler error const int *z = &y; // z is a (variable pointer) to a (const int) *z += 1; // compiler error z++; // ok int *const w = &x; // w is a (const pointer) to a (variable int) *w += 1; // ok w++; // compiler error const int *const v = &x; // v is a (const pointer) to a (const int) *v += 1; // compiler error v++; // compiler error constmadness.cc

Style guide tip use const reference parameters for input values - particularly for large values use pointers for output parameters input parameters first, then output parameters last #include <cstdlib> void CalcArea(const int &width, const int &height, int *const area) { *area = width * height; int w = 10, h = 20, a; CalcArea(w, h, &a); styleguide.cc

When to use references? A stylistic choice - not something mandated by the C++ language Google C++ style guide suggests: - input parameters: either use values (for primitive types like int or small structs/objects) or use const references (for complex structs / object instances) - output parameters use const pointers (i.e., unchangeable pointers referencing changeable data see previous slide)

virality of const - OK to pass a pointer to non-const - to a function that expects a pointer to const - not OK to pass a pointer to a const - to a function that expects a pointer to a non-const #include <iostream> void foo(const int *y) { std::cout << *y << std::endl; void bar(int *y) { std::cout << *y << std::endl; const int a = 10; int b = 20; foo(&b); // OK bar(&a); // not OK return 0;

Classes class declaration syntax (in a.h file) class Name { public: members; private: members; ; class member definition syntax (in a.cc file) returntype classname::methodname(parameters) { statements; You can name your.cc,.h file anything (unlike Java) typically name them Classname.cc, Classname.h

.h file #ifndef _POINT_H_ #define _POINT_H_ class Point { public: Point(const int x, const int y); // constructor int get_x() const { return x_; // inline member function int get_y() const { return y_; // inline member function double Distance(const Point &p) const; // member function void SetLocation(const int x, const int y); // member functn private: int x_; // data member int y_; // data member ; // class Point #endif // _POINT_H_ Point.h

.cc file #include <cmath> #include "Point.h" Point::Point(const int x, const int y) { x_ = x; this->y_ = y; // this-> is optional, unless names conflict double Point::Distance(const Point &p) const { // We can access p s x_ and y_ variables either through the // get_x(), get_y() accessor functions, or the x_, y_ private // member variables directly, since we re in a member // function of the same class. double distance = (x_ - p.get_x()) * (x_ - p.get_x()); distance += (y_ - p.y_) * (y_ - p.y_); return sqrt(distance); void Point::SetLocation(const int x, const int y) { x_ = x; y_ = y; Point.cc

.cc file with main( ) #include <iostream> #include "Point.h" using namespace std; int main(int argc, char **argv){ Point p1(1, 2); // stack allocate a new Point Point p2(4, 6); // stack allocate a new Point cout << "p1 is: (" << p1.get_x() << ", "; cout << p1.get_y() << ")" << endl; cout << "p2 is: (" << p2.get_x() << ", "; cout << p2.get_y() << ")" << endl; cout << "dist : " << p1.distance(p2) << endl; return 0; usepoint.cc

struct vs. class in C - a struct contains only fields cannot contain methods does not have public vs. private vs. protected in C++ - struct and class are (nearly) the same both can contain methods both can have public vs. private vs. protected - struct: default public, class: default private - common style convention: structs for simple bundles of data (maybe with convenience constructors); classes for abstractions with data + functions

Reading Assignment Before next class: read sections in C++ Primer covering constructors, copy constructors, assignment (operator=), and destructors - Skip move semantics for now - The table of contents and index are your friends

Exercise 1 Write a C++ program that: - has a class representing a 3-dimensional point - has the following methods: return the inner product of two 3d points return the distance between two 3d points accessors and mutators for the x, y, z coordinates

Exercise 2 Write a C++ program that: - has a class representing a 3-dimensional box use your exercise 1 class representing 3d points to store the coordinates of the verticies that define it assume the box has right-angles only and its faces are parallel to the axes, so you only need two vertices to define it - has the following methods: test if one box is inside another box return the volume of a box handles <<, =, and a copy constructor uses const in all the right places

See you on Friday!