C++: Structures, Cont’d

In a previous post we discussed how data structures can be useful in C++ to categorize relevant information together.

We implemented the following example for a driver’s license structure:

struct DriversLicense {
        int number;
        string name;
        string address;
        /**
        we'll cover DOB later 
        (hint: think of it as a struct.)
        */
} MyLicense;


We can create more objects of this in our main function like so:

int main()
{
	MyLicense.number = 54321;
	MyLicense.name = "Marvin The Paranoid Android";
	MyLicense.address = "Magrathea";

	struct DriversLicense anotherLicense;
	anotherLicense.number = 42;
	anotherLicense.name = "Deep Thought";
	anotherLicense.address = "Somewhere in the Galaxy";

	return 0;
}

So, here we’ve created two objects of the type struct DriversLicense: MyLicense is declared in the global scope so all operations done in this source file may use it. In the other hand, anotherLicense is declared in int main() scope so only operations within this scope may use it; any external scopes cannot access anotherLicense.

For convenience, we could define our structure using the typedef keyword like so:

typedef struct DriversLicense {
	int number;
	string name;
	string address;
	/**
	we'll cover DOB later 
	(hint: think of it as a struct.)
	*/
} MyLicense;

Now, MyLicense became a type that maybe used to declare objects of struct DriversLicense. So, our main could look like this:

int main()
{
	MyLicense anotherLicense;
	anotherLicense.number = 42;
	anotherLicense.name = "Deep Thought";
	anotherLicense.address = "Somewhere in the Galaxy";

	return 0;
}

Also, notice that you won’t be able to use MyLicense as an object anymore. It is now a type! So, MyLicense.number = 54321; is invalid and the compiler will throw an error.

We have mentioned before that we want to add a field for Date Of Birth. Take a look at the following code:

typedef struct // notice: we ommited struct name from here
{ 
	string month;
	int day;
	int year;
}Date; // created a type "Date" that will be used to declare objects

typedef struct DriversLicense 
{
	int number;
	string name;
	string address;
	Date dateOfBirth; // notice: a structure within a structure.
} MyLicense;

int main()
{
	MyLicense anotherLicense;
	anotherLicense.number = 42;
	anotherLicense.name = "Deep Thought";
	anotherLicense.address = "Somewhere in the Galaxy";
	anotherLicense.dateOfBirth.month = "January"; // assign some values for DOB
	anotherLicense.dateOfBirth.day = 1;
	anotherLicense.dateOfBirth.year = 1;

	return 0;
}

Above, we created a struct to hold date information, then we created an object of that type within DriversLicense structure to be used for Date Of Birth information. In the main we can just access anotherLicense.dateOfBirth with dot notation and store information for all the drivers license we want.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>