CS 218 - Object Oriented Programming with C++

Chapter 11 - Records (structs)


This chapter introduces the student to structures, which are frequently used for records in databases. Objectives important to this chapter:

  1. What structs are
  2. Operations on structs
  3. Data and structs
  4. Structs and Functions
  5. Arrays inside structs
  6. Arrays whose members are structs

The text has already explained that an array is a collection of data elements of the same type. You can think of a struct (short for structure) as being like an array whose members are not all the same type. It is also correct to think of a struct as a complex variable that can have several types of members.

A struct provides the means to store several data items, usually of different types,in the same data object. This makes it an appropriate kind of object for databases. Each record in a database usually has several fields that hold different types of data, such as names, ID numbers, addresses, scores, balances, and more. That's what a struct is for.

A struct template describes how a structure is put together. The format for a template is as follows:

	struct structName 	{
	type member1;	
	type member2;	
	type member3;

Note that a semi-colon is used at the end of the struct definition. Before you use a struct in a program, you must define a struct type. Then you must declare an instance (a variable) of that struct type, which will cause memory space to be allocated to it.

Accessing a value stored in a structure is done using the dot (.) operator, as in:


Example: assume we create a structure template for students and call it student:

        struct student {
         string name;
         int ID;
         double course_average;

This makes a new data type called student.

Then we create an instance of that type called student1 by declaring a variable with that type and name:

        student student1;           

We could access the name member of that instance with the code


This notation could be used to read or write information to the member as you would an ordinary variable.

An array of structures is like any other kind of array. In this example, we set a constant for the size of the array, we define a structure, and we declare an array of objects based on that structure:

	  const int MAXBKS = 100;

struct book { string title; string author; string publisher; int ISBN; };
book libro[MAXBKS];

Apply the same rules to an array of structs as to other types of arrays. Use the dot operator to access a member. For example,

/*  meaning the member called 
	title in libro[4] */ 

It is possible to nest structures. A member of a structure can be of type struct.

Structures can be passed to functions in three ways: by passing structures as arguments, by passing pointers to structures as arguments, and by passing structure members as arguments.

When the address of a structure is passed to a function, the indirect membership operator ( ->) is required to gain access to a member. When the structure is passed to a function, the dot operator (.) isrequired to gain access to a member.

Like arrays, you cannot do math on structs, nor can you do comparisons. You can do math and comparisons on members of structs, providing they are not structs themselves.