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

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Operator Overloading Fundamentals
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Lesson 9 Pointers CS 1 Lesson 9 -- John Cole1. Pointers in Wonderland The name of the song is called ‘Haddock’s Eyes’.” “Oh, that’s the name of the song,
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Workin’ with Pointas An exercise in destroying your computer.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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 A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers and Classes.
Pointers What is the data type of pointer variables?
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Lecture 9 Files, Pointers
CISC181 Introduction to Computer Science Dr
Pointers Revisited What is variable address, name, value?
The dirty secrets of objects
Pointers and References
Dynamic Memory Allocation
Chapter 9: Pointers.
understanding memory usage by a c++ program
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Indirection.
C++ Testing and Classes
C++ Pointers and Strings
QUIZ.
C Programming Pointers
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Pointers and dynamic objects
C++ Testing and Classes
Passing Arguments and The Big 5
Standard Version of Starting Out with C++, 4th Edition
Dynamic Memory Allocation
Pointers and References
Pointers and References
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
More on C++ G Carl Evans.
Presentation transcript:

More on C++ G Carl Evans

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;

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