structures
Download
Skip this Video
Download Presentation
Structures

Loading in 2 Seconds...

play fullscreen
1 / 17

Structures - PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on

Structures. Motivation: a new type. Remember that an array is a collection of variables of same type, a collection of variables of different types is a ‘structure’. Structures hold data that belong together . Examples: Student record student id, name, major, gender, start year, …

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Structures' - quilla


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
motivation a new type
Motivation: a new type

Remember that

an array is a collection of variables of same type,

a collection of variables of different types is a ‘structure’.

  • Structures hold data that belong together.
  • Examples:
    • Student record
      • student id, name, major, gender, start year, …
    • Bank account:
      • account number, name, currency, balance, …
    • Address book:
      • name, address, telephone number, …
  • In database applications, structures are called records.
date example
‘Date’ example
  • A ‘date’ type:
    • Day (integer)
    • Month (integer)
    • Year (integer)
  • Example:

structDate {

intday;

intmonth;

intyear;

} ;

The new composite type “Date” structure

has 3 members.

slide4

Date chrismas;

chrismas.day = 25;

Chrismas.month = 12;

Chrismas.year = 2003

Define new variable of type ‘Date’

Access to member variables using dot operator

struct definition
‘struct’ definition

struct<struct-type> {

<type> <identifier_list>;

<type> <identifier_list>;

...

} ;

Each identifierdefines a memberof the structure.

It is a ‘global’ definition!

slide6

Example:

struct Student {

string Name;

int Id;

string Dept;

char gender;

};

The “Student” structure has 4

members.

slide7

‘struct’ usage

Name

Id gender

Dept

Name

Id gender

Dept

  • Declaration of a variable of struct type:

<struct-type> <identifier_list>;

  • Example:

Student student1, student2;

student1 and student2 are variables of Student type.

student1

student2

slide8

Member access (dot operator)

Name

Id gender

Dept

Chan Tai Man

12345 M

COMP

  • The members of a struct type variable are accessed with the dot (.) operator:

<struct-variable>.<member_name>;

  • Example:

Student1.Name = "Chan Tai Man";Student1.Id = 12345;Student1.Dept = "COMP";Student1.gender = \'M\';cout << "The student is ";if (Student1.gender = ‘F’){ cout << "Ms. ";

else

cout << "Mr. ";

}cout << Student1.Name << endl;

Student1

slide9

struct-to-struct assignment

  • The value of one struct type variable can be assigned to another variable of the same struct type.
  • Example:

Student1.Name = "Chan Tai Man";Student1.Id = 12345;Student1.Dept = "COMP";Student1.gender = \'M\';Student2 = Student1;

Student1

Chan Tai Man

12345 M

COMP

Chan Tai Man

12345 M

COMP

Student2

struct initialization
‘struct’ initialization

Student a = {“John Wong";, 98765, "COMP“, ‘M’;};

slide12

“Nested” structures

struct Point { double x, y;};struct Line { point p1, p2;};struct Triangle { point p1, p2, p3;};

Point P;

Line L;

Triangle T;

(P.x, P.y)

(L.p2.x, L.p2.y)

(L.p1.x, L.p1.y)

(T.p2.x, T.p2.y)

(T.p3.x, T.p3.y)

(T.p1.x, T.p1.y)

slide13

Arrays of structures

0 1 2 … 98 99

0 1 2 … 98 99

  • An ordinary array: One type of data
  • An array of structs: Multiple types of data in each array element.
slide14

Chan Tai Man

12345 M

COMP

. . .

0 1 2 … 98 99

Example:Student Class[100];Class[98].Name = "Chan Tai Man";Class[98].Id = 12345;Class[98].Dept = "COMP";Class[98].gender = \'M\';

Class[0] = Class[98];

slide15

4 3

x y

x y

x y

Structures of arrays

  • We can use arrays inside structures.
  • Example:struct square{ point vertex[4];};square sq;
  • Assign values to Sq using the given square

sq.vertex[0].x = 4;

sq.vertex[0].y = 3;

(4, 3)

(10, 3)

(4, 1)

(10, 1)

slide16

Defining types is fundamental  objects!

    • A simple example of ‘enum’
  • ‘struct’ is obsolete, welcome to ‘class’!
slide17

Enumeration type

0

1

2

Enum Dept {CSE,ECE, MATH};

Dept d;

If (d==CSE) … if (d==0) …

Enum Month {Jan=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};

Month m;

ad