Skip to main content

Section 15.1 Structs

In C++, the simplest way to create a new aggregate data type is by defining a struct. A struct is a collection of variables, possibly of different types, that are grouped together under a single name.
As a simple example, let us consider a point in the x, y plane. In mathematical notation, points are often written in parentheses, with a comma separating the coordinates. For example, \((0, 0)\) indicates the origin, and \((x, y)\) indicates the point \(x\) units to the right and \(y\) units up from the origin.
To create a new data type that represents a point, we can use this struct definition:
struct Point {
  double x, y;
};
This definition indicates that there are two elements in this structure, named x and y. These elements are called instance variables or members.

Warning 15.1.1.

It is a common error to leave off the semi-colon at the end of a structure definition. It might seem odd to put a semi-colon after a squiggly-brace, but you’ll get used to it.
The struct definition must appear before it is used and be in scope where you use it. This means that they generally appear outside of any function definition, usually at the beginning of the program (after the include statements) so that they are available in all the functions. If building a program in multiple files, the struct definition generally goes into a header or module file that other files can import or include to make use of.
A struct defines a new type of data. Think of it as a β€œblueprint” that describes how to build an item of that type. struct Point does not actually create a point anymore than the blueprint of a house gives you something to live in. To actually make a point that we can work with, we need to create a variable using the new data type that has been defined. The value that is created is referred to as an instance of the struct.
Point blank;
blank.x = 3.0;
blank.y = 4.0;
The first line is a conventional variable declaration: blank has type Point. The next two lines initialize the instance variables of the structure. The β€œdot notation” used here is similar to the syntax for invoking a function on an object, as in fruit.length().
The result of these assignments is shown in the following state diagram:
’blank’ names a box with two halves. One is ’x: 3’ the other is ’y: 4’.
Figure 15.1.1.
As usual, the name of the variable blank appears outside the box and its value appears inside the box. In this case, that value is a compound object with two named instance variables.

Checkpoint 15.1.1.

Which of the following would be the correct way to initialize the x instance variable of the Point object?
struct Point() {
  double x, y;
};

int main() {
  Point nice;
  ???
}
  • blank.x = 3.0;
  • This declaration would not work for the specific code block below.
  • Point.x = 3.0;
  • The specific name of the structure should be used, not its type.
  • nice.x = 3.0;
  • Yes, we can access and modify the instance variables using the dot operator.
  • nice.x( ) = 3.0;
  • You are not calling a function therefore brackets for an argument list aren’t required.

Checkpoint 15.1.2.

Construct a block of code that correctly creates variables of a certain structure’s type.

Checkpoint 15.1.3.

struct definitions generally occur…
  • outside of any function definition, usually at the beginning of the program
  • Read over the other answer choices as well.
  • after the main function
  • The struct cannot be defined after the main function or else it can’t be used in the program.
  • after the include statements
  • Read over the other answer choices as well.
  • both a and c
  • Yes, structs are usually defined after the include statements and before the main function.
You have attempted of activities on this page.