C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
C++ Programming Concepts
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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.
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.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers and Dynamic Arrays
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
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.
Constructors and Destructors
Pointers and Dynamic Arrays
Motivation and Overview
Student Book An Introduction
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
This pointer, Dynamic memory allocation, Constructors and Destructor
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructors and Destructors
Indirection.
9-10 Classes: A Deeper Look.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Dynamic Memory CSCE 121.
Constructors & Destructors
9-10 Classes: A Deeper Look.
Presentation transcript:

C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors

Introduction References New Arrays –by type –by pointers Destructors

Constructors - Extra If the class does not contain a constructor –The system provides a default If the class contains one or more –You lose the default constructor. –You have to provide all required.

References Referring back to the “swap” function Instead of passing values. Passed addresses of the variables when calling function. Manipulated values by dereferencing. Had to remember to use “&” and “*”

References C++ has “reference type” Do not need to use “&” and “*” when using. Declared by –basic type, followed by –“&” –e.g int&“reference to an integer” double&“reference to a double”

Reference Usage Must declare and initialise at same time E.g. –intnCount; –int&refInt = nCount; Can then access the value by –using “nCount” –or its alias –refInt

References and classes Can use references for classes E.g.Account Freddy; Account& refAcc = Freddy; so, can access the object using either E.gFreddy.setBalance(13.26); refAcc.setBalance(13.26); Both – produce the same result.

New So far – instantiated by “name” –e.g. Account Freddy; Account Tripta(12.36, 2.5, 2); Can also use “new” –similar to malloc() –much easier to use. –allocates memory for the object. –returns a pointer to the memory address.

Using “new” First need a pointer –e.g. Account *pAcc; Now instantiate –e.g. pAcc = new Account; or with arguments –pAcc = new Account(12.36, 2.4, 6);

Error trapping If “new” is unable to acquire memory –it will return “NULL” Should always test for this –e.g. Account *pAcc = new Account(12.36, 2.4, 1); if(!pAcc)// test for NULL { // Error handler cout << “Error allocating memory”; exit(1);// terminate application }

Accessing the object When using a pointer. Methods etc. are accessed differently Using “.” notation –e.g.(*pAcc).setBalance(15.26); Using an alternative notation “ -> ” –e.g.pAcc->setBalance(15.26);

Arrays So far, have used names for our accounts. Have just seen how to use individual pointers. Not very efficient! We can do better than that …. –Array of class objects. –Array of pointers. –Linked lists- later lecture

Array Indexing Don’t forget……. –If you declare an array e.g.arAccs[10] –The indexing values are 0 to 9 –NOT1 to 10 Also – don’t forget that in C/C++ there is no bounds checking.

Array example Declaration –Account arAccs[3]; This will work only under certain conditions –The class does not have a constructor or –The class has a constructor that does not require arguments.

Arrays and Constructor Args Account arAcc[3]; If Account has constructors that require zero arguments – equivalent to… Account arAcc[3] = {account(), account(), account()};

Arrays and Constructor Args If wanted to include arguments. Account arAccs[3] = {account(12.36, 1.2, 1), account(100.52, 2.5, 2), account(125.0, 2.5, 3)}; There are many other possibilities, all dependant upon the number of arguments required. You should consult your text books etc. for more information regarding this matter.

Arrays and Pointers Also possible to have an array of pointers. –e.g. Account *parAccs[10]; An array of pointers to objects of type account Instantiation –e.g. parAccs[0] = new Account(12.36, 2.5, 1);

Multiple Instantiations Account *parAccs[10]; // Instantiate a number of Accounts for(int nCount = 0; nCount < 10; nCount++) { parAccs[nCount] = new Account(0.0, 0.0, nCount +1); } // Access a single account cout getBalance() << endl << endl;

Destructors Just as a class can have constructors –That are “special” methods which….. –are called when instantiating an object It can also have a “special” methods that are called when deleting an object –These are called “destructors” –Take the same name as the class –But preceded by the “tilde” character – “~”

Destructors So, for the class – Account. The destructor would be…. – ~Account Destructors –Do not return a value. –Do not accept any arguments. –Called automatically when using “delete”. –Maximum of one per class.

“delete” Just as “new” will acquire memory “delete” will release it back to the system Can only be used when memory via “new” –See malloc() & free (‘C’ programming) Use of delete –“Tidy up” objects – esp. if holding pointers. –Save object data. –etc.

Summary In this lecture have considered –References –Use of “new” –Arrays - by type - by pointers –“new” –Destructors –“delete”

Next Lecture Copy Constructors Overloading operators –This is challenging so make sure you are up to date with the work.