1 / 19

Lab 1

Lab 1. Getting Started. Overview. Using CodeCrunch [Demo] Object Oriented Modeling Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program Header File ( XXXX.h ) Implementation File (XXXX.cpp). Using CodeCrunch. Let's try out lab 0. Modular Design in C++.

tauret
Télécharger la présentation

Lab 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 1 Getting Started

  2. Overview • Using CodeCrunch [Demo] • Object Oriented Modeling • Tutorial 1 Q2 + Q3 [Demo] • Modularizing C++ program • Header File (XXXX.h) • Implementation File (XXXX.cpp) [CG1103 AY1011S2 Lab 1]

  3. Using CodeCrunch Let's try out lab 0

  4. Modular Design in C++ Organizing header and implementation

  5. Modular Design in C++ • A class file can be similarly partitioned into 2 parts: • Header File (XXXX.h) : Contains only the class declaration with no coding • Implementation File (XXXX.cpp): Contains the implementation file • C++ code examples up to this point mix both the declaration, implementation and usage into a single file: • Not a good practice  • Make it harder to reuse classes [CG1103 AY1011S2 Lab 1]

  6. Modular Design : BankAcct.h Example • The above is a correct class header, which includes only declaration but no implementation //BankAcct.h class BankAcct { private: int _acctNum; double _balance; public: BankAcct( int ); BankAcct( int, double ); int withdraw( double ); void deposit( double ); }; Just like function prototype, only data type is important for method parameter Do not forget the “;” for each method prototype [CG1103 AY1011S2 Lab 1]

  7. Modular Design : BankAcct.cpp Example Include the header #include “BankAcct.h” //BankAcct.cpp BankAcct::BankAcct( intaNum ) { _acctNum = aNum; _balance = 0; } BankAcct::BankAcct( intaNum, double amt ) { ... Code not shown ... } intBankAcct::withdraw( double amount ) { if (_balance < amount) return 0; _balance -= amount; return 1; } void BankAcct::deposit( double amount ){ ... Code not shown ... } “BankAcct::” tells the compiler that this method belongs to BankAcct class “BankAcct::” should appears after the return type and before the method name Actual implementation [CG1103 AY1011S2 Lab 1]

  8. Modular Design : User Program • A user program only includes the header file that contains the class declaration • Example: • TestBankAcct.cpp that plays with the BankAcct class //User program: TestBankAcct.cpp #include “BankAcct.h” int main( ) { BankAcct ba1( 1234, 300.50 ); BankAcct ba2( 9999, 1001.40 ); ba1.withdraw(100.00); ba2.withdraw(100.00); } Include the header [CG1103 AY1011S2 Lab 1]

  9. Object Oriented Modeling How to approach problem in OO way

  10. Problem Solving Approach: Review • With procedural programming languages, we usually approach a problem in the following steps: • Identify all information (data) known at the beginning • Identify the desired end result (data) • Figure out the necessary steps to transform (1) into (2) • From (3), modularize the steps into separate functions • Implement the functions in an incremental fashion [CG1103 AY1011S2 Lab 1]

  11. OO Problem Solving Approach • With object oriented languages, the approach is slight different: • Identify objects involved in the problem • Identify the capability (functionality) of the objects • Identify the information ( data ) kept by the objects • Deduce classes from (1) • Generalize the objects found to design the classes • Identify relationship between classes • Use the "is-a" and "has-a" rules to help • "is-a": Potential class hierarchy • "has-a": Association between separate class • Implement the classes in incremental fashion • Implement method by method [CG1103 AY1011S2 Lab 1]

  12. Problem: Surprise! • Let's simulate the following scenario: • We can attach a countdown timer to an alarm. • The duration of the timer can be set. • When the timer runs out, the alarm will be triggered. Timer Alarm [CG1103 AY1011S2 Lab 1]

  13. Step 1: Identify Objects • The Timer object • Main capabilities: • Set duration • Decrease time • Main information: • Duration • Alarm object attached to the timer • The Alarm object • Main capabilities: • Trigger (make noise) • Main information: • None [CG1103 AY1011S2 Lab 1]

  14. Step 2 & 3: Classes and Relationship • Timer is attached to an alarm object upon instantiation • The start() method: • Begin the count down • Invoke trigger() when duration reaches zero Timer Alarm Attached-to • -_durationInSecond : Integer +Timer( aObj: Alarm ) +setDuration( dur: Integer ) +start() ...... +trigger( ) ...... [CG1103 AY1011S2 Lab 1]

  15. Step 4: Implementation • With the class identified, we can now implement the methods • Suggested implementation sequence: • Constructor (if any) • A print() method (or similar) so that we can observe the changes to the internal attributes • Other methods • Use method in (2) to observe and debug [CG1103 AY1011S2 Lab 1]

  16. Common OO Program Structure • Note that the classes represent the "internal" structures of the program • To interact with a program user, we need a driver code to: • Handle input/output • Initialize the necessary object(s) • Invoke the necessary method(s) • The driver code mainly contains: • The main()function • Implement the user/program interaction • A number of functionsto help modularization if the main()functionis complicated [CG1103 AY1011S2 Lab 1]

  17. Program Structure: Illustration • Driver Class • main() method • other helper functions if needed • Predefined Classes • C++Library Classes • User Defined Classes Main program The Solution Classes [CG1103 AY1011S2 Lab 1]

  18. Program Structure: Example classAlarm { public: voidtrigger( ) { cout << "Riinnnggg!!!\n"; } } intmain() { Alarm*aObjPtr= newAlarm; TimermyTimer = new Timer( aObjPtr); intinDuration; cout << "Duration: "; cin >> inDuration; myTimer.setDuration( inDuration ); myTimer.start() ) } } classTimer { private: int_durationInSec; Alarm*_attachedAlarm; public: Timer( Alarm*a ) { _attachedAlarm = a; } voidsetDuration( int duration ) { _durationInSec = duration; } voidstart() { while (_durationInSec > 0) { cout << "Counting.." << endl; _durationInSec--; } _attachedAlarm->trigger(); } } [CG1103 AY1011S2 Lab 1]

  19. Review: Tutorial 1 Q2 and Q3 • Discuss how we use the same approach to arrive at the solution suggested in tutorial Q2 and Q3 [CG1103 AY1011S2 Lab 1]

More Related