Question: QUESTION 1 - WORD PROBLEM - (10 MARKS): Design and code a class named Ticket that holds the following information about a ticket for a
QUESTION 1 - WORD PROBLEM - (10 MARKS):
Design and code a class named Ticket that holds the following information about a ticket for a movie theatre: its price and the date upon which the ticket is valid. Place your class declaration in a header file named Ticket.h and your function definitions in an implementation file named Ticket.cpp. Include in your design all of the statements necessary to compile and to run your code successfully under a standard C++ compiler.
Upon instantiation, a Ticket object may receive no information or may receive two values:
an integer for the date on which the ticket is valid
a positive floating-point number for the price
The date is received in YYYYDDD form where the rightmost 3 digits identify the day in the year and the leftmost 4 digits identify the year. Valid values for the year component are the current year (2010) and any year until 2050 inclusive. Set the current year as an unmodifiable constant in your header file. Valid values for the day component are between 1 and 365 inclusive for non-leap years and between 1 and 366 inclusive for leap years. The leap years amongst the valid years are 2012, 2016, 2020, etc. If the object receives no information or an invalid value for the date or the price, the object adopts a safe empty state.
Your design includes three member functions and one helper operator:
bool empty() const - a query that returns true if the object is in a safe empty state; false otherwise.
Ticket& operator+=(int) - a modifier that receives the number of days by which to postpone the date of the ticket. If the number is positive and the new date is on or before the last day of 2050, your function increases the ticket's date by the number of days requested. Your function does nothing if that number is not positive or exceeds the upper limit or the current object is empty. In any case, your operator returns a reference to the current object.
void display(ostream&) const - a query that receives a reference to an ostream object and inserts into that object
the date of the ticket in YYYYDDD format in a field of ten (10) and
the price of the ticket to two (2) decimal places in a field of six (6)
as shown in the example below. If the current object is empty, this function does nothing.
an insertion operator (<<) - a helper operator that takes a reference to an ostream object as its left operand and a reference to an unmodifiable Ticket object as its right operand and inserts into the output stream information about the ticket described in the display() query above. Code your operator to allow cascading as shown below.
The program on the following page uses your Ticket class and produces the output shown on the right side:
| #include using namespace std; #include "Ticket.h"
int main ( ) { Ticket adult(2010200, 12.50);
adult.display(cout); cout << endl; adult += 165; adult.display(cout); cout << endl; adult += 1; adult.display(cout); return 0; }
|
2010-200 12.50
2010-365 12.50
2011-001 12.50
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
