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

1 / 16
About This Presentation
Title:

CS 211: Introduction to Computer Programming II

Description:

CS 211: Introduction to Computer Programming II. Instructor: ... { MLBPlayer sammy_sosa('Sammy Sosa', 'Chicago Cubs'); MLBPlayer* bmd_wishes = &sammy_sosa; ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 17
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
  • Introduction to classes
  • Class declaration
  • Instance creation
  • Members
  • Constructors
  • Some mopup
  • Dynamic allocation
  • Binary numbers
  • Next week's schedule
  • Lecture
  • Monday Tuesday
  • Wednesday
  • Quiz 1
  • Friday
  • Quiz 1 back hopefully
  • Reading
  • 6.5 6.10

3
Issues with structs
  • Initialization
  • Not guaranteed
  • Done by hand
  • Packaging datatypes behavior
  • Connecting
  • Reuse / extension
  • Salaried players

4
Basics of classes
  • // An example of declaring
  • // a new user defined type, using the class mech
  • class MLBPlayer
  • public
  • MLBPlayer(char name, char team)
  • private
  • enum Hit SINGLE 0, DOUBLE, TRIPLE, HOMER
  • char name
  • char team
  • int at_bats
  • int walks
  • int hits4
  • float batting_average
  • bool bats_lefty

5
Basics of classes
  • 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
  • strcpy(team, t)
  • at_bats walks 0
  • bats_lefty false
  • batting_average 0.0
  • for (int i 0 I lt 4 i) hitsi 0

6
Basics of classes
  • // Moving some private fields to public for
    convenience
  • class MLBPlayer
  • public
  • MLBPlayer(char name, char team)
  • int at_bats
  • int walks
  • float batting_average
  • private
  • enum Hit SINGLE 0, DOUBLE, TRIPLE, HOMER
  • char name
  • char team
  • int hits4
  • bool bats_lefty

7
Basics of classes
  • include ltiostreamgt
  • using namespace std
  • include "MLBPlayer.h"
  • // Accessing public member variables
  • int main(int argc, char argv)
  • MLBPlayer sammy_sosa("Sammy Sosa", "Chicago
    Cubs")
  • MLBPlayer bmd_wishes sammy_sosa
  • sammy_sosa.at_bats 1
  • int total_hits 0
  • for (int i 0 i lt 4 i)
  • total_hits sammy_sosa.hitsi
  • bmd_wishes-gtbatting_average
  • total_hits / bmd_wishes-gtat_bats
  • return 0

8
Basics of classes
  • // Instead of public fields, let's write public
    accessor funcs
  • class MLBPlayer
  • public
  • MLBPlayer(char name, char team)
  • 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

9
Basics of classes
  • 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

10
Basics of classes
  • float MLBPlayerbattingAverage()
  • return totalHits() / (at_bats 1.0f)
  • void MLBPlayerstrikeout()
  • at_bats

11
Basics of classes
  • include ltiostreamgt
  • using namespace std
  • include "MLBPlayer.h"
  • // Now using public accessor functions
  • int main(int argc, char argv)
  • MLBPlayer sammy_sosa("Sammy Sosa", "Chicago
    Cubs")
  • MLBPlayer bmd_wishes sammy_sosa
  • sammy_sosa.addHit(MLBPlayerSINGLE)
  • sammy_sosa.strikeout()
  • sammy_sosa.strikeout()
  • float ba sammy_sosa.battingAverage()
  • return 0

12
Basics of classes
  • include ltiostreamgt
  • using namespace std
  • include "MLBPlayer.h"
  • // Now using public accessor functions
  • int main(int argc, char argv)
  • MLBPlayer sammy_sosa("Sammy Sosa", "Chicago
    Cubs")
  • MLBPlayer bmd_wishes sammy_sosa
  • bmd_wishes-gtaddHit(MLBPlayerSINGLE)
  • bmd_wishes-gtstrikeout()
  • bmd_wishes-gtstrikeout()
  • float ba bmd_wishes-gtbattingAverage()
  • return 0

13
Basics of classes
  • // Use overloading to define default constructor
  • class MLBPlayer
  • public
  • MLBPlayer(char name, char team)
  • MLBPlayer()
  • 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

14
Basics of classes
  • include "MLBPlayer.h"
  • // Stuff elided
  • MLBPlayerMLBPlayer()
  • char n "Joe Schlabotnik"
  • char t "Waffletown Syrups"
  • int cnt strlen(n) 1
  • name new charcnt
  • strcpy(name, n)
  • cnt strlen(t) 1
  • strcpy(team, t)
  • at_bats walks 0
  • bats_lefty false
  • batting_average 0.0
  • for (int i 0 i lt 4 i) hitsi 0

15
Basics of classes
  • include ltiostreamgt
  • using namespace std
  • include "MLBPlayer.h"
  • int main(int argc, char argv)
  • // Imagine all our old stuff here.
  • MLBPlayer black_sox9
  • MLBPlayer joe_s new MLBPlayer
  • joe_s new MLBPlayer()
  • joe_s new MLBPlayer("Joe Schlabotnik",
    "Waffletown Syrups")
  • return 0

16
Dynamic allocation
  • include ltcstringgt
  • // Leaves us with a dangling pointer, also wastes
    space
  • char dup_string(char s)
  • const int max_buf 1024
  • char bufmax_buf
  • strcpy(buf, s)
  • return buf
  • // Using dynamic allocation we're okay
  • char dup_string(char s)
  • int cnt strlen(s) 1
  • char new_string new charcnt
  • return new_string

17
Thats a Wrap
  • Take away
  • Classes are an improved form of structs
  • Know how to declare classes, define member
    functions
  • Dynamic allocation
  • Space managed by program at run time
  • Binary representations
Write a Comment
User Comments (0)
About PowerShow.com