Headers and Classes

May 11, 2013 at 6:24am
Hi guys,

I am going over an old exam paper and need some help with a few of the questions. Below is the scenario given.

Define a class Booking that represents one booking for a performance at a venue. This class has four member variables:
• show, a string that holds the name of the show or performance
• nrTickets, an integer value that indicates the number of tickets booked
• seats, a string that holds seats booked (either “standing” or the seat numbers, e.g. “A6-A10”)
• customer, a string containing the name of the person who made the booking

In addition, the class should have the following member functions:
• A default constructor that initializes show, seats and customer respectively to an empty string.
• nrTickets should be initialized to 0.
• An overloaded constructor that accepts a new booking and sets show, seats, customer and nrTickets to specified values.
• A destructor that does not perform any action.
• An overloaded equality operator== to compare two bookings. The ==operator is implemented as a friend function with the following prototype:

bool operator==(const Booking & booking1, const Booking & booking2)
This function returns true if booking1and booking2 has been made for the same show and customer; and false if not.
• An overloaded operator+ for class Booking so that the following are feasible in the main program: a =b + c; where a, b, and c are all Booking objects. The overloaded operator+ should add the values of the nrTickets member variables. It should also concatenate the value of the seats member variable of the second operand to the value of the seats member variable of the first operand. The show and customer member variables should be the same as for the first operand. The overloaded operator+ should return a Booking object. The prototype is as follows:

Booking &operator+ (const Booking & b1, const Booking & b2);
The overloaded operator+ will allow a customer to buy more tickets and book the seats for the tickets under the same name.
• A member function calcFee() to determine the amount charged for the tickets. Use the following prototype:

float calcFee();


• The fee for “standing” tickets is R100.00 per ticket. All other tickets cost R200.00 per ticket.
• An overloaded extraction operator >> (implemented as a friend function) so that it can be used to input values of type Booking.
• An overloaded insertion operator << (implemented as a friend function) that displays all the member variables of a Booking object.

Create the header file Booking.h that contains the Booking class specification.

I am not sure if I am on the right track here.

Booking.h:
1
2
3
4
5
6
7
#ifndef BOOKING_H
#define BOOKING_H
string show;
int nrTickets =0;
string seats;
string customer;
#endif 


Any assistance/advice would be appreciated.

Thanks
May 11, 2013 at 6:41am
You declare global variables there, not a class. See:
http://www.cplusplus.com/doc/tutorial/classes/
May 11, 2013 at 4:49pm
Hi,

Thanks for pointing me in the right direction. Still not understanding this fully but this is what I have created based on class and another example from an assignment that we worked on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef BOOKING_H
#define BOOKING_H
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class BOOKING
{
    public:
        Booking ();
        Booking (string show, int nrTickets, string seats, string customer)
        string get_show()const;
        int get_nrTickets()const;
        string get_seats()const;
        string get_customer()const;
    private:
        string show;
        int nrTicket;
        string seats;
        string customer;
};
#endif 


Please let me know if this is heading on the right track and where I might have gone wrong.

Thanks again.
May 11, 2013 at 6:19pm
That seems right. You'll have to #include booking.h in any of the .cpp files that use this class and create an instance of it like BOOKING performance_booking;

Read this if you're not sure what should go in headers and what shouldn't:
http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798
Last edited on May 11, 2013 at 6:19pm
May 11, 2013 at 6:48pm
Thanks for coming back to me here. That is the next question:

Create the implementation of the class Booking including all the friend functions.

I will read the suggested link and update once I have worked out my answer.

Thanks
Topic archived. No new replies allowed.