Lab 2 Diagrams Cop 4232 Fall 2005 Lab

  • Slides: 13
Download presentation
Lab 2 Diagrams Cop 4232 Fall 2005 Lab 2 Cop 4232

Lab 2 Diagrams Cop 4232 Fall 2005 Lab 2 Cop 4232

IOMgmt IOError In. Mgr Token. Error Out. Mgr pgm. In. Mgr pgm. Out. Mgr

IOMgmt IOError In. Mgr Token. Error Out. Mgr pgm. In. Mgr pgm. Out. Mgr Fall 2005 Tokenizer String. Tokenizer Lab 2 Cop 4232 2

Sim. Mgmt & Sim. Models Sim. Mgmt Sim. Models Agent #In. Mgr& fin. Mgr

Sim. Mgmt & Sim. Models Sim. Mgmt Sim. Models Agent #In. Mgr& fin. Mgr //class #Out. Mgr& fout. Mgr //class -string name //instance +Agent() +Agent(Tokenizer& ) +Agent(string ) +Insert() +Extract(Tokenizer&) +Extract(string) +operator>>(ifstream&, Agent&) +operator<<(ostream&, Agent&) +Name. Of()=>string +In. Mgr. Of()=>In. Mgr& +Out. Mgr. Of()=>Out. Mgr& +set. In. Mgr(In. Mgr&) +set. Out. Mgr(Out. Mgr&) #Put() #Get(Tokenizer&) #Get(String. Tokenizer&) Fall 2005 Customer -Money cash -string paymethod -Credit. Card bankcard +Customer() +Customer(Tokenizer& ) +Customer(string ) +Extract(Tokenizer&) +Extract(string) +Insert() #Get(Tokenizer&) #Get(String. Tokenizer&) #Put() Lab 2 Cop 4232 Pump. Station -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected + Pump. Station() + Pump. Station(Tokenizer& ) + Pump. Station(string ) +Extract(Tokenizer&) +Extract(string) +Insert() #Get(Tokenizer&) #Get(String. Tokenizer&) #Put() 3

Sim. Mgmt & Sim. Models Version #1 In. Mgr and Out. Mgr are NOT

Sim. Mgmt & Sim. Models Version #1 In. Mgr and Out. Mgr are NOT USED! Agent -string name //instance +Agent() +Agent( ifstream& ) +Agent(Tokenizer& ) +Agent(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) +operator>>(ifstream&, Agent&) +operator<<(ostream&, Agent&) +Name. Of()=>string #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( String. Tokenizer& ) Fall 2005 Customer -Money cash -string paymethod -Credit. Card bankcard +Customer() +Customer( ifstream& ) +Customer(Tokenizer& ) +Customer(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( String. Tokenizer& ) Lab 2 Cop 4232 Pump. Station -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected +Pump. Station() +Pump. Station( ifstream& ) +Pump. Station(Tokenizer& ) +Pump. Station(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( String. Tokenizer& ) 4

Client A for Version #1 int main() { ifstream fin; ostream fout; string fin.

Client A for Version #1 int main() { ifstream fin; ostream fout; string fin. Name, fout. Name; cout << “Enter input file name: “; cin >> fin. Name; cout << “Enter output file name: “; cin >> fout. Name; fin. open( fin. Name. c_str()); fout. open( fout. Name. c_str()); Input File Agent{ name: BETA }Agent{ name: GAMMA }Agent{ name: ALPHA }Agent alpha, beta(fin), gamma; fin >> gamma; alpha. Extract(fin); fout << alpha << endl; beta. Insert(fout); fout << gamma << endl; Output File Output Style 1 Agent{ name: ALPHA }Agent{ name: BETA }Agent{ name: GAMMA }Agent } Problem Formatting the output image is limited. To achieve the indented style, each instance must begin on a new line of the output stream. Agent{ name: ALPHA }Agent Output Style 2 Agent{ name: BETA }Agent{ name: GAMMA }Agent Fall 2005 Lab 2 Cop 4232 5

Client B for Version #1 int main() { Tokenizer T; //Tokenizer encapsulates input stream

