 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 6 Outline 6.1 Introduction 6.2 Implementation: Visibility 6.3 Implementation:

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System.
Advertisements

1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
CS-2303 System Programming Concepts
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
1 Object Oriented Analysis and Design. 2 Object-Oriented Analysis  Statement of what our client wants Object-Oriented Design  How to provide it using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 5 Outline 5.1 Introduction 5.2 Collaborations 5.3 Creating Collaborations.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 7 Outline 7.1 Introduction 7.2 Overview of Simulation Implementation.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 4 Outline 4.1 Introduction 4.2 Class Operations 4.3 Creating Class.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 3 Outline 3.1 Introduction 3.2 Class Attributes 3.3 Statechart Diagrams.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
 2002 Prentice Hall, Inc. All rights reserved. Appendix H – Elevator Model Outline H.1 Introduction H.2 Class ElevatorModel H.3Classes Location and Floor.
Chapter 10 Introduction to Classes
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 2 Outline 2.1 Introduction 2.2 Thinking About Objects: Identifying.
11 Introduction to Object Oriented Programming (Continued) Cats.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
 2003 Prentice Hall, Inc. All rights reserved. Appendix E – Elevator Model Outline E.1 Introduction E.2 Class ElevatorSimulation E.3Classes Location and.
 2006 Doan Van Ban, IOIT. All rights reserved. 1 Case Study: E levator System Simulation Program Goal –Software simulator application –N-floor elevator.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Chapter 22 - C++ Templates
