Struct Data Type in C++ What Are Structures?

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?
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
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.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
More Arrays Arrays and classes Multi-dimensional Arrays Dynamic arrays.
Chapter 11 – Structured Data
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
 Review structures  Program to demonstrate a structure containing a pointer.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Structured Data Types array array union union struct struct class class.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Module 4: Structures ITEI222 Advanced Programming.
Programmer Defined Structures (Records)
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
Arrays & Structures II Chapter 9. 2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data 
Lecture 26: Structures / struct s/. 2 Lecture Contents: t Basics of structs t Struct type definition ( struct reserved word) t Struct type definition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
CO1401 Program Design and Implementation
Chapter 11: Structured Data.
C++ Arrays.
Structures.
S. Kiran, PGT (CS) KV, Malleswaram
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Lecture 12 Oct 16, 02.
Struct Data Type in C++ What Are Structures?
Chapter 1: Introduction to Data Structures(8M)
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
C Programming Lecture-8 Pointers and Memory Management
CPS120: Introduction to Computer Science
Structure (i.e. struct) An structure creates a user defined data type
Structures Structured Data types Data abstraction structs ---
Programming Fundamental
Presentation transcript:

Struct Data Type in C++ What Are Structures? A structure is a data type that is an aggregate; that is, it contains other data types, which are grouped together into a single user-defined type. It is like an array, but the array is homogenous and the structure is heterogeneous Structures are used when it makes sense to associate two or more data variables. Structures are made up of member variables.

Declaring a Struct Type and Struct Variable Synatx: struct struct_name { <type> field1-name; <type> field2-name; … <type> fieldn-name; } ; Declaring a structure introduces a new type of variable into your program, struct variables. Variables of this new type can be defined just like int, char, or float variables are defined.

Declaring a Struct Type and Struct Variable An example of a structure is a payroll record, where the number of hours worked and the pay rate are combined in a structure, as shown below: struct PAYROLL_REC { double hours; double rate; }; The declaration PAYROLL_REC pay1; declares pay1 variable to be of type PAYROLL_REC It Allocates storage space for two variables: hours, rate.

Accessing Members of a Struct Accessing struct is by using the member access operator, a period ( a dot) placed between a struct variable and a member name for that struct type. E.g., pay1. hours = 30.0 pay1.rate = 2.5

Examples on Struct Example1: Using a structure to calculate a weekly salary. #include <iostream.h> struct PAYROLL_REC { double hours; double rate; }; void main() { PAYROLL_REC pay1; pay1.hours = 40.0; pay1.rate = 3.75; cout << "This week's payroll information:" << endl; cout << "Hours worked : " << pay1.hours << endl; cout << "Rate :$" << pay1.rate << endl; double salary = pay1.rate * pay1.hours; cout << "Salary :$" << salary << endl; }

Examples on Struct void main( ) { ExamStats student; Example 2: Using struct and array to store student’s information. #include <iostram.h> #include <cstring> struct ExamStats { char name [20]; int scores[4]; float average; char grade; }; void printStats(ExamStats stuExams ) { cout<<“Exam scores for “<< stuExams.name<<“: “ << stuExams.scores[0]<<“ “<< stuExams.scores[1]<<“ “<< stuExams.scores[2]<<endl; cout <<“Average score: “<<stuExams.average<<endl; cout<<“Letter garde: “<<stuExams.grade<<endl; } void main( ) { ExamStats student; cin.getline(student.name, 20, ‘\n’); for (int i = 0; i<3; i++) cin >>student.scores [i]; cin >> student.average; cin >> student.grade; printStats (student); }

Array of Structs Example: We want to store in an array the students structs defined previously.

Array of Structs #include <iostream.h> #include <cstring> struct ExamStats { char name [20]; int scores[3]; float average; char grade; }; void printStats(ExamStats stud[]) { for (int i=0; i<20;i++) { cout<<"Exam scores for "<< stud[i].name<<": " << stud[i].scores[0]<<" "<< stud[i].scores[1]<<" "<< stud[i].scores[2]<<endl; cout <<"Average score: "<<stud[i].average<<endl; cout<<"Letter garde: “ << stud[i].grade<<endl; } } void main( ) { ExamStats students[20]; //Reading students records and storing them in the array for (int i=0; i<20;i++) { cout<<"Enter student "<<i+1<< "information: "; cin.getline(students[i].name, 20); for (int j= 0; j<3; j++) cin >>students[i].scores [j]; cin >> students[i].average; cin >> students[i].grade; printStats (students); }

Pointers to Structs We can declare pointers to structured data. E.g., the declaration struct electric { char current[2]; int volts; }; electric *p, *q; Defines the variables p and q to be of type “pointer to electric”. Type electric is a struct of two variables: current and volts.

Pointers to Structs (Cont.) Accessing elements of a struct by a pointer: p->current = “AC” p->volts = 115