Client B for Version #1 int main() { Tokenizer T; //Tokenizer encapsulates input stream Agent alpha, beta(T), gamma(T); alpha. Extract(T); Input File Agent{ name: BETA }Agent{ name: GAMMA }Agent{ name: ALPHA }Agent string Image = “ Agent{ name: DELTA }Agent “; Agent delta( Image ); //Hidden use of String. Tokenizer fout << alpha << endl; beta. Insert(fout); fout << gamma << endl; fout << delta << endl; or } Problem Formatting the output image is limited. To achieve the indented style, each instance must begin on a new line of the output stream. Output File Output Style 1 Agent{ name: ALPHA }Agent{ name: BETA }Agent{ name: GAMMA }Agent{ name: DELTA }Agent{ name: ALPHA }Agent Output Style 2 Agent{ name: BETA }Agent{ name: GAMMA }Agent{ name: DELTA }Agent Fall 2005 Lab 2 Cop 4232 6

Sim. Mgmt & Sim. Models Version #2 In. Mgr and Out. Mgr are USED!

Sim. Mgmt & Sim. Models Version #2 In. Mgr and Out. Mgr are USED! Agent -string name //instance +Agent() +Agent( In. Mgr& ) +Agent( Tokenizer& ) +Agent( string ) +Insert( Out. Mgr& ) +Extract( In. Mgr& ) +Extract( Tokenizer& ) +Extract( string ) +operator>>(ifstream&, Agent&) +operator<<(ostream&, Agent&) +Name. Of()=>string #Put( Out. Mgr& ) #Get( In. Mgr& ) #Get( Tokenizer& ) #Get( String. Tokenizer& ) Fall 2005 Customer -Money cash -string paymethod -Credit. Card bankcard +Customer() +Customer( In. Mgr& ) +Customer( Tokenizer& ) +Customer( string ) +Insert( Out. Mgr& ) +Extract( In. Mgr& ) +Extract( Tokenizer& ) +Extract( string ) #Put( Out. Mgr& ) #Get( In. Mgr& ) #Get( Tokenizer& ) #Get( String. Tokenizer& ) Lab 2 Cop 4232 Pump. Station -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected +Pump. Station() +Pump. Station( In. Mgr& ) +Pump. Station( Tokenizer& ) +Pump. Station( string ) +Insert( Out. Mgr& ) +Extract( In. Mgr& ) +Extract( Tokenizer& ) +Extract( string ) #Put( Out. Mgr& ) #Get( In. Mgr& ) #Get( Tokenizer& ) #Get( String. Tokenizer& ) 7

Client A for Version #2 Input File int main() { In. Mgr fin. Mgr;

Client A for Version #2 Input File int main() { In. Mgr fin. Mgr; In. Mgr& fin = fin. Mgr. get. Stream(); Out. Mgr fout. Mgr; Out. Mgr& fout = fout. Mgr. get. Stream(); Agent{ name: BETA }Agent{ name: GAMMA }Agent{ name: ALPHA }Agent alpha, beta(fin. Mgr), gamma; fin >> gamma; alpha. Extract(fin. Mgr); Output File fout << alpha << endl; //<< cannot use fout. Mgr beta. Insert(fout. Mgr); //previous line compromises fout. Mgr fout << gamma << endl; //invalidates state of fout. Mgr Output Style 1 Agent{ name: ALPHA }Agent{ name: BETA }Agent{ name: GAMMA }Agent } Problem Use of insertion operators in client may corrupt or invalidate the state of Out. Mgr. The implementation of << cannot know the associated Out. Mgr (and there may not be one)! If the client inserts its own formatting operations (e. g. endl) then the state of the associated Out. Mgr becomes invalid and subsequent output may not be formatted correctly. To correct this, operator<< should not be defined as a friend. Defining operator>> is permitted as a friend. In fact, Extract() and Get() can be defined to take ifstream& as a parameter – it is not necessary to use In. Mgr& Fall 2005 Agent{ name: ALPHA }Agent Output Style 2 Agent{ name: BETA }Agent{ name: GAMMA }Agent Lab 2 Cop 4232 8

Sim. Mgmt & Sim. Models Version #3 Agent In. Mgr and Out. Mgr are

