Advanced Classes and Dynamic Memory Allocation Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Writing a Good Program 6. Pointers and Arrays
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
C++ Review (3) Structs, Classes, Data Abstraction.
Pointers OVERVIEW.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
C++ Memory Overview 4 major memory segments Key differences from Java
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
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:
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
References and Pointers Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
References and Pointers CS 244 Connect Speakers for this Presentation Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
11 Introduction to Object Oriented Programming (Continued) Cats.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
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.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Memory Management.
Overview 4 major memory segments Key differences from Java stack
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 5 Classes.
Pointers Revisited What is variable address, name, value?
Object Oriented Programming COP3330 / CGS5409
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Presentation transcript:

Advanced Classes and Dynamic Memory Allocation Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content derived/taken from: and some from C++ Through Game Programming (Dawson) Connect Speakers for this Presentation

Things to Note Homework 4 is Due Soon Homework 5 is Posted on D2L – Do NOT delay in starting it Do not forget to look at the Meta-Info files

From Last Time – Reviewed C++ Classes Enumerated Types Operator Overloading – References in C++ – Pointers in C++ Pointers Program: in-class activity Pointers as parameters to functions (Swap) Returning Ptrs from function (on your own - InventoryPtr) Arrays as pointers Tic-Tac-Toe via pointers

For Today Some of this has been seen already – We will add a few more details today Advanced Classes – Use friend functions – Overload operators Dynamic Memory – Allocate and free memory – Avoid memory leaks

Marker Slide Any General Questions ? Next up – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Friend Functions and Overloading Operators Friend functions – have complete access to any member data of a class Operator Overloading – allows you to define new meanings – for built-in operators – as they relate to objects of your own class

In-Class Activity: FriendCritter Download, compile, and run the FriendCritter example on D2L Discussion on this will follow shortly

Looking at FriendCritter //Friend Critter //Demonstrates friend functions and operator overloading #include using namespace std; class Critter { //make the following global functions friends of the Critter class friend void Peek(const Critter& aCritter); friend ostream& operator<<(ostream& os, const Critter& aCritter); public: Critter(const string& name = ""); // default name to empty string private: string m_Name; }; Note the source code here would typically be put into a header file named: FriendCritter.h as it declares the interface to the FriendCritter class

Looking at FriendCritter //Friend Critter //Demonstrates friend functions and operator overloading #include using namespace std; class Critter { //make the following global functions friends of the Critter class friend void Peek(const Critter& aCritter); friend ostream& operator<<(ostream& os, const Critter& aCritter); public: Critter(const string& name = ""); // default name to empty string private: string m_Name; }; Peek is identified here as being a global friend function that returns nothing and takes a constant reference to aCritter

Looking at FriendCritter //Friend Critter //Demonstrates friend functions and operator overloading #include using namespace std; class Critter { //make the following global functions friends of the Critter class friend void Peek(const Critter& aCritter); friend ostream& operator<<(ostream& os, const Critter& aCritter); public: Critter(const string& name = ""); // default name to empty string private: string m_Name; }; Here the operator<< is being identified as friend of the Critter class Which means we will have to provide a definition of it that can process the Critter class This is called overloading the operator Note the source code here would typically be put into a header file named: FriendCritter.h as it declares the interface to the FriendCritter class

