Thread: Vectors and Classes

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Vectors and Classes

    Hi All,

    I am quite new to c++ and getting a better understanding of classes and vectors. I have decided to write an airplane booking program to add and remove passengers onto a plane. So my problem is i can create one passenger and display the details to a console window, but if i wanted to create multiple passengers (name, age, food) would i store them in a vector. So my questions are below:

    1) Would i create a vector for each passenger, so each location would have name, age, food etc.

    2) Can i store passenger vector details in a vector map( so put a vector in a vector), because a plane doesn't have one row of seats it has multiple rows. So eventually i want to say add passenger 'fred' to seat a1 and 'susan' to seat f3, and then at a later date maybe delete these passengers.

    I think this is the correct way of doing this but i just need feedback if this is correct or not, or would you recommend a different method.

    Thanks,

    Rocketman46

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,107
    I doubt anyone understands what you're getting at.
    A vector for each passenger?
    "passenger vector details"?
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi,

    I will try and explain a different way. Below is picture of a basic plane seat layout. I want to be able to book for example seat A1 to (Name: Fred, Age: 21, Food: Fish & Chips).

    So i would like to book any of the below seats with (Name, Age and Food) and at a later date cancel the seats. I guessed but i am not sure, each passenger details could be stored in a vector, and this vector could be stored in the planes seat layout a map of vectors, so a vector in a map of vectors.

    A1, B1, C1, D1, E1
    A2, B2, C2, D2, E2
    A3, B3, C3, D3, E3

    I hope this makes sense. If this is bad practice could you advise better way of doing this.

    Look forward to your reply.

    Thanks,

    Rocketman46

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    I would create a class to model a passenger: name, age, food preference, and later you could add boarding pass number, etc.

    If you did have data specific to seats, like cabin class or near to an emergency exit, then I would create another class to model a seat and the passenger allocated to it, but presently that doesn't appear necessary.

    To model the seat allocation, one approach is to use a vector of vectors of passenger or seat smart pointers, the idea being that an empty seat would have a null pointer, and that the number of seats per row sometimes differs (e.g., across cabin classes, or for safety reasons) so a "jagged 2D array" is appropriate, but this also means that you need to be careful when accessing by index to be sure that the seat exists.

    Instead of smart pointers, another approach is to have a default constructed passenger or seat object denote an empty seat.

    If your scenario model is simplified such that you assume each row has the same number of seats, then it would be better to have a single large vector of passenger or seat objects, then compute the desired index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi laserlight,

    Thank you for your help and advice. I have drafted out my basic example and added the code below - it is such a small program i thought it would be easier to add the code and then i can ask a few other questions.

    I can take the passengers details (name, age and sex) in main and add to the 'passenger' vector ok, but my problem is pointing this vectors to the 'seats' vector.

    I have declared a *Ptr_passanger pointer at the top of the Passenger class, and i am trying to use in the void Plane::BookSeat() method but this does not seem to work. I also tried declaring the *Ptr_passanger pointer in the BookSeat() method and that throws and error. At this point i am a bit lost, i can point an int to an int ok, but an object to an object in this manner i struggle. I have tried something like this but still no luck, seats[1] = *Ptr_passanger.

    If you see any obvious bad coding practices please mention as i am trying to learn classes and vetors.

    Look forward to your advice.

    Thank,

    Rocketman46

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    
    using namespace std;
    
    
    //***************************** Passanger Class ******************************************
    class Passanger
    {
    public:
        void Name();
        void Age();
        void Sex();
    
        vector<string> passanger;
        vector<string> *Ptr_passanger;
        
    private:
    
    
    };
    
    
    void Passanger::Name()
    {
        string name;
        cout << "Enter name: \n";
        cin >> name;
        passanger.push_back(name);
    
    
    }
    
    
    void Passanger::Age()
    {
        string age;
        cout << "Enter age: \n";
        cin >> age;
        passanger.push_back(age);
    }
    
    
    void Passanger::Sex()
    {
        string sex;
        cout << "Enter sex: \n";
        cin >> sex;
        passanger.push_back(sex);
    }
    
    
    //***************************** Plane Class ******************************************
    class Plane : public Passanger
    {
    public:
        Plane();
        void AvailableSeats();
        void BookSeat();
        void CancelSeat();
        void EmptySeats();
    
    
        vector<string> seats;
    
    
    private:
        
    };
    
    
    Plane::Plane()
    {
        // just a basic seat layout to get my program working
    
    
        seats.push_back("A1"); // 0
        seats.push_back("B1"); // 1
        seats.push_back("C1"); // 2
        seats.push_back("D1"); // 3
        seats.push_back("E1"); // 4
        
    }
    
    
    void Plane::AvailableSeats()
    {
        vector<string>::const_iterator iter;
    
    
        for (iter = seats.begin(); iter != seats.end(); ++iter)
        {
            cout << *iter;
            cout << "\t";
        }
        cout << endl;
    }
    
    
    void Plane::BookSeat()
    {
        string choice;
        Ptr_passanger = &passanger;
    
    
        cout << "Book Seat: \n";
        cout << "Available seats please select one: \n\n";
        AvailableSeats();
        cout << "Select seat: \n";
        cin >> choice; // B1 will be selected for example reasons
    
    
        // seats[1] = *Ptr_passanger; // trying to point passanger details to seat loation 1 (B1)
    
    
    }
    
    
    void Plane::CancelSeat()
    {
    
    
    }
    
    
    void Plane::EmptySeats()
    {
    
    
    }
    
    
    //***************************** Main ******************************************
    int main()
    {
        Plane passanger1;
    
    
        cout << "Welcome to Airline Booking\n\n";
        cout << "Please select from the following choices: \n\n";
    
    
        int choice;
        
        do
        {
            cout << "\n\n";
            cout << "1) View available seats: \n";
            cout << "2) Make a booking: \n";
            cout << "3) exit: \n\n";
            cin >> choice;
    
    
            switch(choice)
            {
                case 1:
                {
                    passanger1.AvailableSeats();
                    break;
                }
    
    
                case 2:
                {
                    cout << "Please select a seat: \n\n";
                    passanger1.Name();
                    passanger1.Age();
                    passanger1.Sex();
                    passanger1.BookSeat();
                    break;
                }
    
    
                case 3:
                {
                    cout << "exit.\n\n";
                    break;
                }
    
    
                default:
                {
                    cout << "Sorry try again. \n\n ";
                    break;
                }
    
    
            }
    
    
        } while (choice != 3);
    
    
        return 0;
    }
    Last edited by RocketMan46; 04-20-2020 at 07:17 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Surely a plane is not a passenger? A plane has passengers (or it has seats that may be occupied by passengers).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi,

    Yes i agree, are you therefore saying the 'plane' class shouldn't inherit from the 'passenger' class. I am still trying to get up to speed with OOP. I do not fully understand what i need to change.

    Thanks,

    Rocketman46

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,625
    Class diagram - Wikipedia
    A plane and it's passengers are a thing that the airline specifies.

    A plane could be a cargo plane.
    A passenger could decide to take the train instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi,

    I am kinda bouncing off the rev limiter here trying to work this through. I come from C and this is a bit different.

    So 'Plane' and 'Passenger' classes are now separated, and 'Plane' doesn't inherit from the 'Passenger' class. 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?

    Thanks,

    RocketMan46

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Quote Originally Posted by RocketMan46
    So 'Plane' and 'Passenger' classes are now separated, and 'Plane' doesn't inherit from the 'Passenger' class. 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?
    The Passenger class should model a single passenger. That is, it should not have a vector of strings named "passanger", and I don't see a reason for Ptr_passanger.

    A plane has one or more seats, each of which has zero or one passenger, and you could also say that a plane has zero or more passengers. In post #4, I already suggested a few ways how you might model this. You can come up with a Seat class to model a single seat, or you could directly use the Passenger class; it all depends on exactly what you want to model. If you just want to map seat numbers to Passengers, then you could very well go with a std::map<std::string, std::unique_ptr<Passenger>>, but then you may find it more difficult to do things like identify all the passengers in a row.

    The Passenger, Plane, and Seat classes should not have member functions that do interactive I/O. For example, this is wrong:
    Code:
    void Passanger::Name()
    {
        string name;
        cout << "Enter name: \n";
        cin >> name;
        passanger.push_back(name);
    }
    This kind of code should go in a non-member function that makes use of the interface of the Passenger class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2020
    Posts
    1

    Lightbulb The Way of the Structures of Arrays

    Not Relevant: Psst, hey, don't follow the OOP hole; it's scary
    Super Relevant: ..psst, try this

    (0) let's suppose we have a Data Structure named BUFFER

    (1) the first container in BUFFER is a
    Code:
     vector<uint32_t> _id_
    (1.1) _id_ contains all the passengers ids; Attention: All ids are unique, like all people on this earth.. and all ids are natural numbers (e.i 1,2,3,4,..).
    (1.2) _id_ is the most trivial and important container in BUFFER
    (1.3)
    every new passenger gets a new id; if for some
    Code:
    n >= 0, if (_id_[n] <= 0) is true 
    then the nth passenger does not exist.
    (2.) Every passenger has a known/fixed number of features like the ones you mentioned (they could have more). These features are: age; phone number; ACTUAL id number; flight "something"; "has the passenger arrived yet"; "how many cpu cycles did he skip due to his inability to recognize the presence of a well established dogma" (passive aggressive OOP vs DOD pun); The number of bags he brings; etc. NOTICE that all of these features are addressable via native types (unsigned int, int, bool(C doesn't like these), and more);
    (2.1) The next part is to create a container for each feature of type passenger feature type that we want to address.
    very Pseudo-code
    Code:
    BUFFER{
        Vec<uint32> _id_;
        /*containers with the passenger features */
        Vec<uint32> _planeID_;
        Vec<uint8> _numBags_;
        .
        .
    }
    In a short schema:
    Code:
    for some n >= 0
    _id_[n] : id of nth passenger
    _numBags_[n] : number of bags the nth passenger brings
    _planeID_[n] : the id of the aircraft the nth passenger was assigned to
    , provided that (_id_[n] > 0) is true
    You could also have an AIRCRAFT_BUFFER with the same formatting.

    I mean, this is one way of Designing your structures of data before you start filling anything with eternal loops of iterators; I like this way a lot. This was the first thing that came out of the top of my head.. hope i could've helped with the designing part. stay safe; stay oop minimal.
    Last edited by coddiwomple; 05-07-2020 at 09:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Classes/Vectors help!
    By bigboybz in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2011, 03:42 AM
  2. Vectors of classes
    By Dondrei in forum C++ Programming
    Replies: 26
    Last Post: 03-20-2009, 05:11 AM
  3. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 04:19 PM
  4. Classes or Vectors?
    By Ideswa in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2006, 10:07 AM
  5. vectors and classes
    By kooma in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2002, 12:44 PM

Tags for this Thread