Lexington High School Mathematics Math Dept. Front Page  | Student Links  | Family Links 
C++ courses Intro. Prog. I  | Intro. Prog. II  | AP Comp. Sci.  | C++ Info Page 

Structures Reference Sheet

We use variables to hold pieces of information in our programs. There are times when we want to group certain variables together under one name. For example, perhaps we want to look at information about cars. We want to store the make, model, and year of a car. We could do the following:

#include <iostream.h>
#include "apstring.h"

int main()
{
	apstring make,
		  model;

	int year;

	// Information about my car
	make = "Dodge";
	model = "Viper";
	year = 2001;

	cout << "My car is a " << year << ' ' << make << ' ' << model << endl;

	return 0;
}

This piece of code works well enough if we only have one car to keep track of but what if we have several?. Then is it more convenient to group the variables together in something called a structure. A structure in C++ allows us to group various types of variables together and give the group a name. The variables inside the group are called members. Let's work through the example below:

#include <iostream.h>
#include "apstring.h"

struct CAR
{
	apstring make,
		  model;
	int year;
};

int main()
{
	CAR mycar, yourcar, momcar;

	// Information about my car
	mycar.make = "Dodge";
	mycar.model = "Viper";
	mycar.year = 2001;

	// Information about your car
	yourcar.make = "Toyota";
	yourcar.model = "Camry";
	yourcar.year = 1997;

	// Information about mom's car
	momcar.make = "Volvo";
	momcar.model = "850";
	momcar.year = 1995;

	cout << "My car is a " << mycar.year << ' '  << mycar.make
	        << ' '  << mycar.model << endl;
	cout << "Your car is a " << yourcar.year << ' '  << yourcar.make
	        << ' '  << yourcar.model << endl;
	cout << "Mom's car is a " << momcar.year << ' '  << momcar.make
	        << ' '  << momcar.model << endl;

	return 0;
}

Outside of the main program we have created a structure called CAR (by convention, when you create a type of variable, its name is capitalized). To define a structure, start with the keyword struct which tells the computer that you are going to create a new data type, then give a name to the structure (CAR), then list the parts of the structure between curly brackets.

Inside the program we have declared three CAR variables: mycar, yourcar, and momcar. Each of these CAR variables contains information about the make, model and year of the car in question. The make, model and year variables are the members of the CAR structure. To access each of these members, we pick the CAR variable of interest, put a dot, and then add the member name. For example, to access the year of mycar, use mycar.year.

In this way we can keep track of a great deal of information by sensible grouping. Indeed, it is possible to use other structures as members in a structure. This is called nesting. Please see the structures declared in the following example:

#include <iostream.h>
#include "apstring.h"

struct SPECIFICS			//  specific information about
{					//  motor vehicles
	apstring color,
		  transmission;
	int	  num_doors;
};

struct CAR				//  information about cars
{
	apstring make,
		  model;
	SPECIFICS car_specs;
	int year;
};

int main()
{
	CAR mycar;

	// Information about my car
	mycar.make = "Dodge";
	mycar.model = "Viper";
	mycar.car_specs.color = "black";
	mycar.car_specs.transmission = "standard";
	mycar.car_specs.num_doors = 2;
	mycar.year = 2001;

	cout << "My car is a " << mycar.year << ' '  << mycar.make
	        << ' '  << mycar.model << endl;
	cout << "It is " << mycar.car_specs.color << " with a "
	        << mycar.car_specs.transmission << " transmission and "
	        << mycar.car_specs.num_doors << " doors." << endl;

	return 0;
}
Key things to remember: