1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.

Slides:



Advertisements
Similar presentations
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Advertisements

Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Binary Search Trees II Morse Code.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Jan. 29, 2008(c) Mike Barnoske Introduction to Runtime Debugging Using the Visual C++ IDE COP 4331: OO Processes for SW Development © Dr. David A. Workman.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Creating your first C++ program
Computer Science I How to Configure Visual Studio.NET 2003 for C++ Colin Goble.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
ITEC 320 C++ Examples.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
11 Introduction to Object Oriented Programming (Continued) Cats.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Modeling CDs (Continued). 2 Getting Started Download: 2011_02_07_CD_Collection_Incomplete/
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
Objective: Students will be able to: Declare and use variables Input integers.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Project 3: Ticket Printer
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
1 Modeling CDs. 2 Objectives You will be able to: Sketch a diagram showing the relationship between a class and classes that it contains a members. Define.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Software Engineering Algorithms, Compilers, & Lifecycle.
1 Command Processor. Objectives You will be able to Create and understand State Diagrams. Write a text based command processor with multilevel menus.
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
Tutorial 1 Writing Your First C++ Program CSC1110C Introduction to Computer Programming By Paul Pun Acknowledgement: Special thanks to Dr. Michael Fung.
1. Open Visual Studio 2008.
Lab 1 Introduction to C++.
CS150 Introduction to Computer Science 1
Computer Terms Review from what language did C++ originate?
Presentation transcript:

1 Implementing Ticket Printer

Class Diagram 2

Dependencies A class that contains objects of another class, or has a reference to another class, depends on that class. Example: Class Venue depends on class Address and class Seat_Row. Before we can implement a class, we have to implement the classes on which it depends. Those classes may also depend on other classes, forming dependency chains. 3

Implementing the Design Keep the class diagram in front of you. This is a blueprint for the structure you are building. Start with a stub Just the main.cpp Hello world Add classes one at a time. Get each one to compile and work before adding another. Start at the ends of dependency chains and work up the chains. 4

Classes Design your classes as reusable components No knowledge of the specific venue and performance described for this program. Pass that information in from main. Future programs might get that information from user input or a file. 5

Create a New Project Visual C++ Win32 Win32 Console Application Empty project 6

Add main.cpp Project > Add New Item Visual C++ C++ File Name: main.cpp 7

main.cpp #include using namespace std; int main() { cout << "This is Ticket_Printer\n\n\n"; cin.get(); return 0; } 8 Build and run

Ticket_Printer in Action 9 From here on, ALWAYS HAVE A WORKING PROGRAM

What Next? Pick a class that has no dependencies. How about class Address? Project > Add Class Start with the class declaration Address.h 10

Address.h #pragma once #include using namespace std; class Address { private: string street_address; string city; string state; int zip_code; public: Address(string Street_Address, string City, string State, int Zip_Code); void Display() const; }; 11

Implement the Class #include #include "Address.h" Address::Address(string Street_Address, string City, string State, int Zip_Code) : street_address(Street_Address), city(City), state(State), zip_code(Zip_Code) {} 12 Address.cpp

Implement the Class void Address::Display() const { cout.fill('0'); cout << street_address << endl; cout << city << ", " << state << " "; cout.width(5); cout << zip_code << endl; } 13 Build and run

Program Running 14 It's still at the "Hello, World" level, because we have not added any code to main.cpp

Add Test Driver for the Class #include #include "Address.h" using namespace std; // Create an Address object with the address of The Little Theater Address* Create_Address() { Address* address = new Address("19 Foster Street", "Littleton", "MA", 1460); return address; } 15

