I currently know a bit of C, and i thought as I am off work on furlough I will learn some C++. I have created a basic airline booking system that outputs to a console window.
So the main menu accepts a passengers details (Name, Age and Sex) and assigns these details to a seat on the plane. The program consists of 'main', the 'plane' class and the 'passengers' class. I have created vectors for both the passenger and also the seats.
So my next step is trying to get the 'passenger' vector to point to the 'seats' vector. Due to both of these vectors being public (global) is it better to do this pointer math in 'main' or in the 'Plane' classes. What is the correct OOP way?
If someone could give me a bit of direction on how to move forward on pointing the passenger vector to a seat vector.
So my next step is trying to get the 'passenger' vector to point to the 'seats' vector.
What exactly do you mean by that?
What is the correct OOP way?
Usually, it's "more correct" to do anything that specifically involves the sub-elements of an object within functions inside of the class and keep it as far away from main as possible.
For example, your vectors are public, when they can be private. Any work you do to those vectors should be within the class itself or friend functions created for the class. It's safer than having them accessible to any code that wants to change it in main.
I would add a struct of seat with the members ID and empty.
The Plane would have a collection of seats.
The passenger should have a field for the seat id
The actual booking and cancellation could be done in a class Airline which also stores the passengers..
I also would do all the input and output in main and pass the collected data to the Plane and Passenger class.
I hope this makes some sense. English isn't my native language and sometimes I express my clearly enough.
"So my next step is trying to get the 'passenger' vector to point to the 'seats' vector"
Is what i am trying to do and work up to, on the main menu, a passengers details will be taken and stored into a vector 'passenger'. Then next step the passenger has an option to sit in a number of available seats stored in the vector 'seats'. Below is my example:
I am guessing the passenger vectors would need to point to the seats vector using pointers, but i cannot quite workout how to do this. I am sure i am making this harder than it is but i just cannot see it.
I have used your program layout shell and i have added my code into it. I have run into another OOP problem i do not really understand the flow of OOP. Below are the steps that work so far:
1) passenger details are added into the main menu - working
2) passenger details are returned from 'Person' class back to main using a vector - working
3) At this point i want to add this details into the seat class, but the data members are different. I also notice that in the 'Seat' class, one of the data members m_person is of type 'Person'. How would i use this?
I am guessing the flow of the program is as follows... passenger data is added into the 'Person' class and passed back to main. This data is then passed to the 'Seat' class and passed back to main. Then this data is passed to the 'Plane' class?
Why does a person have a vector of strings called person?? What is in that vector that isn't in the member data?
What if a seat is reserved but the name is blank? Or vice versa? I'd remove the m_isReserved member and replace it with bool isReserved() { return m_person.size(); } In other words, a seat is reserved if the person has a name.
Plane::m_seat should be called m_seats because there are many of them. It should be a vector of Seat, not a vector of string.
Populate m_seats in Plane's constructor, not AvailableSeats(). Otherwise every time you call AvailableSeats, you'll add new seats to the plane!
Then add some methods to lookup the seat from a seat number or occupant name.
Here is the data that I used in a working version.
It will take me a while to work through it but i will come back to you. There is some new concepts in the code that i will have to go and have a read up on.
I am working through your code and learning lots. I am currently struggling to initialize the 'Seat' class constructor. I have tried to initialize 'num' to 0 in private (int num = 0) and also in main 'Seat seat(0)' and also 'Seat seat = 0' but the compiler shows the error (no default constructor exists for class 'Seat'. If i remove 'Seat seat' from main i do not get this error. I cannot work out what i am doing wrong?
Thanks for your help. Yes i am keen, but struggling lots as new to C++.
I think my made up example of a booking system has been a bit more challenging than i first expected. I make good progress then get caught up in syntax problems and brick wall for a day or so.
It seems you try to learn C++ and OOP at once, which seems too much
Why not postpone your current project and learn of C++ first?
Another useful book is the "C++ Programming Language 4.ed." of Bjarne Stroupstrup. Might also be available as .pdf
Once you have read these 2 books you have a proper understanding of C++.