CS132L-Lesson5

CS132L-Lesson5 - Lesson5Structures

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 5 - Structures  C++ allows the programmer to extend the language through new data types. Structure A structure provides a means of grouping variables under the same name for  easier handling and identification.  You may look at a structure as a cabinet that  holds different values. In Figure 1. Person’s clothes cabinet; clothes cabinet stores  clothes, each drawer contains different types of clothes that makes up the persons  wardrobe.  Drawer 1 stores t-shirts, drawer 2 stores all pants, drawer 3 stores all  socks.  Each drawer can only store a specific type of clothing. To create a container that will hold information of a student, we can therefore  a cabinet that contains drawers that holds variables that define the properties of an  employee (see Figure 2). Figure 1.  Person’s clothes cabinet socks pants t-shirts
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Figure 2.  Employee cabinet Employee  Employee Age
Background image of page 2
Declaring a structure Syntax: struct structure-name { field-type field-name // Comment field-type field-name // Comment .... } ; Example: struct employee {                      //name of new struct type char name;    int age;          fieldnames or member names float salary; };   // do not forget semicolon here! To access the employee structure, use: variable.field Example: employee newlyHired; cin>>newlyHired.name; cin>>newlyHired.age; newlyHired.salary = 326; Arrays of Structures
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Structures and arrays can be combined. Suppose you want to record the time 
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/14/2011 for the course COMPUTER 091 taught by Professor Rajivsir during the Summer '11 term at MIT.

Page1 / 8

CS132L-Lesson5 - Lesson5Structures

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online