SENECA COLLEGE OF APPLIED ARTS AND TECHNOLOGY

SCHOOL OF COMPUTER STUDIES

FINAL EXAMINATION

SEMESTER SUBJECT CODE

WINTER 2010 Introduction to Object-Oriented Programming OOP244

NAME:

STUDENT NUMBER:

SECTION:

DATE: Tuesday April 20 2010 – 11:30 a.m.

TIME ALLOWED: 2 Hours

QUESTIONS:

1 Word Problem 10 MARKS

2 Word Problem 10 MARKS

3 Walkthrough 20 MARKS

TOTAL MARKS 40

PROFESSOR(S): Danny Abesdris, Catherine Leung, Chris Szalwinski

SPECIAL INSTRUCTIONS:

1.  Open Book and Open Notes

2.  One Foreign Language Dictionary Allowed – paper or digital

3.  No programmable aids or messaging devices allowed

4.  Turn off your cell phone

5.  Answer all questions in the exam booklet. Submit this exam paper inside the exam booklet.

SENECA COLLEGE ACADEMIC POLICY:

CHEATING is an offence, which will not be tolerated by the College. Such offences occur when a student violates the procedures governing the administration of examinations, tests or other means of evaluating student achievement in a subject or program.

APPROVED BY: ______

EVAN WEAVER, CHAIR

QUESTION 1 - WORD PROBLEM - (10 MARKS):
Design and code a class namedTicketthat 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 namedTicket.hand your function definitions in an implementation file namedTicket.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, aTicketobject may receive no information or may receive two values:
1.  an integer for the date on which the ticket is valid
2.  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 unmodifiableTicketobject 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 yourTicketclass and produces the output shown on the right side:
#include <iostream>
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
QUESTION 2 - WORD PROBLEM - (10 MARKS):
You may answer this question even if you have not completely designed theTicketclass in the previous question; that is, you may assume that you have access to a complete design for theTicketclass and may base your answer to this question on that complete design.
Derive from theTicketclass a class namedMTicketthat holds information about a ticket for a particular movie. The information includes the name of the movie. Place your class declaration in a header file namedMTicket.hand your function definitions in an implementation file namedMTicket.cpp. Include in your design all of the statements and keywords necessary to compile and to run your code successfully under a standard C++ compiler.
Upon instantiation, anMTicketobject may receive no information or may receive three values:
1.  an integer for the date on which the ticket is valid
2.  a positive floating-point number for the price
3.  the address of an unmodifiable C-style null terminated string holding the name of the movie.
The details of the date and price information are described in question 1. The string may be of any length. If the object receives a valid date and price, it stores the received values and a copy of the string. If the object receives no information or an invalid value for the date or the price, the object adopts a safe empty state and does not store a copy of the string.
Your design includes the following member function and any other member functions needed to manage dynamic memory:
·  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),
·  its price to two (2) decimal places in a field of six (6), and
·  the string describing the movie on a separate line
as shown in the example below.
The following program uses your derived class and produces the output on the right side:
#include <iostream>
using namespace std;
#include "MTicket.h"
int main ( ) {
MTicket adult(2010200, 12.50,
“The Ghost Writer”);
cout < adult < endl;
adult += 165;
cout < adult < endl;
adult += 1;
cout < adult < endl;
return 0;
} /
2010-200 12.50
The Ghost Writer
2010-365 12.50
The Ghost Writer
2011-001 12.50
The Ghost Writer
QUESTION 3 - WALKTHROUGH - (20 MARKS):
What is the exact output of the following program? Show your rough work to avoid deductions.
#include <iostream>
using namespace std;
class Fruit {
int items;
double price;
public:
Fruit();
Fruit(int, double);
Fruit(const Fruit&);
void to(Fruit&);
void operator+=(double);
friend ostream& operator<(ostream&, const Fruit&);
virtual ~Fruit();
};
Fruit::Fruit() {
items = 0;
price = 0.0;
cout < '#';
}
Fruit::Fruit(int i, double p) {
items = i;
price = p;
cout < '!';
}
Fruit::Fruit(const Fruit& f) {
items = f.items;
price = f.price;
cout < '$';
}
void Fruit::to(Fruit& dest) {
if (items > 0) {
dest.items++;
items--;
}
}
void Fruit::operator+=(double change) {
price += change;
}
Fruit::~Fruit() {
cout < '~' < *this < endl;
}
ostream& operator<(ostream& os, const Fruit& s) {
os < s.items < '-' < s.price;
return os;
}
class OldFruit : public Fruit {
public:
OldFruit ();
OldFruit (int, double);
OldFruit (const OldFruit&);
friend ostream& operator<(ostream&, const OldFruit&);
~OldFruit();
};
OldFruit::OldFruit () {
cout < '&';
}
OldFruit::OldFruit (int i, double p) : Fruit(i, p) {
cout < ':';
}
OldFruit::OldFruit(const OldFruit& f) : Fruit(f) {
cout < '*' < endl;
}
OldFruit::~OldFruit() {
cout < '%' < *this < endl;
}
ostream& operator<(ostream& os, const OldFruit& s) {
os < (Fruit&)s; // calls Fruit version of < operator
os < " reduced ";
return os;
}
OldFruit reduce(Fruit& fruit, int n, double p) {
OldFruit old;
for (int i = 0; i < n; i++)
fruit.to(old);
old += p;
return old;
}
int main() {
Fruit apples(5, 2.00), pears;
cout < endl;
cout < '=' < apples < endl;
OldFruit oldApps = reduce(apples, 2, 1.00);
cout < '=' < oldApps < endl;
return 0;
}