Sim. Mgmt & Sim. Models Version #3 Agent In. Mgr and Out. Mgr are Encapsulated by Agent! #In. Mgr& fin. Mgr //class #Out. Mgr& fout. Mgr //class -string name //instance +Agent() +Agent(Tokenizer& ) +Agent(string ) +Insert() //uses In. Mgr +Extract() //uses Out. Mgr +Extract(Tokenizer&) +Extract(string) +operator>>(ifstream&, Agent&) +operator<<(ostream&, Agent&) +Name. Of()=>string +In. Mgr. Of()=>In. Mgr& +Out. Mgr. Of()=>Out. Mgr& +set. In. Mgr(In. Mgr&) +set. Out. Mgr(Out. Mgr&) #Put() //uses Out. Mgr #Get() //uses In. Mgr #Get(Tokenizer&) #Get(String. Tokenizer&) Fall 2005 Customer -Money cash -string paymethod -Credit. Card bankcard +Customer() +Customer(Tokenizer& ) +Customer(string ) +Extract(Tokenizer&) +Extract(string) +Insert() #Get(Tokenizer&) #Get(String. Tokenizer&) #Put() Lab 2 Cop 4232 Pump. Station -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected + Pump. Station() + Pump. Station(Tokenizer& ) + Pump. Station(string ) +Extract(Tokenizer&) +Extract(string) +Insert() #Get(Tokenizer&) #Get(String. Tokenizer&) #Put() 9

Client A for Version #3 Input File int main() { In. Mgr& fin. Mgr

Client A for Version #3 Input File int main() { In. Mgr& fin. Mgr = Agent: : In. Mgr. Of(); //access class attributes Out. Mgr& fout. Mgr = Agent: : Out. Mgr. Of(); //access class attributes ifstream& fin = fin. Mgr. get. Stream(); ostream& fout = fout. Mgr. get. Stream(); Agent{ name: BETA }Agent{ name: GAMMA }Agent{ name: ALPHA }Agent alpha, beta, gamma; beta. Extract(); fin >> gamma; alpha. Extract(); Output File fout << alpha; //verifies that fout is associated with fout. Mgr beta. Insert(); //uses fout. Mgr fout << gamma; //verifies that fout is associated with fout. Mgr. new. Line(); Agent{ name: ALPHA }Agent{ name: BETA }Agent{ name: GAMMA }Agent } Fall 2005 Lab 2 Cop 4232 10

Lab 2 Fall 05 App. Error IOMgmt Token. Error Tokenizer IOError Sim. Mgmt String.

Lab 2 Fall 05 App. Error IOMgmt Token. Error Tokenizer IOError Sim. Mgmt String. Tokenizer Out. Mgr In. Mgr pgm. Out. Mgr Agent pgm. In. Mgr Sim. Models Jiffy. Stop 1. . * Customer Pump. Station Money Fall 2005 Credit. Card Lab 2 Cop 4232 11

Lab 1 Figures A-1, 2, 3, 4 File Org Line Org Cur pos MARGINSIZE

Lab 1 Figures A-1, 2, 3, 4 File Org Line Org Cur pos MARGINSIZE Margin Stack (before) idx Stack (after) idx Method: push. Margin() File Org Line Org Cur pos Stack (before) Curpos (before) Stack (after) idx File Org Line Org idx Method: new. Line() Method: pop. Margin() Curpos (after) File Org New fin Cur fin New fin (delta < 0) (delta > 0) Method: delta. Margin(input) Fall 2005 Lab 2 Cop 4232 12

Figures 3 -6, 7, 8 diff File Org blanks Cur pos Line Org Cur

Figures 3 -6, 7, 8 diff File Org blanks Cur pos Line Org Cur pos (delta > 0) File Org New fout Cur fout New fout diff Line Org idx Cur pos (delta < 0) Method: delta. Margin(output) Method: to. Margin(output) X{ XData 1: value 1 XData 2: value 2 XData 3: Y{ YData 1: value 1 YData 2: value 2 Instance of X }Y }X Fall 2005 Embedded instance of Y XDatak: valuek Lab 2 Cop 4232 13