CPSC 427: Object-Oriented Programming

Michael J. Fischer

Lecture 13
October 15, 2018

Bar Graph Demo

Overview of bar graph demo These slides refer to demo 13-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.

AWF  00  
MJF  98  
FDR  75  
...

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:

00..09:  AWF 0  
10..19:  
20..29:  
30..39:  PLK 37  
40..49:  
50..59:  ABA 56  
60..69:  PRD 68 RBW 69  
70..79:  HST 79 PDB 71 FDR 75  
80..89:  AEF 89 ABC 82 GLD 89  
90..99:  GBS 92 MJF 98  
Errors:  ALA 105 JBK -1

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.

Analysis of 13-BarGraph demo

main.cpp

Points to note:

Design issues for main.cpp

1.
Should instructions be a static class method or a static constant?
2.
Should fname be a char[] or a string? If the latter, how does one prevent buffer overrun?
3.
Where should the file opening code go – in run() (where it is now), in Graph, or in a new controller class?

graph.hpp

Points to note:

graph.cpp

Points to note:

Design issues for Graph class

1.
Note the use of the C preprocessor to allow preprocessor macro NESTED to cause compilation in two different ways.
2.
Could we declare bar as Row& bar[BARS]? How might this affect the program?
3.
Should initials be a string?
4.
Why is there a potential infinite loop? What should be done about it?

Design issues in main.cpp, graph.hpp, and graph.cpp

row.hpp

Points to note:

row.cpp

Points to note:

rowNest.hpp

This is an alternative definition of class Row with the same public interface and behavior but different internal structure.

Points to note:

Discussion of row.hpp vs. rowNest.hpp

What are the questions you should be asking yourself when deciding which version you prefer?

item.hpp

This is a data class. In C, one would use a struct, but C++ permits tighter semantic control.

Points to note: