Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
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.
1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.
The evolution of Pokemon society Structures. COMP102 Prog. Fundamentals, Structures / Slide 2 “Good Old Days” At first it was Pokemon and his variables.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
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.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Programming Initializing Data of Struct Type. COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By.
COMP103 - C++ Review1 Variables and Special Chars (Ch. 2, 3)
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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.
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.
Module 4: Structures ITEI222 Advanced Programming.
STRUCT Thanachat Thanomkulabut 1. Array Review 2  Group multiple items of the same type into one "variable" double[] score; score = new.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. 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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
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.
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
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.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
User-Defined Data Types
Programmer Defined Types and Classes
Variables Mr. Crone.
11 Chapter Structured Data
Pointers, Enum, and Structures
Chapter 11: Structured Data.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Programming Structures.
Variables and Special Chars (Ch. 2, 3)
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
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.
Structures.
Chapter 11: Structured Data.
Programming Structures.
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
Structure (i.e. struct) An structure creates a user defined data type
Programming Structures.
Structures in c By Anand George.
Structures Structured Data types Data abstraction structs ---
Programming Fundamental
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

Structures

COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender, start year, … n Bank account:  account number, name, currency, balance, … n Address book:  name, address, telephone number, … * In database applications, structures are called records. Remember that an array is a collection of variables of same type, a collection of variables of different types is a ‘structure’.

COMP104 Structs / Slide 3 ‘Date’ example * A ‘date’ type: n Day (integer) n Month (integer) n Year (integer) * Example: struct Date { int day; int month; int year; } ; The new composite type “Date” structure has 3 members.

COMP104 Structs / Slide 4 Date chrismas; chrismas.day = 25; Chrismas.month = 12; Chrismas.year = 2003 Define new variable of type ‘Date’ Access to member variables using dot operator

COMP104 Structs / Slide 5 ‘struct’ definition struct { ;... } ; Each identifier defines a member of the structure. It is a ‘global’ definition!

COMP104 Structs / Slide 6 struct Student { string Name; int Id; string Dept; char gender; }; The “Student” structure has 4 members. Example:

COMP104 Structs / Slide 7 ‘struct’ usage  Declaration of a variable of struct type: ; * Example: Student student1, student2; student1 and student2 are variables of Student type. student1student2 Name Idgender Dept Name Idgender Dept

COMP104 Structs / Slide 8 Name Idgender Dept Chan Tai Man M COMP Member access (dot operator) * The members of a struct type variable are accessed with the dot (.) operator:. ; * Example: Student1.Name = "Chan Tai Man"; Student1.Id = 12345; Student1.Dept = "COMP"; Student1.gender = 'M'; cout << "The student is "; if (Student1.gender = ‘F’){ cout << "Ms. "; else cout << "Mr. "; } cout << Student1.Name << endl; Student1

COMP104 Structs / Slide 9 Chan Tai Man M COMP struct-to-struct assignment  The value of one struct type variable can be assigned to another variable of the same struct type. * Example: Student1.Name = "Chan Tai Man"; Student1.Id = 12345; Student1.Dept = "COMP"; Student1.gender = 'M'; Student2 = Student1; Student1 Chan Tai Man M COMP Student2

COMP104 Structs / Slide 10 ‘struct’ initialization Student a = { “John Wong";, 98765, "COMP“, ‘M’; };

Putting things together …

COMP104 Structs / Slide 12 struct Point { double x, y; }; struct Line { point p1, p2; }; struct Triangle { point p1, p2, p3; }; Point P; Line L; Triangle T; (P.x, P.y) (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) (T.p2.x, T.p2.y) (T.p1.x, T.p1.y) (T.p3.x, T.p3.y) “ Nested ” structures

COMP104 Structs / Slide 13 Arrays of structures * An ordinary array: One type of data * An array of structs: Multiple types of data in each array element … 98 99

COMP104 Structs / Slide 14 Example: Student Class[100]; Class[98].Name = "Chan Tai Man"; Class[98].Id = 12345; Class[98].Dept = "COMP"; Class[98].gender = 'M'; Class[0] = Class[98]; … Chan Tai Man M COMP

COMP104 Structs / Slide 15 Structures of arrays * We can use arrays inside structures.  Example: struct square{ point vertex[4]; }; square sq;  Assign values to Sq using the given square sq.vertex[0].x = 4; sq.vertex[0].y = 3; (4, 3)(10, 3) (4, 1)(10, 1) 4 3x y

COMP104 Structs / Slide 16 * Defining types is fundamental  objects! n A simple example of ‘enum’ * ‘struct’ is obsolete, welcome to ‘class’!

COMP104 Structs / Slide 17 Enum Dept {CSE,ECE, MATH}; Dept d; … If (d==CSE) … if (d==0) … Enum Month {Jan=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC}; Month m; 012 Enumeration type