19
Lab 1 Getting Started

Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program Header File (XXXX.h)

Embed Size (px)

Citation preview

Page 1: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Lab 1

Getting Started

Page 2: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Overview Using CodeCrunch [Demo]

Object Oriented Modeling Tutorial 1 Q2 + Q3 [Demo]

Modularizing C++ program Header File (XXXX.h) Implementation File (XXXX.cpp)

2[CG1103 AY1011S2 Lab 1]

Page 3: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Using CodeCrunch

Let's try out lab 0

Page 4: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Modular Design in C++

Organizing header and implementation

Page 5: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

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

5[CG1103 AY1011S2 Lab 1]

Page 6: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Modular Design : BankAcct.h Example//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

The above is a correct class header, which includes only declaration but no implementation

6[CG1103 AY1011S2 Lab 1]

Page 7: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Modular Design : BankAcct.cpp Example#include “BankAcct.h”//BankAcct.cpp

BankAcct::BankAcct( int aNum ){ _acctNum = aNum; _balance = 0;}

BankAcct::BankAcct( int aNum, double amt ){ ... Code not shown ... }

int BankAcct::withdraw( double amount ){ if (_balance < amount) return 0; _balance -= amount; return 1; }

void BankAcct::deposit( double amount ){ ... Code not shown ... }

Include the header

“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

7[CG1103 AY1011S2 Lab 1]

Page 8: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

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

8[CG1103 AY1011S2 Lab 1]

Page 9: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Object Oriented Modeling

How to approach problem in OO way

Page 10: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Problem Solving Approach: Review With procedural programming languages, we

usually approach a problem in the following steps:1. Identify all information (data) known at the

beginning

2. Identify the desired end result (data)

3. Figure out the necessary steps to transform (1) into (2)

4. From (3), modularize the steps into separate functions

5. Implement the functions in an incremental fashion

10[CG1103 AY1011S2 Lab 1]

Page 11: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

OO Problem Solving Approach With object oriented languages, the approach

is slight different:1. Identify objects involved in the problem

i. Identify the capability (functionality) of the objects

ii. Identify the information ( data ) kept by the objects

2. Deduce classes from (1) Generalize the objects found to design the classes

3. 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

4. Implement the classes in incremental fashion Implement method by method

11[CG1103 AY1011S2 Lab 1]

Page 12: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

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

12[CG1103 AY1011S2 Lab 1]

Page 13: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

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

13[CG1103 AY1011S2 Lab 1]

Page 14: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

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-_durationInSecond : Integer

+Timer( aObj: Alarm )

+setDuration( dur: Integer )

+start()

......

Alarm

+trigger( )

......

Attached-to

14[CG1103 AY1011S2 Lab 1]

Page 15: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Step 4: Implementation With the class identified, we can now

implement the methods

Suggested implementation sequence:1. Constructor (if any)

2. A print() method (or similar) so that we can observe the changes to the internal attributes

3. Other methods Use method in (2) to observe and debug

15[CG1103 AY1011S2 Lab 1]

Page 16: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

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 functions to help modularization if the main() function is complicated

16[CG1103 AY1011S2 Lab 1]

Page 17: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Program Structure: Illustration

Driver Class

• main() method

• other helper functions if needed

Main program

Predefined Classes

• C++ Library Classes

User Defined Classes

The Solution Classes

17[CG1103 AY1011S2 Lab 1]

Page 18: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Program Structure: Example

int main() { Alarm* aObjPtr = new Alarm; Timer myTimer = new Timer( aObjPtr ); int inDuration;

cout << "Duration: "; cin >> inDuration; myTimer.setDuration( inDuration );

myTimer.start() ) }

}

class Timer {

private: int _durationInSec; Alarm* _attachedAlarm;

public: Timer( Alarm* a ) { _attachedAlarm = a; }

void setDuration( int duration ) { _durationInSec = duration; }

void start() { while (_durationInSec > 0) { cout << "Counting.." << endl; _durationInSec--; } _attachedAlarm->trigger(); }}

class Alarm {public: void trigger( ) { cout << "Riinnnggg!!!\n"; }}

18[CG1103 AY1011S2 Lab 1]

Page 19: Lab 1 Getting Started. Overview Using CodeCrunch [Demo] Object Oriented Modeling  Tutorial 1 Q2 + Q3 [Demo] Modularizing C++ program  Header File (XXXX.h)

Review: Tutorial 1 Q2 and Q3 Discuss how we use the same approach to

arrive at the solution suggested in tutorial Q2 and Q3

19[CG1103 AY1011S2 Lab 1]