C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation

Slides:



Advertisements
Similar presentations
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Advertisements

C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Processing Data in External Storage CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
C++ Classes C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Interlude 1 C++ Classes CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
A First Book of C++ Chapter 12 Extending Your Classes.
Design issues for Object-Oriented Languages
Chapter 4 Link Based Implementations
Link Based Implementations
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Pointers, Polymorphism, and Memory Allocation
CS212: Object Oriented Analysis and Design
Class Operations Pointer and References with class types
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Chapter 16 Tree Implementations
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ Classes C++ Interlude 1
C++ Classes C++ Interlude 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 4 Link Based Implementations
Basic C++ What’s a declaration? What’s a definition?
Sorted Lists and Their Implementations
Pointers Dr. Bhargavi Goswami Department of Computer Science
Queues and Priority Queue Implementations
Dynamic Memory CSCE 121.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Contents Memory Allocation for Variables and Early Binding of Methods A Problem to Solve Pointers and the Program’s Free Store Virtual Methods and Polymorphism Dynamic Allocation of Arrays http://mediaplayer.pearsoncmg.com/_ph_cc_ecs960544_set.title.Cpp_Memory_Allocation__/aw/streaming/ecs_carrano_dapscpp_6/CI2_C_Memory_Allocation.m4v Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Memory Allocation for Variables and Early Binding of Methods A function’s locally declared variables placed into run-time stack Storage for newly created object placed into activation record Instantiated objects placed into run-time stack Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Memory Allocation for Variables and Early Binding of Methods Memory location set during compilation Cannot be changed during execution Sometimes early binding and automatic memory management insufficient In context of polymorphism Access of an object outside of the function or method that creates it. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Memory Allocation for Variables and Early Binding of Methods From previous Interlude … Creating a video game with a group of classes to represent three types of boxes Plain box, Toy box, Magic box Function with two arguments: Object of any of the three types of boxes An item of type string Should place item in box with setItem method. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Memory Allocation for Variables and Early Binding of Methods UML class diagram for a family of classes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store Sample program memory layout Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store Run-time stack and free store after myboxPtr points to a MagicBox object and its data member item is set Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store myBoxPtr and the object to which it points Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store Two pointer variables that point to the same object Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store Other issues: Deallocating Memory Avoiding Memory Leaks Poorly written function that allocates memory in the free store Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store (a) Creating the first object; (b) creating the second object; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store (c) assignment causes an inaccessible object Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Pointers and Program’s Free Store View Listing C2-2 Header file for the class GoodMemory Consider Listing C2-3 Implementation file for the class GoodMemory Contrast myLeakyFunction Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Avoiding Dangling Pointers Two pointers referencing (pointing to) the same object Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Avoiding Dangling Pointers Example of a dangling pointer Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Avoiding Dangling Pointers How to avoid dangling pointers Set pointer variables to nullptr either initially or when you no longer need them Test whether a pointer variable contains nullptr before using Don’t delete object in free store until certain no other alias needs it Set all aliases that reference a deleted object to nullptr when object is deleted Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Virtual Methods and Polymorphism Consider Listing C2-4 Revised header file for the class PlainBox Key points about virtual methods A derived class can override Must implement a class’s virtual methods Derived class does not need to override existing implementation of inherited virtual method. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Virtual Methods and Polymorphism Key points about virtual methods Any of a class’s methods may be virtual. Constructors cannot be virtual Destructors can and should be virtual. If you do not want derived class to override a particular method, it should not be virtual Virtual method’s return type cannot be overridden Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Dynamic Allocation of Arrays Ordinary C++ array is statically allocated Use new operator to allocate an array dynamically delete returns dynamically allocated array to system for reuse You can increase size of dynamically allocated array Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A Resizable Array-Based Bag Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013