Add Test Driver for the Class int main() { cout << "This is Ticket_Printer\n\n\n"; Address* adr = Create_Address(); adr->Display(); cout << endl; cin.get();// Hold the window open return 0; } 16 Build and run

Program Running 17 Often you will find errors when you compile or when you run a test. Keep working on that class until it compiles and runs correctly.

Add Another Class The Seat class has no dependencies. Only remaining class with no dependencies. A good choice to add next. Add class Seat to the project 18

Seat.h #pragma once #include using namespace std; class Seat { private: string seat_row_name; int seat_number; public: Seat(string Row_Name, int Seat_Number); void Display() const; }; 19

Seat.cpp #include #include "Seat.h" using namespace std; Seat::Seat(string Row_Name, int Seat_Number) : seat_row_name(Row_Name), seat_number(Seat_Number) {} void Seat::Display() const { cout << "Row " << seat_row_name << " Seat " << seat_number << endl; } 20 Build and run

Add Test Code for Class Seat #include "Seat.h"... adr->Display(); cout << endl; Seat* seat = new Seat("A", 1); seat->Display(); 21 main.cpp Build and run

Program Running 22 Test successful!

Add Another Class Work up a dependency chain Now that class Seat exists we can add class Seat_Row. Add class Seat_Row to the project. 23

Seat_Row.h #pragma once #include "Seat.h" class Seat_Row { public: static const int MAX_SEATS_PER_ROW = 1000; private: string row_name; const Seat* seats[MAX_SEATS_PER_ROW]; int number_of_seats; public: Seat_Row(const string& Row_Name); void Add_Seat(const Seat* new_seat); void Display() const; }; 24

Seat_Row.cpp #include #include "Seat_Row.h" using namespace std; Seat_Row::Seat_Row(const string& Row_Name) : row_name(Row_Name), number_of_seats(0) {} void Seat_Row::Add_Seat(const Seat* new_seat) { assert(number_of_seats < MAX_SEATS_PER_ROW); seats[number_of_seats++] = new_seat; } 25

Seat_Row.cpp void Seat_Row::Display() const { cout << "Row " << row_name << " has " << number_of_seats << " seats" << endl; for (int i = 0; i < number_of_seats; ++i) { cout << "\t";// Indent the Seat description seats[i]->Display(); } 26 Build and run

Add Test Code for Seat Row #include "Seat_Row.h"... In main() cout << endl; Seat_Row* row = new Seat_Row("Test"); row->Display(); cin.get(); return 0; } 27 main.cpp

Program Running 28 Add some seats to row Test.

In main.cpp // Create a Seat_Row with the specified name and // specified number of seats, Seat_Row* Create_Seat_Row(const string seat_row_name, int number_of_seats) { Seat_Row* row = new Seat_Row(seat_row_name); for (int i = 1; i <= number_of_seats; ++i) { Seat* new_seat = new Seat(seat_row_name, i); row->Add_Seat(new_seat); } return row; } 29

In main.cpp //Seat_Row* row = new Seat_Row("Test"); Seat_Row* row = Create_Seat_Row("Test", 4); row->Display(); cout << endl; cin.get(); return 0; } 30 Build and run

Program Running 31

Add Class Venue We now have the classes that class Venue depends on. Add class Venue to the project. 32

Venue.h #pragma once #include #include "Address.h" #include "Seat.h" #include "Seat_Row.h" class Venue { public: static const int MAX_SEAT_ROWS = 1000; private: string name; const Address* address; const Seat_Row* seat_rows[MAX_SEAT_ROWS]; int number_of_seat_rows; 33

Venue.h public: Venue(const string& name_, const Address& address_); void Add_Seat_Row(const Seat_Row* seat_row); int Capacity() const; // Number of seats void Display() const; }; 34

Venue.cpp #include #include "Venue.h" #include "Seat_Row.h" using namespace std; Venue::Venue(const string& name_, const Address& address_) : name(name_), address(&address_), number_of_seat_rows(0) {} void Venue::Add_Seat_Row(const Seat_Row* seat_row) { assert(number_of_seat_rows < MAX_SEAT_ROWS - 1); seat_rows[number_of_seat_rows++] = seat_row; } 35

Venue.cpp void Venue::Display() const { cout << name << endl; address->Display(); for (int i = 0; i < number_of_seat_rows; ++i) { const Seat_Row* row = seat_rows[i]; row->Display(); } 36 Build and run

Program Running 37 No output for Venue yet.

Add Test Code for Class Venue #include "Venue.h"... // Create a Venue object corresponding to The Little Theater. Venue* Create_Venue() { Address* adr = Create_Address(); Venue* venue = new Venue("The Little Theater", *adr); Seat_Row* sr = Create_Seat_Row("A", 4); venue->Add_Seat_Row(sr); sr = Create_Seat_Row("B", 4); venue->Add_Seat_Row(sr); sr = Create_Seat_Row("C", 4); venue->Add_Seat_Row(sr); return venue; } 38 main.cpp

Add Test Code for Class Venue In main() Venue* venue = Create_Venue(); venue->Display(); cin.get(); // Hold the window open return 0; } 39 Build and run

Program Running 40

Summary Work in tiny steps Add one class at a time Working up dependency chains. Before adding a class, add classes on which that class depends. Add test code to main function. Build and test. Be sure the program compiles and works before adding more code. 41