Chapter 9 More on Objects and Classes
Review: Two Programming Paradigms
Optional Case Study - Chapter 4
Introduction to Classes
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Chapter 4 - Control Structures: Part 1
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
SPL – PS3 C++ Classes.
Object Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 6 Outline 6.1 Introduction 6.2 Implementation: Visibility 6.3 Implementation: Handles 6.4 Implementation: Class Header Files Bell Clock Door Light Building FloorButton ElevatorButton FloorButton Scheduler Floor Elevator 6.5 Conclusion

 2000 Deitel & Associates, Inc. All rights reserved. 6.1 Introduction Chapters 1 to 5 –Fundamentals of object orientation –Developed an object-oriented design Body of Chapter 6 –Details of programming with C++ classes This section –Begin implementing our design in C++ –Use UML class diagram to outline C++ header files Define classes

 2000 Deitel & Associates, Inc. All rights reserved. 6.2 Implementation: Visibility public or private ? –Data members generally private visibility –Member functions and operations? Invoked by clients Therefore, must be public visibility In UML ( + ) indicates public ( - ) indicates private –As we write header files, place + items into public, - items into private

 2000 Deitel & Associates, Inc. All rights reserved. 6.2 Implementation: Visibility (II) Building + runSimulation( ) : void - on : bool = false Light + turnOff( ) : void + turnOn( ) : void + ringBell( ) : void Bell - pressed : bool = false FloorButton + pressButton( ) : void + resetButton( ) : void - pressed : bool = false ElevatorButton + pressButton( ) : void + resetButton( ) : void Person - ID : int + stepOntoFloor( ) : void + exitElevator( ) : void + enterElevator( ) : void - time : int = 0 Clock + getTime( ) : int + tick( ) : void - occupied : bool = false Floor + elevatorArrived( ) : void + isOccupied( ) : bool + personArrives( ) :void - open : bool = false Door + openDoor( ) : void + closeDoor( ) : void Scheduler - floor1ArrivalTime : int - floor2ArrivalTime : int + processTime( time : int ) : void Elevator - currentFloor : int = 1 - direction : enum = up - capacity : int = 1 - arrivalTime : int - moving : bool = false + summonElevator( ) : void + prepareToLeave( ) : void + processTime( time : int ) : void + personEnters( ) : void + personExits( ) : void

 2000 Deitel & Associates, Inc. All rights reserved. 6.3 Implementation: Handles Handles –For an object of class A to communicate with an object of class B, it must have a handle to the object of class B Know name, hold a reference, or have a pointer –Body of Chapter 6 discussed how to implement handles as references and pointers References become attributes of the class –Until we discuss composition in chapter 7, we cannot implement all the classes we have Next Slide –List of classes and the classes they must have handles to

 2000 Deitel & Associates, Inc. All rights reserved. 6.3 Implementation: Handles (II)

 2000 Deitel & Associates, Inc. All rights reserved. 6.4 Implementation: Class Header Files Examine header files for each class –To demonstrate how constructors and destructors run, we will code them to display messages (implementation in Chapter 7)

 2000 Deitel & Associates, Inc. All rights reserved. Outline Bell Header public: Constructor, destructor, and function ringBell 1// bell.h 2// Definition for class Bell. 3#ifndef BELL_H 4#define BELL_H 5 6class Bell { 7public: 8 Bell(); // constructor 9 ~Bell(); // destructor 10 void ringBell(); // ring the bell 11}; 12 13#endif // BELL_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Clock Header public: Constructor, destructor, tick, and getTime private: time attribute In simulation, Building increments time with tick once per second, and gets current value with getTime 1// clock.h 2// Definition for class Clock. 3#ifndef CLOCK_H 4#define CLOCK_H 5 6class Clock { 7public: 8 Clock(); // constructor 9 ~Clock(); // destructor 10 void tick(); // increment clock by one second 11 int getTime(); // returns clock's current time 12private: 13 int time; // clock's time 14}; 15 16#endif // CLOCK_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Person Header Person objects created dynamically, so must be implemented differently (more Chapter 7) public: Constructor, destructor, operations private: ID number 1// person.h 2// definition of class Person 3#ifndef PERSON_H 4#define PERSON_H 5 6class Person { 7public: 8 Person( int ); // constructor 9 ~Person(); // destructor 10 int getID(); // returns person's ID void stepOntoFloor(); 13 void enterElevator(); 14 void exitElevator() 15private: 16 int ID; // person's unique ID # 17}; 18 19#endif // PERSON_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Door Header public: Constructor, destructor, member functions private: open status Door needs a handle to a Person object, which is dynamically created (more Chapter 7) 1// door.h 2// Definition for class Door. 3#ifndef DOOR_H 4#define DOOR_H 5 6class Door { 7public: 8 Door(); // constructor 9 ~Door(); // destructor void openDoor(); 12 void closeDoor(); 13private: 14 bool open; // open or closed 15}; 16 17#endif // DOOR_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Light Header public: Constructor, destructor, member functions private: data member on We need to distinguish between the two lights (one on each floor) Name each light (declare char *name ) Add char* parameter to constructor so it can initialize name 1// light.h 2// Definition for class Light. 3#ifndef LIGHT_H 4#define LIGHT_H 5 6class Light { 7public: 8 Light( char * ); // constructor 9 ~Light(); // destructor 10 void turnOn(); // turns light on 11 void turnOff(); // turns light off 12private: 13 bool on; // on or off 14 char *name; 15}; 16 17#endif // LIGHT_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Building Header public: Constructor, destructor, member function A building object will run the simulation. int parameter: number of seconds to run simulation private: Need handles to composite objects (delay implementation until Chapter 7) 1// building.h 2// Definition for class Building. 3#ifndef BUILDING_H 4#define BUILDING_H 5 6class Building { 7public: 8 Building(); // constructor 9 ~Building(); // destructor // runs simulation for specified amount of time 12 void runSimulation( int ); 13private: 14 // In Chapter 7, we show how to include: 15 // one object of class Clock 16 // one object of class Scheduler 17 // one object of class Elevator 18 // two objects of class Floor 19}; 20 21#endif // BUILDING_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline ElevatorButton Header public: Constructor, destructor, member functions private: pressed attribute We include a handle to an Elevator object (using a reference) 1// elevatorButton.h 2// Definition for class ElevatorButton. 3#ifndef ELEVATORBUTTON_H 4#define ELEVATORBUTTON_H 5 6class Elevator; // forward declaration 7 8class ElevatorButton { 9public: 10 ElevatorButton( Elevator & ); // constructor 11 ~ElevatorButton(); // destructor void pressButton(); // press the button 14 void resetButton(); // reset the button 15private: 16 bool pressed; // state of button // reference to button's elevator 19 Elevator &elevatorRef; 20}; 21 22#endif // ELEVATORBUTTON_H References must be initialized when declared, but we cannot assign data members in the header file. Reference therefore initialized in constructor; pass Elevator reference to constructor (line 10). Line 6 is a forward declaration. It allows us to reference an object without including the header.

 2000 Deitel & Associates, Inc. All rights reserved. Outline FloorButton Header Header identical to ElevatorButton except we declare variable floorNumber (type int ) Objects of type floorButton need to know to which floor they belong (passed as argument to constructor) 1// floorButton.h 2// Definition for class FloorButton. 3#ifndef FLOORBUTTON_H 4#define FLOORBUTTON_H 5 6class Elevator; // forward declaration 7 8class FloorButton { 9public: 10 FloorButton( int, Elevator & ); // constructor 11 ~FloorButton(); // destructor void pressButton(); // press the button 14 void resetButton(); // reset the button 15private: 16 int floorNumber; // number of the button's floor 17 bool pressed; // state of button // reference to button's elevator 20 Elevator &elevatorRef; 21}; 22 23#endif // FLOORBUTTON_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Scheduler Header public: Constructor, destructor, member function private : scheduleTime and delayTime (not operations) References to floors 1 and 2 (scheduler needs these for isOccupied call) Arrival times for floors 1 and 2 1// scheduler.h 2// definition for class Scheduler 3#ifndef SCHEDULER_H 4#define SCHEDULER_H 5 6class Floor; // forward declaration 7 8class Scheduler { 9public: 10 Scheduler( Floor &, Floor & ); // constructor 11 ~Scheduler(); // destructor 12 void processTime( int ); // set scheduler's time 13private: // method that schedules arrivals to a specified floor 16 void scheduleTime( Floor & ); // method that delays arrivals to a specified floor 19 void delayTime( Floor & ); Floor &floor1Ref; 22 Floor &floor2Ref; 23 int floor1ArrivalTime; 24 int floor2ArrivalTime; 25}; 26 27#endif // SCHEDULER_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Floor Header public: Constructor, destructor, member functions Example: Elevator invokes elevatorLeaving, and Floor object turns off its light 1// floor.h 2// Definition for class Floor. 3#ifndef FLOOR_H 4#define FLOOR_H 5 6class Elevator; // forward declaration 7 8class Floor { 9public: 10 Floor( int, Elevator & ); // constructor 11 ~Floor(); // destructor // return true if floor is occupied 14 bool isOccupied(); // return floor's number 17 int getNumber(); // pass a handle to new person coming on floor 20 void personArrives(); // notify floor that elevator has arrived 23 void elevatorArrived(); // notify floor that elevator is leaving 26 void elevatorLeaving(); // declaration of FloorButton component (see Chapter 7) 29

 2000 Deitel & Associates, Inc. All rights reserved. Outline Floor Header private: floorNumber (for output purposes, so floor knows where it is) Constructor takes an int, to initialize floorNumber Elevator reference Another component (more Chapter 7) 30private: 31 int floorNumber; // the floor's number 32 Elevator &elevatorRef; // pointer to elevator 33 // declaration of Light component (see Chapter 7) 34}; 35 36#endif // FLOOR_H

 2000 Deitel & Associates, Inc. All rights reserved. Outline Elevator Header public : Constructor, destructor, member functions (operations) Rename last two functions to differentiate between people waiting and passengers 1// elevator.h 2// Definition for class Elevator. 3#ifndef ELEVATOR_H 4#define ELEVATOR_H 5 6class Floor; // forward declaration 7 8class Elevator { 9public: 10 Elevator( Floor &, Floor & ); // constructor 11 ~Elevator(); // destructor // request that elevator service a particular floor 14 void summonElevator( int ); // prepare elevator to leave 17 void prepareToLeave(); // give time to elevator 20 void processTime( int ); // notify elevator that passenger is boarding 23 void passengerEnters(); // notify elevator that passenger is exiting 26 void passengerExits(); 27

 2000 Deitel & Associates, Inc. All rights reserved. Outline Elevator Header private: Declare attributes Declare Floor references (initialized by the constructor) We do not declare capacity attribute - in implementation, we write code so only one person in elevator at a time 28 // declaration of ElevatorButton component (see Chapter 7) 29private: 30 bool moving; // elevator state 31 int direction; // current direction 32 int currentFloor; // current location // time for arrival at a floor 35 int arrivalTime; // References to floors serviced by elevator 38 Floor &floor1Ref; 39 Floor &floor2Ref; // declaration of Door component (see Chapter 7) 42 // declaration of Bell component (see Chapter 7) 43}; 44 45#endif // ELEVATOR_H

 2000 Deitel & Associates, Inc. All rights reserved. 6.5 Conclusion Next chapter –Full code for simulation New ideas –Composite relationships –Dynamic creation of objects –static and const data members and functions Chapter 9 –Use inheritance to further improve our design