Download presentation
Presentation is loading. Please wait.
1
CS148 Introduction to Programming II
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 4: 1/27/2003 Lecture 4: 1/27/2003 CS148 Spring 2003
2
Outline Simple versus Structured data types C++ Structs
Lecture 4: 1/27/2003 CS148 Spring 2003
3
Simple vs. Structured Data Types
Simple Data types (int, float, double, char, ..) A value is a single data item (can not be further broken into components) Structured Data types A value is a collection of components items Entire collection given a single name Each component can be accessed individually string name = “Ayman”; cout << name[3]; Lecture 4: 1/27/2003 CS148 Spring 2003
4
C++ Structs (Records) Record is heterogeneous structured data type (components can be of different data types, each component called a field) In C++, a record is called a structure, fields called members, each member has a member name Why do we need a struct? Student information (int Sid; string name; float GPA) How about if we have 100 students, we need to declare 3 arrays, one for Student ID, name, and GPA Alternative: Declare a student record having as members (Sid, name and GPA), and declare one array of student records Lecture 4: 1/27/2003 CS148 Spring 2003
5
Example struct StudentRec { int Sid; string name; float GPA;
}; //struct declaration //declare a variable of type StudentRec //struct members associated with actual memory locations StudentRec aStudent; cin >> aStudent.Sid >> aStudent.name; aStudent.GPA = 3.5f; cout << aStudent.Sid << aStudent.name; //member selector (dot notation) Lecture 4: 1/27/2003 CS148 Spring 2003
6
Aggregate Operations on Structs
An aggregate operation is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure Operation Allowed I/O No Assignment Yes Arithmetic No Comparison No Argument Passage Yes (by value or by reference) Return as function’s return value Yes Lecture 4: 1/27/2003 CS148 Spring 2003
7
Examples on aggregate operations
StudentRec st1, st2; st1 = st2; //allowed since both are of type StudentRec st1 = st2 + 10; //Not allowed if (st1 < st2) //Not allowed cin >> st1; //Not allowed cout << st1; //Not allowed cin >> st1.Sid; //Allowed Lecture 4: 1/27/2003 CS148 Spring 2003
8
Alternative Forms of struct declarations
//mix struct declaration with variable declaration //Better to keep them separate for clarity and separation of notions struct StudentRec { int Sid; string name; float GPA; } aStudent; struct int firstMember; Float secondMember; } someVar; //variable of anonymous data type Lecture 4: 1/27/2003 CS148 Spring 2003
9
A struct member can be a function!
struct StudentRec { int Sid; string name; float GPA; string getName() { return name;}; float getGPA() { return GPA;}; }; //struct declaration //declare a variable of type StudentRec StudentRec aStudent; cout << aStudent.Sid<< aStudent.getName() << aStudent.getGPA(); Lecture 4: 1/27/2003 CS148 Spring 2003
10
An array of structs struct StudentRec { int Sid; string name;
float GPA; string getName() { return name;}; float getGPA() { return GPA;}; }; //struct declaration //declare a variable of type StudentRec StudentRec aStudent; StudentRec Students[100]; cout << Students[10].Sid << Students[10].getName(); cout << Students[5].name[4]; Lecture 4: 1/27/2003 CS148 Spring 2003
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.