this Pointer and Dynamic Memory Management - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

this Pointer and Dynamic Memory Management

Description:

this Pointer and Dynamic Memory Management ... programmers can control the allocation and deallocation of memory for any user-defined or built-in data type The ... – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 9
Provided by: Pacif5
Category:

less

Transcript and Presenter's Notes

Title: this Pointer and Dynamic Memory Management


1
this Pointer and Dynamic Memory Management
2
Cascading Member Function Calls
  • Lets examine fig. 7.14 - 7.16

3
Dynamic Memory Management
  • In C, programmers can control the allocation
    and deallocation of memory for any user-defined
    or built-in data type
  • The operators used are
  • new
  • delete
  • Must also have
  • include ltnewgt

4
Example
  • Time pTime1
  • pTime1 new Time
  • pTime1-gtprint()
  • Time pTime2
  • pTime2 new Time( 15, 2, 59 )
  • pTime2-gtprint()
  • Time t( 2, 23, 90 )
  • Time pTime3 t
  • pTime3-gtprint()

5
Example
  • What would happen if we output the values of each
    of the pointers on the previous slides
  • cout ltlt pTime1 ltlt endl
  • How would we free the space pointed to by the
    pointers

6
Problem
  • Create a class called IntegerSet where a set is
    represented as an array of ones and zeros. Array
    element ai is 1 if integer i is in the set and
    array element aj is 0 if integer j is not in
    the set
  • An object of type IntegerSet is instantiated by
    passing to the constructor an integer
    representing the range of the set.
  • IntegerSet mySet( 100 )

7
integerset.h
  • class IntegerSet
  • public
  • IntegerSet( int )
  • IntegerSet( const IntegerSet )
  • IntegerSet unionOfIntegerSets( const
    IntegerSet )
  • IntegerSet intersectionOfIntegerSets( const
    IntegerSet )
  • void emptySet()
  • void inputSet()
  • void insertElement( int )
  • void deleteElement( int )
  • void setPrint() const
  • bool isEqualTo( const IntegerSet ) const
  • private
  • int set // dynamically allocated set
  • int size
  • // function validEntry definition
  • bool validEntry( int x ) const
  • return x gt 0 x lt size

8
Summary
  • Today we covered
  • this Pointer
  • Dynamic memory management
  • Completed pages 489 - 497
Write a Comment
User Comments (0)
About PowerShow.com