CS 211: Introduction to Computer Programming II - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CS 211: Introduction to Computer Programming II

Description:

'Chicago Cubs'); cubs_starters[3] = MLBPlayer('Sammy Sosa', 'Chicago Cubs'); pinch_hit(8, 'Tom Goodwin'); Instance deallocation // stuff from above ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 15
Provided by: briand8
Category:

less

Transcript and Presenter's Notes

Title: CS 211: Introduction to Computer Programming II


1
CS 211Introduction to Computer Programming II
  • Instructor Brian M. Dennis
  • Teaching Assistants
  • Tom Lechner, Bin Lin, Rachel Goldsborough
  • http//www.cs.northwestern.edu/bmd/cs211/

2
Todays Topics
  • Destructors
  • The delete operator
  • setters/getters
  • Extended example
  • Linked list
  • Struct version
  • Class version
  • Administrivia
  • Reading 6.12, 6.13, 6.14, 6.16
  • Appendix C if you need it

3
Assignment/delete
  • Two ops defined on all classes
  • Assignment
  • Memberwise bit copy
  • Delete
  • Calls a no-op destructor
  • What's a destructor?

4
C Class Mechanism
  • Data abstraction feature
  • User defined datatype
  • Collection
  • Compound data
  • Heterogeneous
  • Typed fields
  • Indexed by name
  • Access control
  • Information hiding
  • Controlled initialization deallocation
  • Encapsulation
  • data operations on that data

5
Class basics
  • // Use overloading to define default constructor
  • class MLBPlayer
  • public // Access specifier
  • MLBPlayer(char name, char team) //
    Constructor
  • MLBPlayer() // Default constructor
  • // Member functions
  • int totalHits()
  • float battingAverage()
  • void addHit(int hit)
  • void strikeout()
  • enum Hit SINGLE 0, DOUBLE, TRIPLE, HOMER
  • private
  • // Member variables
  • char name char team
  • int at_bats int walks
  • int hits4
  • float batting_average bool bats_lefty

6
Class basics
  • include "MLBPlayer.h"
  • include ltcstringgt
  • MLBPlayerMLBPlayer(char n, char t)
  • int cnt strlen(n) 1
  • name new charcnt
  • strcpy(name, n)
  • cnt strlen(t) 1
  • team new charcnt // fixed up from last
    lecture
  • strcpy(team, t)
  • at_bats walks 0
  • bats_lefty false
  • batting_average 0.0
  • for (int i 0 I lt 4 i) hitsi 0

7
Class basics
  • int MLBPlayertotalHits()
  • int hits_count 0
  • for (int i 0 i lt 4 i)
  • hits_count hitsi
  • return hits_count
  • void MLBPlayeraddHit(int hit)
  • at_bats
  • if ((SINGLE lt hit) (hit lt HOMER))
  • hitshit 1

8
Memberwise copy
  • include "MLBPlayer.h"
  • include ltcstringgt
  • int main(int argc, char argv)
  • MLBPlayer cubs_starters9
  • cubs_starters0 MLBPlayer("Corey Patterson",
  • "Chicago Cubs")
  • cubs_starters3 MLBPlayer("Sammy Sosa",
  • "Chicago Cubs")
  • pinch_hit(8, "Tom Goodwin")

9
Instance deallocation
  • // stuff from above
  • bool pinch_hit(int pos, char name,
  • MLBPlayer lineup)
  • MLBPlayer on_deck(name,
    lineuppos.getTeam())
  • if (!calledBack(name))
  • lineuppos on_deck
  • return false
  • else
  • return true
  • // What happens to on_deck here?

10
Destructors
  • class MLBPlayer
  • public
  • MLBPlayer(char name, char team)
  • MLBPlayer()
  • MLBPlayer() // Destructor for the class
  • enum Hit SINGLE 0, DOUBLE, TRIPLE, HOMER
    int totalHits()
  • float battingAverage()
  • void addHit(int hit)
  • void strikeout()
  • private
  • char name char team
  • int at_bats int walks int hits4
  • float batting_average
  • bool bats_lefty

11
Destructors
  • // Defining the (and only) constructor
  • // for a class
  • MLBPlayerMLBPlayer()
  • delete team // Give back space for team
  • delete name // Give back space for name
  • // Except now we have another problem!!

12
Destructors
  • // stuff from above
  • bool pinch_hit(int pos, char name,
  • MLBPlayer lineup)
  • MLBPlayer on_deck(name,
    lineuppos.getTeam())
  • if (!calledBack(name))
  • lineuppos on_deck // But we member copied!
  • return false
  • else
  • return true
  • // On deck gets cleaned up!

13
Some delete examples
  • include "MLBPlayer.h"
  • include ltcstringgt
  • int main(int argc, char argv)
  • MLBPlayer cubs_starters9
  • MLBPlayer leadoff_hitter cubs_starters0
  • MLBPlayer closer new MLBPlayer("Eric Gagne",
  • "LA Dodgers")
  • MLBPlayers dodgers new MLBPlayers9
  • delete cubs_starters
  • delete cubs_starters
  • delete leadoff_hitter
  • delete closer
  • delete dodgers
  • delete dodgers

14
Constructor/destructor exec
  • include "MLBPlayer.h"
  • MLBPlayer ichiro("Ichiro", "Seattle Mariners")
  • int main(int argc, char argv)
  • MLBPlayer cubs_starters9
  • MLBPlayer local_player
  • on_injured_reserve(local_player)
  • MLBPlayer dynamic_player new MLBPlayer
  • MLBPlayer big_unit("Randy Johnson",
    "Diamondbacks")
  • return 0
  • void on_injured_reserve(MLBPlayer p)
  • static cnt 0
  • static MLBPlayer reserve4
  • reservecnt p
  • cnt

15
Linked list example
  • Time permitting

16
Thats a Wrap
  • Takeaways
  • Memberwise copy
  • Instance allocation deallocation
  • Setters and getters
Write a Comment
User Comments (0)
About PowerShow.com