Delete post

Delete
Last edited on
Could you provide some code?
Start by creating the struct. I don't remember, can you declare a struct in the header and define its parameters in the .cpp file?

I rarely use structs....

Anyhow, write your struct, then call the array of structs in the reservations class under the class's private section.

Then write your public functions to interact with the private members.

I like to start with a single .cpp file that also contains my main(), making sure that the class declaration is above my using namepsace std call.

I write the definitions to public functions under the using namespace call.

Once I have everything running as expected I cut the class declaration and includes and paste them into a header, remember to wrap them in an #ifndef macro.

#include the new header in the original .cpp file. Then I cut main() out and put it in its own file and once again #include the new header in the main cpp file...

Remember to add all the files into your project if you're using an IDE, or just to include all .cpp's in a g++ command if you compile in-console.
Last edited on
can you declare a struct in the header and define its parameters in the .cpp file?

for non-templated structs, yes that is possible

OP:
Need to use a class with an array of 10 structures in the private portion of the class and instances of this array of structures are to be created in the public portion
... can you please explain in simple English what this means? I'm lost!
@gunnerfunner: translated into normal English, there's a private array of 10 structs in the class, those will be initialized inside a public function (constructor I suppose)

Start by creating the struct. I don't remember, can you declare a struct in the header and define its parameters in the .cpp file?

I rarely use structs....


Structs are just classes whose members are public by default. So anything you can do with a class, you can do with a struct.

@OP: here's a skeleton for your struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct passenger
{
      std::string first_name;
      std::string last_name;
      int flight_number;
      bool membership;

      passenger(std::string f_name, std::string l_name, int number, bool member):
      first_name(f_name),
      last_name(l_name),
      flight_number(number),
      membership(member) 
      {};
}
Last edited on
That makes sense and could imply something along following lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
constexpr auto SIZE = 10;
class ReservationSystem
{
    private:
        Passenger passengers[10];
        //...
    public:
        ReservationsSystem()
        {
            //10 calls to the Passenger ctor;
            //place each Passenger object into passengers;
        }
        //...
};
.
Last edited on
unsure how to make it so that 10 flight infos can be displayed
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<Flight> flights{};

    for (int i = 0; i < 10; ++i)
    {
        Flight obj{};
        obj.getFlight();
        flights.push_back(obj);
    }
    for (const auto& elem : flights)
    {
        std::cout << elem ; //requires operator << overload for Flight class
        //https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx
    }
Topic archived. No new replies allowed.