0% found this document useful (0 votes)
3 views12 pages

Data Structures Code8

Uploaded by

LAU FANG YI Moe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Data Structures Code8

Uploaded by

LAU FANG YI Moe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DATA

STRUCTURES

❑ INTRODUCTION
❑ SINGLE STRUCTURES
❑ ARRAYS OF STRUCTURES
Introduction

2
Introduction (cont'd.)

Structure: data structure that stores different


types of data under single name
- A structure is a class that has no methods
- All variables of class are public (default)

Creating and using a structure requires two steps:


- Declaration
- Assigning values
Single Structures

Example:
birth structure consists of three data items or
fields called structure members

struct DATA TYPES


{
int month;
int day; DATA NAMES
int year;
} birth;
ARRANGEMENT
OF DATA
Single Structures (cont’d.)

Populating the structure: assigning data values


to data items of a structure
- Data structure members are accessed by
giving the structure name and individual
data item name separated by period
- Period is called member access operator
(dot operator)

struct
{
int month;
int day;
int year;
} birth;

Example:
[Link] refers to the first member of birth
structure
[Link] = 2;
CODE 8A: CREATE A
STRUCTURE

#include <iostream>
using namespace std;

int main()
{
struct
{
int month;
int day;
int year;
} birth;

[Link] = 12;
[Link] = 28;
[Link] = 1992;

cout << “My birth date is “


<< [Link] << ‘/’
<< [Link] << ‘/’
<< [Link] << endl;

return 0;
}
Single Structures (cont’d.)

struct
{
int month;
int day;
int year;
} birth;

struct {int month; int day; int year;} birth;

Multiple variables can be declared in the same


statement:
struct
{
int month;
int day;
int year;
} birth, current;

struct {int month; int day; int year;} birth,


current;

• [Link], [Link], [Link]

• [Link], [Link], [Link]


CODE 8B: MULTIPLE
VARIABLES

int main()
{
struct
{
int month;
int day;
int year;
} birth, current;

[Link] = 12;
[Link] = 28;
[Link] = 1992;

[Link] = 7;
[Link] = 7;
[Link] = 2023;

cout << "My birth date is "


<< [Link] << '/'
<< [Link] << '/'
<< [Link] << endl;

cout << "Today is "


<< [Link] << '/'
<< [Link] << '/'
<< [Link] << endl;

return 0;
}
CODE 8C: STRUCTURE TYPE
NAME

#include <iostream>
using namespace std;

struct
{
int month;
int day; Global
int year; Declaration
} birth;

int main()
Date birth = {12, 28, 1992}
{
Date birth;

[Link] = 12;
[Link] = 28;
[Link] = 1992;

cout << “My birth date is “


<< [Link] << ‘/’
<< [Link] << ‘/’
<< [Link] << endl;

return 0;
}
CODE 10C: STRUCTURE
TYPE NAME

#include <iostream>
using namespace std;

Struct Date
{
int month;
int day;
int year;
} ;

int main() Date birth = {12, 28, 1992}


{
Date birth;

[Link] = 12;
[Link] = 28;
[Link] = 1992;

cout << “My birth date is “


<< [Link] << ‘/’
<< [Link] << ‘/’
<< [Link] << endl;

return 0;
}
Arrays of Structures

Real power of structures is realized when same


structure is used for lists of data.
Arrays of Structures (cont'd.)

Data can be processed as single array of ten


structures.

struct PayRecord
{
int idNum;
string name;
double rate;
};

PayRecord employee[10];
employee[0].rate = 16.72;
employee[0].idNum = 32479;
employee[0].name = “Abrams, B.”;

You might also like