Structs in classes

So I have a problem that I have to work on which I'm having issue finding info on the internet or I'm just not putting in the right key words.

Anyways I did a program that read from a binary file and now I have to do part 2 of the assignment and I'm kind of lost of where to start.

Assignment:
Create a class that “wraps” or “hides” the information from flightRec. So your class will convert each char array from the flightRec struct into strings, and also have methods to access each data member. You can name your class whatever you want, although you should keep Meyer's general guidelines in mind (no verb names, please). In this document, I am calling the class an OverFlight.

My Current Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <iomanip>

using namespace std;

struct FlightRec {
	char flightNum[7];
	char originAirportCode[5];
	char destAirportCode[5];
	int dateStamp;
};

int main()
{
	fstream myfile;
	struct FlightRec flights;

	myfile.open("C:\\Users\\ChristopherJ\\Desktop\\Chris School\\C++\\acars.bin", ios::in | ios::binary);


	if (!myfile.is_open())	{
		cerr << "error";
		return -1;
	}
	
	while (!myfile.eof())	{
		myfile.read((char*)(&flights), sizeof(flights));
		cout << left << setw(7) << flights.flightNum << left << setw(5) << flights.originAirportCode << left << setw(5) << flights.destAirportCode << left << setw(12) <<
			flights.dateStamp << endl;
	}
	 
	myfile.close();

	return 0;
}
So where are you stuck?
What do you have so far with regard to part2?

As I read the assignment, you're being asked to convert FlightRec from a struct to a class. The members should be private. You probably want getters and setters for the members of the class.

You probably also want a read method that will read the various fields from myfile. Hint pass myfile as an argument. Return a bool indicating if you've reached eof.

You probably also want a method for printing a FlightRec.

That is where I'm a little lost cause I have not done classes very much and get even more confused on setters and getters
http://www.cplusplus.com/doc/tutorial/classes/

they're the same as structs but you have a public part and a private part. You can also initialise a class object through a contructor. As AA said you want to define your methods in the private part, and access/modify them from a public part.
Last edited on
Sounds like the class encapsulates a FlightRec struct. So, I would pass a FlightRec struct through via the constructor and hold it within the class instance. You then won't need setters only getters.
So are you saying that I should call the struct inside of the class or put the struct into the class?
So are you saying that I should call the struct inside of the class or put the struct into the class?


What I'm saying is that you have the struct declared in the public section of the class so that it can be used outside of the class, have an instance of the struct declared in the private section, and have a custom constructor through which you can pass a struct from the outside.
Alright so after rereading my instruction about 20 times I am thinking that he wants us to move the char array to strings. I started the class by putting the strings public but i'm not sure if this is to be in the setter part, and I'm not sure what to put in the private part of the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct FlightRec {
	char flightNum[7];
	char originAirportCode[5];
	char destAirportCode[5];
	int dateStamp;
};

class overFlight{
public:
	string flightNum[7];
	string originAirportCode[5];
	string desAirportCode[5];

};

int main()
{
	fstream myfile;
	struct FlightRec flights;
	overFlight flightInfo;
Last edited on
This is what I meant, note incomplete:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>

using namespace std;

class OverFlight
{
public:
	struct FlightRec
	{
		char flightNum[7];
		char originAirportCode[5];
		char destAirportCode[5];
		int dateStamp;
	};

	OverFlight(FlightRec flight);
	virtual ~OverFlight();

	string FlightNum()
	{
		return string(m_Flight.flightNum);
	}

	string OriginAirportCode()
	{
		return string(m_Flight.originAirportCode);
	}

private:
	FlightRec m_Flight;
};
Create a class that “wraps” or “hides” the information from flightRec


This is (at least to me) is implying you want to create an instance of one class inside the other.

Also, you don't want/need string arrays, just declare them as strings.
1
2
3
4
5
6
class overFlight{
public:
	string flightNum;
	string originAirportCode;
	string desAirportCode;
};
mutexe thank you I was unsure about that
ajh32

Ok i understand now but now you have me completely confused with the

1
2
OverFlight(FlightRec flight);
	virtual ~OverFlight();


why are these used. I figured we would be calling the class in the main
no worries, then you can set your std::string on construction using your char array
e.g.
1
2
        char originAirportCode[13] = "WibbleMeTits"; 	
	std::string strAirportCode(originAirportCode);
 
OverFlight(FlightRec flight);


Is the custom constructor declaration, you need to add the body of the class (normally in the source file). This is what is called when we are creating an instance of the class, e.g.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    // ...
   FlightRec myflight;
   // assign data to struct instance:
   
   // create instance of OverFlight passing our struct instance through:
   OverFlight myovrflight(myflight);

   // etc...
}


The following:
 
virtual ~OverFlight();


Is the destructor declaration, again you need to add the body, most probably empty. The reason I've added 'virtual' keyword is because if the OverFlight class is inherited and the destructor of OverFlight will still be called; you can leave virtual keyword out for now, it's just a habit I have of always declaring destructors as virtual, don't worry about it for now.
Thanks for the help you 2. I have to get to my day job that pays the bills until I get a little better at this whole programming thing. I will be back on this tonight since this part of the assignment is due tonight so is it ok if I PM either of you if I have any more questions?
I hate to throw a wrench in the works, but I few this differently. We do this sort of thing at work occasionally. The FlightRec equivalent is from an old library and we want to add some C++ safety to it.

You want wrap a FlightRec, not replace it. Don't duplicate the data in FlightRec, just use strings as the parameters/return values of the set/get methods. So I think OverFlight should look like this (in part)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class OverFlight {
private:
    FlightRec rec; // this is the only data member
public:
    OverFlight();
    OverFlight(const FlightRec &rec);
    string getFlightNum() const;
    void setFlightNum(const string &);
    bool read(istream&);
    // etc
}

string OverFlight::getFlightNum() const
{
    return rec.flightNum;
}

void OverFlight::setFlightNum(const string &src)
{
    strncpy(rec.flightNum, src.c_str(), sizeof(rec.flightNum));
    rec.flightNum[sizeof(rec.flightNum)-1] = 0; // null terminate
}

Please ignore my last post. The OP sent me a private message with more details of the assignment and it's clear that the prof wants something closer to what mutexe said.
Topic archived. No new replies allowed.