DOC PREVIEW
Yale CPSC 427 - Object-Oriented Programming
School name Yale University
Pages 23

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineBarGraph Demograph.hppgraph.cpprow.hpprow.cpprowNest.hppStorage ManagemetBells and WhistlesOutline BarGraph Demo Storage Managemet Bells and WhistlesCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 8September 28, 2010CPSC 427a 1/23Outline BarGraph Demo Storage Managemet Bells and WhistlesBarGraph Demograph.hppgraph.cpprow.hpprow.cpprowNest.hppStorage ManagemetBells and WhistlesCPSC 427a 2/23Outline BarGraph Demo Storage Managemet Bells and WhistlesBar Graph DemoWe look at the Bar Graph demo fromChapter 8 of the textbook.CPSC 427a 3/23Outline BarGraph Demo Storage Managemet Bells and Whistlesgraph.hppclass Graph {private:Row* bar[BARS]; // List of bars (aggregation)void insert( char* name, int score );public:Graph ( istream& infile );~Graph();ostream& print ( ostream& out );// Static functions are called without a class instancestatic void instructions() {cout << "Put input files in same directory ""as the executable code.\n";}};inline ostream& operator<<( ostream& out, Graph& G) {return G.print( out );}CPSC 427a 4/23Outline BarGraph Demo Storage Managemet Bells and Whistlesgraph.hppNotes: graph.hppIA Graph consists of an array of pointers to bars.IWe say that it aggregates the bars because they areassociated with the Graph but are not contained within it.IThe bars must be allocated when the Graph is created anddeallocated when the Graph is destroyed. This is done withconstructors and destructors.IThe only constructor builds a Graph by reading an openistream.IThe method insert is used by the constructor. Hence it isdeclared private. It computes which bar an exam scorebelongs to and then puts it there.Iinstructions is a static method. It is called usingGraph::instructions().CPSC 427a 5/23Outline BarGraph Demo Storage Managemet Bells and Whistlesgraph.cppGraph::Graph( istream& infile ) {char initials[4];int score;// Create barsfor (int k=0; k<BARS; ++k) bar[k] = new Row(k);// Fill bars from input streamfor (;;) {infile >> ws; // Skip leading whitespace before get.infile.get(initials, 4, ’ ’); // Safe read.if (infile.eof()) break;infile >> score; // No need for ws before >> num.insert (initials, score); // *** POTENTIAL INFINITE LOOP}}CPSC 427a 6/23Outline BarGraph Demo Storage Managemet Bells and Whistlesgraph.cppNotes: graph.cppThis implements four functions.IGraph() first creates 11 bars and links them to the spinebar[]. This forms a 2D array.IGraph() next reads the scores and fills the graph.Iws skips over leading whitespace.Iget(initials, 4, ’ ’) is a safe way to read initials.IThe destructor ~Graph() deletes the 11 bars.Iinsert() divides the scores 0. . . 99 into 10 intervals.Iprint() delegates the printing of each bar to Row::print().CPSC 427a 7/23Outline BarGraph Demo Storage Managemet Bells and Whistlesrow.hppPrivate class for use by Row.Note friend declaration and private constructor.class Cell{friend class Row;private:Item* data; // Pointer to one data Item (Aggregation)Cell* next; // Pointer to next cell in row (Association)Cell (char* d, int s, Cell* nx) {data = new Item(d, s);next = nx;}~Cell (){ delete data; cerr <<" Deleting Cell " <<"\n"; }};CPSC 427a 8/23Outline BarGraph Demo Storage Managemet Bells and Whistlesrow.hppPublic class represents one bar of the bar graphclass Row { // Interface class for one bar of the bar graph.private:char label[10]; // Row header labelCell* head; // Pointer to first cell of rowpublic:Row ( int n );~Row ();void insert ( char* name, int score ); // delegationostream& print ( ostream& os );};CPSC 427a 9/23Outline BarGraph Demo Storage Managemet Bells and Whistlesrow.hppNotes: row.hppA Row is a list of Item. It is implemented by a linked list of Cell.IThe Cell class is private to Row. Nothing but its name isvisible from the outside.Ifriend class Row allows Row functions to access the privateparts of Cell.ISince all constructors of Cell are private, any attempt toallocate a Row from outside will fail.IEach Cell is initialized when it is created.IRow::head points to the first cell of the linked list.CPSC 427a 10/23Outline BarGraph Demo Storage Managemet Bells and Whistlesrow.cppNotes: row.cppIRow k is labeled by the length 9 string “k0..k9: ”. E.g.,k = 4 ⇒ label is “40..49: ”.ILabel is produced by a safe copy and modify trick:strcpy( label, " 0.. 9: " );label[0] = label[4] = ’0’+ rowNum;I’0’+rowNum converts an integer in [0..9] to the correspondingASCII digit.IAssignment in C++ returns the L-value of its left operand. InC, it returns the R-value of its right operand.ICell created and inserted into linked list in one line!CPSC 427a 11/23Outline BarGraph Demo Storage Managemet Bells and WhistlesrowNest.hppNested classes: rowNest.hppAlternative to Row.Puts entire Cell class definition inside of class Row.Now Cell is private in Row, but everything inside of class Cell ispublic.This obviates the need for Cell to grant friendship toRow and alsocompletely hides Cell—even the name is hidden.Interface is same, so can substitute#include "rowNest.hpp"for#include "row.hpp"in graph.hpp and everything still works!CPSC 427a 12/23Outline BarGraph Demo Storage Managemet Bells and WhistlesStorage managementCPSC 427a 13/23Outline BarGraph Demo Storage Managemet Bells and WhistlesStorage classesC++ supports three different storage classes.1. auto objects are created by variable and parameterdeclarations. (This is the default.)2. static objects are created and initialized at load time andexist until program termination.3. new creates dynamic objects. The exist until explicitlydestroyed by delete or the program terminates.CPSC 427a 14/23Outline BarGraph Demo Storage Managemet Bells and WhistlesAssignment and copyingThe assignment operator = is implicitly defined for all types.Ib=a does a shallow copy from a to b.IShallow copy on objects means to copy all data members fromone object to the other.ICall-by-value uses assignment to copy actual argument tofunction parameter.IIf object contains pointer data members, the pointer is copiedbut not the object it points to. This results inaliasing—multiple pointers to the same object.CPSC 427a 15/23Outline BarGraph Demo Storage Managemet Bells and WhistlesStatic data membersA static class variable must be declared and defined.IA static class member is declared by preceding the memberdeclaration by the qualifier static.IA static class member is defined by having it appear in globalcontext with an initializer but without static.IMust be defined only once.ExampleIn


View Full Document

Yale CPSC 427 - Object-Oriented Programming

Download Object-Oriented Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?