CPSC 427: Object-Oriented Programming
Michael J. Fischer
Notes on PS2
Comments on PS2
Storage Management (continued)
Recall from previous lecture Object properties:
Storage area
Every object is represented by a block of storage in memory.
This memory has an internal machine address, which is not normally
visible to the programmer.
The size of the storage area is determined by the type of the object.
Lifetime
Each object has a lifetime.
The lifetime begins when the object is created or allocated.
The lifetime ends when the object is deleted or deallocated.
Storage class
C++ supports three different storage classes.
Assignment and copying
The assignment operator = is implicitly defined for all types.
Static data members
A static class data member must be declared and defined.
Example
In mypack.hpp file, inside class definition for MyPack:
static int instances; // count # instantiations
In mypack.cpp file:
Static function members
Function members can also be declared static.
Five common kinds of failures
Read the textbook for examples of how these happen and what to do about them.
Bar Graph Demo
Overview
of
bar
graph
demo
These slides refer to demo 09-BarGraph.
This demo reads a file of student exam scores, groups them by deciles,
and then displays a bar graph for each decile.
The input file has one line per student containing a 3-letter student code followed by a numeric score.
Scores should be in the range [0,100]
Overview
(cont.)
The output consists of one line for each group listing all of the students
falling in that group. An 11th line is used for students with invalid
scores.
Sample output:
Method
Each student is represented by an Item object that consists of the
initials and a score.
The program maintains 11 linked lists of Item, one for each bar of the
graph. A bar is represented by a Row object.
For each line of input, an Item is constructed, classified, and inserted
into the appropriate Row.
When all student records have been read in, the bars are printed.
A Graph object contains the bar graph as well as the logic for creating a bar graph from a file of scores as well as for printing it out.
graphM.cpp
Points to note: