Download presentation
Presentation is loading. Please wait.
1
Structures
2
AGGREGATION OF DATA Combining a set of related data elements into one Structure that a program can manipulate as a single object/record.
3
AGGREGATION OF DATA Example: a Student Record may consist of: - string name; - int idNumber; - float average; - char letterGrade;
4
struct declaration in C and C++, we can define a structure with a struct statement This statement describes a structure; it does not create/allocate one!
5
struct declaration Syntax: struct name { list of member declarations } ;
6
struct declaration Syntax:
This is usually written outside/before any function using namespace std; struct name { list of member declarations }; int main() { }
7
struct declaration Semantics: struct name { list of member declarations } ; This defines the word name. It does NOT create/allocate any object. name can now be used as a data type.
8
struct declaration Example: struct student { string name; int id; float average; char grade; };
9
object declaration Syntax: structureName objectName ; Semantics: Allocates an object called objectName containing the members defined in struct structureName
10
object declaration Example: student s;
11
Syntax: Use the Dot Operator to access a member of an object.
objectName.member
12
Example 1: dot operator student s; s.name = "Sally Smith";
s.id = ; s.average = 91.2; s.grade = 'A';
13
Example 2: dot operator student s, t; s.name = "Sally";
t.name = "Billy"; s.id = 1234; t.id = 7654;
14
Example: Array of Structures student s[3]; s[0].name = "Sally";
s[1].name = "Billy"; s[2].name = "Mateo";
15
Populating an Array of Structures
student s[3]; for (int i=0; i<3; i++) { cout << "Enter name: "; cin >> s[i].name; cout << "Enter id: "; cin >> s[i].id; cout << "Enter average: "; cin >> s[i].average; cout >> "Enter grade: "; cin >> s[i].grade; }
16
Searching Array of Structures
cout << "Enter Search Name:"; cin >> srchName; found = -1; for (int i=0; i<numStu; i++) { if (s[i].name == srchName) found = i; } if (found == -1) cout << "Not found"; else cout << s[found].id << s[found].gpa;
17
Vocabulary Term Definition Aggregate
To combine data elements into an object that can be manipulated as one data element. struct A description of a collection of data elements combined into one object. Member A data element of a structure/object (aka. "Field") Object A collection of data elements combined into one structure.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.