1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Introduction to Programming Lecture 39. Copy Constructor.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Win32 Programming Lesson 4: Classes and Structures.
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.
Chapter 4 Summation.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
Binary Search Trees II Morse Code.
CS352-Week 2. Topics Heap allocation References Pointers.
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.
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.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Writing a Good Program 8. Elementary Data Structure.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
11 Introduction to Object Oriented Programming (Continued) Cats.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
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.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
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.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
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.
1 Project 1: Tickets. 2 Class Ticket Write a definition for class Ticket. A Ticket object corresponds to a physical ticket for a stage show. Attributes.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
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.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
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.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
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 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
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.
Chapter 16-2 Linked Structures
Pointers & Functions.
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
Class rational part2.
Presentation transcript:

1 Implementing Ticket Printer

Download project from last class Downloads/2016_02_12_In_Class/ Downloads/2016_02_12_In_Class/ File Ticket_Printer.zip Extract all Open project Build and run 2

Program Running 3

Continue Development Let's continue evolving this project into the real Ticket Printer program. We have our Venue. Before we can implement Ticket_Book we need class Performance Add class Performance to the project. 4

Performance.h #pragma once #include #include "Venue.h" struct Date_Time { int Day; int Month; int Year; int Hour; int Minute; }; 5

Performance.h (continued) class Performance { private: const string show_name; const Venue* venue; const Date_Time when; public: Performance(const string& Show_Name, const Venue* Venue_, const Date_Time& When); const Venue* Get_Venue() const { return venue; }; void Display() const; }; 6

Performance.cpp #include #include "Performance.h" #include "Venue.h" using namespace std; Performance::Performance(const string& Show_Name, const Venue* Venue_, const Date_Time& When) : show_name(Show_Name), venue(Venue_), when(When) {} 7

Performance.cpp (continued) void Performance::Display() const { cout.fill('0'); cout << "Performance: " << show_name << endl; cout << when.Day << "/" << when.Month << "/" << when.Year; cout << " at "; cout.width(2); cout << when.Hour << ":"; cout.width(2); cout << when.Minute << endl; venue->Display(); } 8

main.cpp #include "Performance.h"... Performance* Create_Performance(Venue* venue) { Date_Time when = {4, 2, 2016, 20, 0}; Performance* p = new Performance("Billy Elliot", venue, when); return p; }... Performance* performance = Create_Performance(venue); performance->Display(); 9

Program Running 10

Clean Up Comment out the earlier outputs in main.cpp We know those classes are working now. When we print a ticket, we will use the Display method of Performance to output the name of the show and Venue But we don't want to show all of the seats in the venue, just the name and address. Change the Display method in class Venue to just output what we want. Build and run 11

Program Running 12 Looks like our Performance class is OK.

Continue Development We have all the classes now except Ticket_Book and Ticket. Ticket_Book depends on Ticket "Has a" relationship Let's implement class Ticket next. Add class Ticket to the project. 13

Ticket.h #pragma once #include "Performance.h" #include "Seat.h" class Ticket { private: const Performance* performance; const Seat* seat; bool sold; public: Ticket(const Performance* perf, const Seat* s); void Display() const; }; 14

Ticket.cpp #include "Ticket.h" #include "Performance.h" #include "Seat.h" Ticket::Ticket(const Performance* perf, const Seat* s) : performance(perf), seat(s), sold(false) {} void Ticket::Display() const { performance->Display(); seat->Display(); } 15

Class Ticket Class Ticket delegates its Display functionality to class Performance and class Seat. 16

Testing Class Ticket #include "Ticket.h"... //performance->Display(); Seat* seat = new Seat("A", 1); Ticket* ticket = new Ticket(performance, seat); ticket->Display(); 17 main.cpp Build and run

Program Running 18

Continue Development We have all the classes now except Ticket_Book. Add class Ticket_Book to the project. 19

Implementing Ticket_Book Our first task is to create the tickets. Do this in the constructor. Given the Venue and the Performance Iterate over the seat rows. For each seat row Iterate over the seats. For each seat Create a ticket 20

Ticket_Book.h #pragma once #include "Performance.h" #include "Ticket.h" class Ticket_Book { private: Ticket** tickets; // A dynamically allocated array of // pointers to Tickets int number_of_tickets; public: Ticket_Book(Performance* p); void Print_Tickets() const; }; 21

Ticket_Book.cpp #include #include "Ticket_Book.h" #include "Ticket.h" #include "Venue.h" using namespace std; Ticket_Book::Ticket_Book(Performance* p) { const Venue* venue = p->Get_Venue(); number_of_tickets = venue->Capacity(); tickets = new Ticket*[number_of_tickets]; } 22 Build and run

Error! 23 We have not implemented the Capacity method in class Venue.

Add to Venue.cpp // Return number of seats int Venue::Capacity() const { int count = 0; for (int i = 0; i < number_of_seat_rows; ++i) { count += seat_rows[i]->Number_of_Seats(); } return count; } 24 Visual Studio tells us that Number_of_Seats does not exist.

Seat_Row.h Add accessor method for number_of_seats int Number_of_Seats() const {return number_of_seats;}; 25 Build and run

Remember our objective Given the Venue and the Performance Iterate over the seat rows. For each seat row Iterate over the seats. For each seat Create a ticket 26

Add to Ticket_Book.cpp int n = 0; // index for array tickets // Create a ticket for each seat in the venue. int nr_rows = venue->Number_of_Seat_Rows(); for (int i = 0; i < nr_rows; ++i) { const Seat_Row* row = venue->Get_Seat_Row(i); int nr_seats_in_row = row->Number_of_Seats(); for (int j = 0; j < nr_seats_in_row; ++j) { const Seat* seat = row->Get_Seat(j); tickets[n++] = new Ticket(p, seat); } 27

Missing Accessor Methods Visual Studio tells us that some accessor methods that we need are missing: venue->Number_of_Seat_Rows(); venue->Get_Seat_Row(i); row->Get_Seat(j); 28

Add to Venue.h int Number_of_Seat_Rows() const { return number_of_seat_rows; }; const Seat_Row* Get_Seat_Row(int index) const { return seat_rows[index]; } 29

Add to Seat_Row.h const Seat* Get_Seat(int idx) const { return seats[idx]; }; 30 Build and run

Implementing Print_Tickets We have created an array of Tickets. Iterate over the array and ask each Ticket to print itself. 31

Add to Ticket_Book.cpp void Ticket_Book::Print_Tickets() const { for (int i = 0; i < number_of_tickets; ++i) { tickets[i]->Display(); cout << " \n"; } 32

Testing Ticket_Book #include "Ticket_Book.h"... //Seat* seat = new Seat("A", 1); //Ticket* ticket = new Ticket(performance, seat); //ticket->Display(); Ticket_Book* ticket_book = new Ticket_Book(performance); ticket_book->Print_Tickets(); 33 Build and run

Program Running 34