More on C++ G Carl Evans.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Operator Overloading Fundamentals
Informática II Prof. Dr. Gustavo Patiño MJ
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Workin’ with Pointas An exercise in destroying your computer.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers and Classes.
Pointer to an Object Can define a pointer to an object:
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Lecture 9 Files, Pointers
Chapter 10: Pointers Starting Out with C++ Early Objects
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 10: Pointers Starting Out with C++ Early Objects
The dirty secrets of objects
Pointers and References
Dynamic Memory Allocation
Chapter 9: Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
understanding memory usage by a c++ program
Chapter 9: Pointers.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
C++ File Structure and Intro to the STL
C++ Testing and Classes
C++ Pointers and Strings
Passing Arguments and The Big 5
COP 3330 Object-oriented Programming in C++
C++ File Structure and Intro to the STL
Chapter 9: Pointers and String
C++ Testing and Classes
Passing Arguments and The Big 5
Standard Version of Starting Out with C++, 4th Edition
Dynamic Memory Allocation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and References
Pointers and References
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
More on C++ G Carl Evans.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

More on C++ G Carl Evans

How hard was fifth code review assignment? Easy Moderate Challenging Unreasonable

How long did fifth assignment take? Less than 3 hours 3 to 6 hours 6 to 9 hours 9 to 12 hours More than 12 hours

Classes in C++ File layout .h .cpp Declare structure of the object including member variables Declare member functions (methods) .cpp Define functions of the class

So Far int x; double probability; bool feature_vector[28][28]; vector<ImageData> training_images; ImageData tmp_image; cin >> tmp_image; training_images.push_back();

Pointers (Dereference Operator *, Address of Operator &) Pointers are just variables Store addresses Declare in C++ as follows int *ptr_x; How do I set a pointer ptr_x = ptr_y; ptr_x = &x; How do I access what a pointer points to? *ptr_x = 42; cout << *ptr_x;

What is the behavior? int *ptr; int val; ptr = &val; *ptr = 10; cout << val; What probably happens? 10 printed Some address printed Some unknown value printed Segfault and crash

Explicit Dynamic Allocation new allocates memory and constructs objects returning the address int *heap_int = new int; Can allocator arrays int *heap_array = new int[10] delete Releases memory allocated with new delete heap_int; Must specify when releasing arrays delete[] heap_array;

What is the behavior? int *heap_x; int *heap_y; heap_x = new int; heap_y = heap_x; *heap_y = 10; cout << *heap_x; What probably happens? 10 printed Some address printed Some unknown value printed Segfault and crash

What is the behavior? int *heap_x; int *heap_y; heap_y = heap_x; heap_y = new int; *heap_y = 10; cout << *heap_x; What probably happens? 10 printed Some address printed Some unknown value printed Segfault and crash

Passing Arguments By value By reference By pointer Make a copy Like Java objects By pointer Pass a copy of the pointer

What happens? void fn(int x) { x = 10; } int main() { int x = 200; fn(x); cout << x; What probably happens? 10 printed 200 printed Won't compile Some unknown value printed Segfault and crash

What happens? void fn(int &x) { x = 10; } int main() { int x = 200; fn(x); cout << x; What probably happens? 10 printed 200 printed Won't compile Some unknown value printed Segfault and crash

What happens? void fn(int *x) { x = 10; } int main() { int x = 200; fn(x); cout << x; What probably happens? 10 printed 200 printed Won't compile Some unknown value printed Segfault and crash

What happens? void fn(int *x) { x = 10; } int main() { int x = 200; fn(&x); cout << x; What probably happens? 10 printed 200 printed Won't compile Some unknown value printed Segfault and crash

Operator Overloading Make code more readable and intuitive by allowing more natural expression Change the behavior of many of the standard operators based on the types that are being used. We have seen the overload of [] with map and vector We have seen the overload of -> and * with the iterator in map

Operators that can be overloaded in C++ Operator Category Operators Arithmetic + - * / % ++ -- Bitwise & | ^ ~ >> << Relational < <= > >= == != Logical ! && || Assignment = += -= *= /= %= ˆ= &= |= >>= <<= <= >= Other () [] -> , ->

Stream extraction and insertion std::ostream& operator<<(std::ostream& os, const T& obj){ // write obj to stream return os; } std::istream& operator>>(std::istream& is, T& obj){ // read obj from stream if( /* T could not be constructed */ ) is.setstate(std::ios::failbit); return is;

Student Record