1 of 11

INTRO TO STRUCTS IN C++

A LECTURE FOR THE C++ COURSE

Each slide may have its own narration in an audio file. �For the explanation of any slide, click on the audio icon to start the narration.

The Professor‘s C++Course by Linda W. Friedman is licensed under a �Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

2 of 11

WHAT IS A STRUCTURE? A RECORD

  • A struct is a C++ keyword that refers to a structure. This is a collection of variables, not necessarily of the same type. These variables are called the members of the structure.
  • A structure is equivalent to a record.
  • A record is a collection of data items related to a single object of processing.
  • More formally, a record is a finite, ordered collection of possibly heterogeneous elements, which may themselves be composite structures. The primary purpose of the record data type is to group together logically related fields. A collection of logically related record occurrences that are treated as a unit is called a file.

RECURSION

2

3 of 11

RECORDS

RECURSION

3

 

 

 

 

 

 

 

 

 

 

 

 

 

Telephone Numbers

 

 

 

Name

Office

 

 

 

 

 

Phone

Phone

Job Title

Empl ID

Rate

Last

Firs

MI

Bldg

Rm

Project Codes

Area

Local

Area

Local

ANALYST

123456789

15.93

Mudd

Joe

H

CSC

403

18

40

41

50

53

202

1234567

301

1234567

The record is a simple yet fundamental type of data structure. A record generally contains an identifying field (key), e.g., EMPLOYEE, a data structure of type record:

Where do we see

  • Array within a record:
  • Record within record:
  • Record within record within array:

4 of 11

STRUCTS IN C++

  • A structure enables you to define a new type that logically groups several fields (members) together.

  • Structures were more important in C (in the old days before object-oriented programming). In C++ they are more likely to be part of a class definition. In fact, a struct is almost the same as a class.

RECURSION

4

General syntax:

struct structName {

--list of members--

};

5 of 11

EXAMPLES

struct point {

double x;

double y;

};

  • Defining a structure is like defining a new type. Then you can declare variables of the new type:

point p1, p2, p3;

  • or like this:

struct point {

double x;

double y;

} p1, p2, p3;

RECURSION

5

Also like this:

struct {

double x;

double y;

} p1, p2, p3;

6 of 11

EXAMPLES

struct point {

double x;

double y;

};

 

struct rectangle {

point upperLeftCorner;

point lowerRightCorner;

};

 

struct circle {

point center;

double radius;

};

 

RECURSION

6

Also these

7 of 11

USING STRUCTS

One way to declare and initialize:

point p5 = {1.0, -8.3};

 

To access individual members, use the dot operator:

p1.x = 12.45;

p1.y = 34.56;

p2.x = 23.4 / p1.x;

p2.y = 0.98 * p1.y;

RECURSION

7

8 of 11

SMALL EXAMPLE

//structs.cpp

/*illustration of use of structs for organizing data into records*/

 

#include <iomanip>

#include <fstream>

using namespace std;

 

ifstream infile ("d:in.dat");

ofstream outfile ("d:out.txt");

 

struct record { //struct definition

int num1;

int num2;

};

int main(){

record rec; //declaring struct variable rec

float avg;

if (!infile) //testing files

cerr << "Error: could not open input file\n";

else if (!outfile)

cerr << "Error: could not open output file\n";

//printing headings

outfile << setw(18) << "Number 1" � << setw(15)<< "Number 2 "

<< setw(15)<< "Average\n\n";

 

while (infile >> rec.num1 >> rec.num2){

avg = (rec.num1 + rec.num2) /2.0;

outfile << setiosflags(ios::showpoint | ios::fixed)

<< setprecision(2) << setw(15) <<rec.num1

<< setw(15) << rec.num2

<< setw(15) << avg << endl;

}

return 0;

}

RECURSION

8

9 of 11

INPUT / OUTPUT

RECURSION

9

10 of 11

WHERE TO USE STRUCTS?

What sort of programs are good candidates for using structs?

  • Employee processing Program
  • Inventory processing Program
  • Even student grading Program

RECURSION

10

11 of 11

REVIEW

RECURSION

11

What did we learn in this lecture? Plenty. Some terms to jog your memory:

    • Structs
    • Declaring a struct
    • Accessing the named elements in a struct