CS148 Introduction to Programming II

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Programmer Defined Structures (Records)
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Arrays & Structures II Chapter 9. 2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data 
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
CPS120: Introduction to Computer Science Lecture 15A Structures.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Arrays.
1 CS161 Introduction to Computer Science Topic #15.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
What Actions Do We Have Part 1
Chapter 10-1: Structure.
Chapter 7: Introduction to Classes and Objects
Structured Data (Lecture 07)
Records C++ Structs Chapter 10.1.
CS150 Introduction to Computer Science 1
Sequences 9/18/ :20 PM C201: Linked List.
CS149D Elements of Computer Science
C++ Structs.
Records C++ Structs Chapter 12.
Structures.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Structures Lesson xx In this module, we’ll introduce you to structures.
CS149D Elements of Computer Science
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Heterogeneous aggregate datatypes
CS148 Introduction to Programming II
CS 1430: Programming in C++.
Structs And Arrays.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
CS148 Introduction to Programming II
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS149D Elements of Computer Science
Lec17 Structs.
CS149D Elements of Computer Science
CPS120: Introduction to Computer Science
Structure (i.e. struct) An structure creates a user defined data type
Structures Structured Data types Data abstraction structs ---
CS148 Introduction to Programming II
CS148 Introduction to Programming II
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CS148 Introduction to Programming II
Chapter 9: Records (structs)
CS148 Introduction to Programming II
Programming Fundamental
Presentation transcript:

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

Outline Simple versus Structured data types C++ Structs Lecture 4: 1/27/2003 CS148 Spring 2003

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

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

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

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

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

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

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

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