Looking at FriendCritter Critter::Critter(const string& name): m_Name(name) { // m_Name = name; // is already done in the line above // via ": m_Name(name)" } //global friend function which can access all of Critter object's members //overloads the << operator so you can send a Critter object to cout ostream& operator<<(ostream& os, const Critter& aCritter) { os << "Critter Object - "; os << "m_Name: " << aCritter.m_Name; return os; } The source code here would typically be put into a definition file named: FriendCritter.cpp The code here defines and IMPLEMENTS the interface to the FriendCritter class

Critter::Critter(const string& name): m_Name(name) { // m_Name = name; // is already done in the line above // via ": m_Name(name)" } //global friend function which can access all of Critter object's members //overloads the << operator so you can send a Critter object to cout ostream& operator<<(ostream& os, const Critter& aCritter) { os << "Critter Object - "; os << "m_Name: " << aCritter.m_Name; return os; } Looking at FriendCritter This is a clever way to initialize variables outside the body of the constructor. It comes in useful from time to time.

Critter::Critter(const string& name): m_Name(name) { // m_Name = name; // is already done in the line above // via ": m_Name(name)" } //global friend function which can access all of Critter object's members //overloads the << operator so you can send a Critter object to cout ostream& operator<<(ostream& os, const Critter& aCritter) { os << "Critter Object - "; os << "m_Name: " << aCritter.m_Name; return os; } Looking at FriendCritter Here we are “overloading” the operator<< because we are adding to the “load” of types the operator can handle i.e. it now must process data of type: class Critter in addition to all the other types So its potential work “load” has increased

Critter::Critter(const string& name): m_Name(name) { // m_Name = name; // is already done in the line above // via ": m_Name(name)" } //global friend function which can access all of Critter object's members //overloads the << operator so you can send a Critter object to cout ostream& operator<<(ostream& os, const Critter& aCritter) { os << "Critter Object - "; os << "m_Name: " << aCritter.m_Name; return os; } Looking at FriendCritter Normally you do NOT want to put additional information into the output generated by operator<< So this line typically would NOT be here - typically only want to output just the member data of the class

Critter::Critter(const string& name): m_Name(name) { // m_Name = name; // is already done in the line above // via ": m_Name(name)" } //global friend function which can access all of Critter object's members //overloads the << operator so you can send a Critter object to cout ostream& operator<<(ostream& os, const Critter& aCritter) { os << "Critter Object - "; os << "m_Name: " << aCritter.m_Name; return os; } Looking at FriendCritter Because operator<< is a friend of the FriendCritter class it can use the “dot” notation access member data

Looking at FriendCritter void Peek(const Critter& aCritter); int main() { Critter crit("Poochie"); cout << "Calling Peek() to access crit's private data member, m_Name: \n"; Peek(crit); cout << "\nSending crit object to cout with the << operator:\n"; cout << crit; return 0; } //global friend function which can access all of a Critter object's members void Peek(const Critter& aCritter) { cout << aCritter.m_Name << endl; } The source code here would typically be put into whatever file holds the main() function Perhaps named FriendCritterTester.cpp This code usually is where the class is used. The global friend function Peek is also shown here. Functions such as this would more often be found in the file: FriendCritter.cpp file As that would group all functions definitions related to the class FriendCritter in one file It is placed here to show there are options.

Looking at FriendCritter void Peek(const Critter& aCritter); int main() { Critter crit("Poochie"); cout << "Calling Peek() to access crit's private data member, m_Name: \n"; Peek(crit); cout << "\nSending crit object to cout with the << operator:\n"; cout << crit; return 0; } // global friend function which can access all of a Critter object's members void Peek(const Critter& aCritter) { cout << aCritter.m_Name << endl; } Declare and Instantiate a Critter named Poochie

Looking at FriendCritter void Peek(const Critter& aCritter); int main() { Critter crit("Poochie"); cout << "Calling Peek() to access crit's private data member, m_Name: \n"; Peek(crit); cout << "\nSending crit object to cout with the << operator:\n"; cout << crit; return 0; } // global friend function which can access all of a Critter object's members void Peek(const Critter& aCritter) { cout << aCritter.m_Name << endl; } Call the global function Peek

Looking at FriendCritter void Peek(const Critter& aCritter); int main() { Critter crit("Poochie"); cout << "Calling Peek() to access crit's private data member, m_Name: \n"; Peek(crit); cout << "\nSending crit object to cout with the << operator:\n"; cout << crit; return 0; } // global friend function which can access all of a Critter object's members void Peek(const Critter& aCritter) { cout << aCritter.m_Name << endl; } Make use of the overloaded operator<<

Looking at FriendCritter void Peek(const Critter& aCritter); int main() { Critter crit("Poochie"); cout << "Calling Peek() to access crit's private data member, m_Name: \n"; Peek(crit); cout << "\nSending crit object to cout with the << operator:\n"; cout << crit; return 0; } // global friend function which can access all of a Critter object's members void Peek(const Critter& aCritter) { cout << aCritter.m_Name << endl; } Because Peek is a friend of FriendCritter it can access member data of the class via the “dot” notation

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) Next up – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Memory Allocation Most variables you use have their memory “pre-allocated” – Well before the program is run – And each variable takes up the same space every run – This defines static memory allocation – These variables are allocated on “the stack”

Why dynamically allocate? Efficiency. – You use only the amount of memory you need at any time – You need 100 enemies at the beginning of the level? Allocate them when the level loads Deallocate when the level completes No need to keep them around for the whole game – Extra bonus You can create an object in one function that you can access after that function ends

Dynamic Memory Allocation What to know – Use new to allocate Allocates memory on the Heap (aka Free Store) – Use delete or delete [] to de-allocate – For arrays keep track of how much you allocate You will have a variable pointing at the first array element – int *myArray = new int[10]; You will need to know where the array ends – myArray[0] is the first element – myArray[9] is the last (0, 1, 2, …9  ten elements)

Using new for Variable Allocation Example (for illustration of new w.r.t. dynamic arrays) int main() { char *strPtr = NULL; // a pointer to a string, init to point at NULL string inputStr; // an std::string for keyboard input // get a string from the keyboard cout << “Please enter a word and press enter” << endl; cin >> inputStr; // Want to copy inputStr into strPtr, Alloc memory first strPtr = new char[ inputStr.length() + 1]; // +1 for the ‘\0’ at end // Use a for loop to copy it for (int i=0; i < inputStr.length(); i++) strPtr[i] = inputStr.at(i); strPtr[ inputStr.length() ] = ‘\0’; // null terminate the char string delete [] strPtr; // Free the memory we allocated return 0; // 0 for success (some progs return EXIT_SUCCESS) }

The Heap Program The program you are about to look at – Dynamically allocates memory on the heap (free store) for an integer value assigns it a value displays it – Then calls a function which also dynamically allocates memory for an integer variable assigns it a value returns a pointer to it – The main function then uses the pointer to display the second value – Memory for both variables is then deallocated and the program ends

Class Activity: Heap Program Enter and run the following program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; }

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } int* intOnHeap() This declares a function that will return an integer pointer Presumably to an integer allocated on the heap (free store)

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer variable name pHeap

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer variable name pHeap Initialize it to point to a NEWLY allocated integer (memory allocated on the free store – aka heap)

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Use the “star” to dereference the pointer and assign what it points to the value 10 *pHeap = 10;

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Again Use the “star” to dereference the pointer This time to output the value of what it points to

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare another integer pointer variable, this one is name pHeap2

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare another integer pointer variable, this one is name pHeap2 Initialize it to have the pointer returned from intOnHeap()

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Free the memory of both pointer variables using delete

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } For safety set both pointers to point to NULL (aka zero)

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer named pTemp

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer named pTemp Initialize it to point to newly allocated memory holding the value 20

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer named pTemp Initialize it to point to newly allocated memory holding the value 20 CAUTION If those parentheses get changed into square brackets [20] something entirely different happens!

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer named pTemp Initialize it to point to newly allocated memory holding the value 20 CAUTION If those parentheses get changed into square brackets [20] something entirely different happens! new int[20];  allocates an array of 20 integers

Examination of: Heap Program // Heap // Demonstrates dynamically allocating memory #include using namespace std; int* intOnHeap(); //returns an int on the heap int main() { int* pHeap = new int; *pHeap = 10; cout << "*pHeap: " << *pHeap << "\n\n"; int* pHeap2 = intOnHeap(); cout << "*pHeap2: " << *pHeap2 << "\n\n"; cout << "Freeing memory pointed to by pHeap.\n\n"; delete pHeap; cout << "Freeing memory pointed to by pHeap2.\n\n"; delete pHeap2; // get rid of dangling pointers pHeap = 0; pHeap2 = 0; return 0; } int* intOnHeap() { int* pTemp = new int(20); return pTemp; } Declare an integer pointer named pTemp Initialize it to point to newly allocated memory holding the value 20 Parentheses are okay new int(20) allocates ONE integer with value 20

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory Next up – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Memory Leaks Having the power to allocate memory – Means you – Have the responsibility to de-allocate it – Failure to de-allocate memory causes memory leaks And Bad things happen You wouldn’t want bad things to happen – Would you?

Memory Leaks Having the power to allocate memory – Means you – Have the responsibility to de-allocate it – Failure to de-allocate memory causes memory leaks And Bad things happen You wouldn’t want bad things to happen – Would you?

Example Leak Adding and calling the below function to the Heap program just discussed would cause a memory leak – So do NOT do this void leak1() { int* drip1 = new int(30); } Allocates memory Function ends Pointer to the allocated memory is gone But the allocated memory remains With no way to deallocate it BAD, VERY BAD

Example Leak Adding and calling the below function to the Heap program just discussed would cause a memory leak – So do NOT do this void leak2() { int* drip2 = new int(50); drip2 = new int(100); delete drip2; } Allocates memory and assigns to a pointer – OK Allocates more memory, assigns to same pointer but does NOT free the first memory allocated BAD VERY BAD Does deallocate the second allocation, so that’s good But the first allocation again now no way to free that memory

Other Leaks There are MANY, MANY ways to cause memory leaks One rule can prevent them all – Every time there is a new There must be a delete Follow that rule and it will prevent many bad things

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks Next up – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Arrays: new and delete [] To dynamically allocate an array you need to use square brackets in both the new line and the delete line The rule: – If you see a line with new and square brackets – Then the delete that goes with it must have square brackets too Examples follow on next slide

Example: Arrays and Dynamic Memory int main() { int *pIntArray = new int[45]; delete [] pIntArray; someClass *pSC = new someClass[23]; delete [] pSC; }

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32,

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } Declare an integer pointer p

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Ask the user for the size of the array to allocate

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Allocate the requested memory and assign the pointer new returns to the variable named p the “(nothrow)” is a parameter to new It is not required and may or may not be supported by your compiler

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 If new returns zero (the null pointer) it was unable to allocate the requested memory

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 Standard for-loop Notice the square brackets access element n of the array referenced by p. So p is acting as an array name.

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, Another standard for-loop outputting the numbers entered

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, Another standard for-loop outputting the numbers entered

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, Another standard for-loop outputting the numbers entered

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, Another standard for-loop outputting the numbers entered

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32, Another standard for-loop outputting the numbers entered

Another Example: Arrays and Dynamic Memory #include using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; } How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32, Use delete [] to de-allocate the array referenced by p

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays Next up – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: [go to next slide] cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too The can point to dynamically allocated arrays cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too The can point to dynamically allocated arrays or dynamically allocated single entities cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too The can point to dynamically allocated arrays or dynamically allocated single entities or ‘normal’ allocated entities cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too The can point to dynamically allocated arrays or dynamically allocated single entities or ‘normal’ allocated entities Dynamically Alloc’d arrays use delete [] cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Classes and Dynamic Memory // pointer to classes example #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Main points: You can have pointers to classes too The can point to dynamically allocated arrays or dynamically allocated single entities or ‘normal’ allocated entities Dynamically Alloc’d arrays use delete [] Dynamically Alloc’d single entities use delete cout << "a area: " << a.area() << endl; cout area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; }

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory Next up – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Free Store Allocation Summary Allocate using new – New allocates an object on the free store, sometimes initializes it, and returns a pointer to it int* pi = new int; – // default initialization (none for int) char* pc = new char('a'); – // explicit initialization double* pd = new double[10]; – // allocation of (uninitialized) array – New throws a bad_alloc exception if it can't allocate

Free Store DE-Allocation Summary Deallocate using delete and delete[ ] – delete and delete[ ] return the memory of an object allocated by new to the free store so that the free store can use it for new allocations delete pi;// deallocate an individual object delete pc;// deallocate an individual object delete[ ] pd;// deallocate an array – Delete of a zero-valued pointer ("the null pointer") does nothing char* p = 0; or char *p = NULL; delete p;// harmless

Classes and New and Delete You can have pointers to classes – and use new and delete with them – just as with any other type New and Delete inside Classes – Classes typically allocate memory for member variables in the class’s constructor for use while the instantiated object of the class exists – When the object goes out of scope, or is explicitly deleted itself The class’s destructor deallocates the memory that was allocated in the constructor using delete – More on this shortly

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary Next up – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

More Fun with Classes There still remain several things to be discovered about classes, such as – destructors – copy constructors – overloaded assignment operator= We will do a little review of things you have already seen – establish context – and move into more new stuff

A Basic Class: A Place to Start // example: one class, two objects #include using namespace std; class CRectangle { int x, y; // default to private public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main point: No Constructor, No Destructor Forces call to set_values to initialize stuff No fancy slides setup for this one Does show some change-up options in how you write/format things

Constructor // example: class constructor #include using namespace std; class CRectangle { int width, height; public: CRectangle (int,int); int area () {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main point: Have a Constructor Stuff gets “auto” initialized

Constructor // example: class constructor #include using namespace std; class CRectangle { int width, height; public: CRectangle (int,int); int area () {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main point: Have a Constructor Stuff gets “auto” initialized

Constructor AND Destructor with Dynamic Memory Allocation // example on constructors and destructors #include using namespace std; class CRectangle { int *width, *height; public: CRectangle (int,int); ~CRectangle (); int area () {return (*width * *height);} }; CRectangle::CRectangle (int a, int b) { width = new int; height = new int; *width = a; *height = b; } CRectangle::~CRectangle () { delete width; delete height; } int main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main points:

Constructor AND Destructor with Dynamic Memory Allocation // example on constructors and destructors #include using namespace std; class CRectangle { int *width, *height; public: CRectangle (int,int); ~CRectangle (); int area () {return (*width * *height);} }; CRectangle::CRectangle (int a, int b) { width = new int; height = new int; *width = a; *height = b; } CRectangle::~CRectangle () { delete width; delete height; } int main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main points: Have a Constructor that dynamically allocates some memory and initializes the member vars’ values

Constructor AND Destructor with Dynamic Memory Allocation // example on constructors and destructors #include using namespace std; class CRectangle { int *width, *height; public: CRectangle (int,int); ~CRectangle (); int area () {return (*width * *height);} }; CRectangle::CRectangle (int a, int b) { width = new int; height = new int; *width = a; *height = b; } CRectangle::~CRectangle () { delete width; delete height; } int main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main points: Have a Constructor that dynamically allocates some memory and initializes the member vars’ values Have a Destructor that de-allocates the memory

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Next up – More on Classes Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Overloading Constructors // overloading class constructors #include using namespace std; class CRectangle { int width, height; public: CRectangle (); CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle () { width = 5; height = 5; } CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb; cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main points:

Overloading Constructors // overloading class constructors #include using namespace std; class CRectangle { int width, height; public: CRectangle (); CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle () { width = 5; height = 5; } CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb; cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 30 Main points: Have a Constructor with no params, sets default values for member variables

Overloading Constructors // overloading class constructors #include using namespace std; class CRectangle { int width, height; public: CRectangle (); CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle () { width = 5; height = 5; } CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb; cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; } rect area: 12 rectb area: 25 Main points: Have a Constructor with no params, sets default values for member variables Have a second Constructor with TWO params, sets member var values to params sent

Important Constructor to Overload The COPY constructor is one you almost always want to overload – This handles cases where you send a class object to a function via pass-by-value – C++ gives you an “implicit” copy constructor if you do not create one But it does not always (rarely) works the way you want or need it to – Especially when using pointers

Overloading the Copy Constructor // overloading class copy constructor #include using namespace std; class CRectangle { int *pWidth, *pHeight; public: CRectangle (); CRectangle (CRectangle& copyMe); CRectangle (int,int); int area (void) {return (*pWidth) * (*pHeight);} }; CRectangle::CRectangle () { pWidth = new int(5); pHeight = new int(5); } CRectangle::CRectangle (CRectangle& copyMe) { pWidth = new int(*(copyMe.pWidth)); pHeight = new int(*(copyMe.pHeight)); } CRectangle::CRectangle (int a, int b) { pWidth = new int(a); pHeight = new int(b); } rect area: 12 passed rect area: 12 Main points: [next slide] void PrintRect (CRectangle printMe) { cout << "passed rect area: " << printMe.area() << endl; } int main () { CRectangle rect (3,4); cout << "rect area: " << rect.area() << endl; PrintRect(rect); return 0; }

Overloading the Copy Constructor // overloading class copy constructor #include using namespace std; class CRectangle { int *pWidth, *pHeight; public: CRectangle (); CRectangle (CRectangle& copyMe); CRectangle (int,int); int area (void) {return (*pWidth) * (*pHeight);} }; CRectangle::CRectangle () { pWidth = new int(5); pHeight = new int(5); } CRectangle::CRectangle (CRectangle& copyMe) { pWidth = new int(*(copyMe.pWidth)); pHeight = new int(*(copyMe.pHeight)); } CRectangle::CRectangle (int a, int b) { pWidth = new int(a); pHeight = new int(b); } rect area: 12 passed rect area: 12 Main points: Declare copy constructor void PrintRect (CRectangle printMe) { cout << "passed rect area: " << printMe.area() << endl; } int main () { CRectangle rect (3,4); cout << "rect area: " << rect.area() << endl; PrintRect(rect); return 0; }

Overloading the Copy Constructor // overloading class copy constructor #include using namespace std; class CRectangle { int *pWidth, *pHeight; public: CRectangle (); CRectangle (CRectangle& copyMe); CRectangle (int,int); int area (void) {return (*pWidth) * (*pHeight);} }; CRectangle::CRectangle () { pWidth = new int(5); pHeight = new int(5); } CRectangle::CRectangle (CRectangle& copyMe) { pWidth = new int(*(copyMe.pWidth)); pHeight = new int(*(copyMe.pHeight)); } CRectangle::CRectangle (int a, int b) { pWidth = new int(a); pHeight = new int(b); } rect area: 12 passed rect area: 12 Main points: Declare copy constructor Implement the copy constructor void PrintRect (CRectangle printMe) { cout << "passed rect area: " << printMe.area() << endl; } int main () { CRectangle rect (3,4); cout << "rect area: " << rect.area() << endl; PrintRect(rect); return 0; }

Overloading the Copy Constructor // overloading class copy constructor #include using namespace std; class CRectangle { int *pWidth, *pHeight; public: CRectangle (); CRectangle (CRectangle& copyMe); CRectangle (int,int); int area (void) {return (*pWidth) * (*pHeight);} }; CRectangle::CRectangle () { pWidth = new int(5); pHeight = new int(5); } CRectangle::CRectangle (CRectangle& copyMe) { pWidth = new int(*(copyMe.pWidth)); pHeight = new int(*(copyMe.pHeight)); } CRectangle::CRectangle (int a, int b) { pWidth = new int(a); pHeight = new int(b); } rect area: 12 passed rect area: 12 Main points: Declare copy constructor Implement the copy constructor Verify the copy constructor works void PrintRect (CRectangle printMe) { cout << "passed rect area: " << printMe.area() << endl; } int main () { CRectangle rect (3,4); cout << "rect area: " << rect.area() << endl; PrintRect(rect); return 0; }

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Next up – More on Classes Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

Important Operator to Overload The Assignment Operator is one you almost always want to overload – operator= – This handles cases where you set one variable equal to another myClass A(1,2); myClass B(6,7) A = B cout << A; // expect 6 and 7 to be printed out – C++ gives you an “implicit” assignment operator if you do not create one But it does not always (rarely) works the way you want or need it to

Overloading operator= For this example we will overload the assignment operator as a member function – you can make the operator a friend function also – but this is the “other way” Some code has been omitted, to focus on the important parts =) int main() { MyClass c1, c2; c1 = c2; // assigns c2 to c1 } The Goal is to be able to do this

Overloading operator= For this example we will overload the assignment operator as a member function – you can make the operator a friend function also – but this is the “other way” Some code has been omitted, to focus on the important parts =) int main() { MyClass c1, c2; c1 = c2; // assigns c2 to c1 } The Goal is to be able to do this Assume we have a class that has private members: int x, char c, std::string s And a constructor that works like this: MyClass c1; c1 = MyClass( 5, 'a', "Hello World" );

Overloading operator= For this example we will overload the assignment operator as a member function – you can make the operator a friend function also – but this is the “other way” Some code has been omitted, to focus on the important parts =) int main() { MyClass c1, c2; c1 = c2; // assigns c2 to c1 } The Goal is to be able to do this Assume we have a class that has private members: int x, char c, std::string s And a constructor that works like this: MyClass c1; c1 = MyClass( 5, 'a', "Hello World" ); Then to override the assignment operator as a member function we would need to implement it like this: MyClass& MyClass::operator=( const MyClass& rhs ) { x = other.x; c = other.c; s = other.s; return *this; }

Overloading operator= For this example we will overload the assignment operator as a member function – you can make the operator a friend function also – but this is the “other way” Some code has been omitted, to focus on the important parts =) int main() { MyClass c1, c2; c1 = c2; // assigns c2 to c1 } The Goal is to be able to do this Assume we have a class that has private members: int x, char c, std::string s And a constructor that works like this: MyClass c1; c1 = MyClass( 5, 'a', "Hello World" ); Then to override the assignment operator as a member function we would need to implement it like this: MyClass& MyClass::operator=( const MyClass& rhs ) { x = other.x; c = other.c; s = other.s; return *this; } In the class declaration we would need the line: MyClass& operator=( const MyClass& rhs )

Marker Slide Any Questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Next up – More on Classes Graded In-Class Activity: HeapDataMember

Download the file: – ICA015_BrokenHeapDataMember.cpp Rename it: HeapDataMember.cpp Fix the program as indicated – Compile it – Run it – When satisfied with it (or out of time) upload the resulting FIXED code to the appropriate D2L dropbox You can work in groups – BUT each individual must submit something to D2L

Marker Slide Any questions on: – Making friends and overloading functions in your classes (FriendCritter) – Dynamically Allocating Memory – Memory Leaks – Using new and delete [] with arrays – Classes and Dynamic Memory – Allocation Summary – More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember Any General Questions? Next Up – Free Play

Free Play – Things to Work On Graded In-Class Activity: HeapDataMember Homework 4 Homework 5 Various In-Class Activities to revisit Several On-Your Own Activities

The End Or is it?