Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.

Slides:



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

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
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?
Structure.
Programming Languages and Paradigms The C Programming Language.
Structures Spring 2013Programming and Data Structure1.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
EXERCISE IN CLASS CHAPTER 3. PART 1 IDENTIFIER SCENARIO 1 o record1 o 1record o file_3 o return o $tax o Name o name and address o name-and-address o.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Multiple-Subscripted Array
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
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.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Strings Sujana Jyothi C++ Workshop Day 4. A String also called character string is a sequence of contiguous characters in memory terminated by the NUL.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
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.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
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.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
1 Lecture 6 Classes and Data Abstraction: Part II.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
Struct Data Type in C++ What Are Structures?
Arrays.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
 2000 Prentice Hall, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf Introduction Object-oriented programming (OOP) –Objects Encapsulates.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
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.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Classes and Data Abstraction
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Array Data Structure Chapter 6
Glenn Stevenson CSIS 113A MSJC
Structures In C Programming By Rajanikanth B.
Chapter 6: Classes and Data Abstraction
Structure (i.e. struct) An structure creates a user defined data type
Structures in c By Anand George.
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures u Accessing the Array of Structures u Rules When using Structures u Case Study u Exercise/Home Work Dr. Ming Zhang

Syntax of a struct Declaration u Structures Structures ae aggregate data types built using elements of other types including other struct. u Syntax of a sturct struct structure_name { …… Declaratioons for the components making up the sturcture …… }; // must end with a semicolon! Dr. Ming Zhang

Example of Structure Structure definition Structure name struct fruit_case { //struct likes a record int number; // member, or field float average_weight; // member, or field Char name[21]; // member, or field }; Dr. Ming Zhang

Structure Variables u Declare Structure Variables So the structure fruit_case thus created is similar to a type, in that we can declare variables using it: fruit_case orange_case, apple_case; u Structure Variables orange_case and apple_case are structure variables, each one contains the three component number, average_weight, and name. Dr. Ming Zhang

orange_case and apple_case orange_case: apple_case: number: average_weight: average_weight: name: Dr. Ming Zhang

Accessing Members of Structures u Dot Operator (. ) The dot operator (field selection) accesses a structure or class member via the variable name for the object or via a reference to the object. orange_case.average_weight = 4.8; orange_case.number = 64; orange_case.name = “Oranges”; Dr. Ming Zhang

Accessing orange_case orange_case.average_weight = 4.8; orange_case.number = 64; orange_case.name = “Oranges”; orange_case: apple_case: number: 64 number: average_weight: 4.8 average_weight: name: Oranges name: Dr. Ming Zhang

A Structure is Single Variable apple_case = orange_case; orange_case: apple_case: number: 64 average_weight: 4.8 average_weight: 4.8 name: Oranges Dr. Ming Zhang

Initialize Structure Variables fruitcase banana_case = { 96, 0.2, “Bananas”}, tomato_case = {64, 0.5, “Tomatoes”}; banana_case: tomato_case: number: 96 number: 64 average_weight: 0.2 average_weight: 0.5 name: Bananas name: Tomatoes Dr. Ming Zhang

Array of Structures fruitcase cases[4]; cases[1] = banana_case = { 96, 0.2, “Bananas”}, cases[2]=tomato_case = {64, 0.5, “Tomatoes”}; banana_case: tomato_case: number: 96 number: 64 average_weight: 0.2 average_weight: 0.5 name: Bananas name: Tomatoes Dr. Ming Zhang

Accessing the Array of Structures fruitcase cases[4];cases[3] = strawberry_case; cases[3].number =24; cases[3].average_weight=0.1; cases[3].name=“Strawberry”; strawberry_case: tomato_case: number: 24 number: 64 average_weight: 0.1 average_weight: 0.5 name: Strawberry name: Tomatoes Dr. Ming Zhang

Rules When using Structures u We may not have two fields of the same name in the one structure. u We may have two fields of the same name in different structures. u A structure must have at least one field. u A structure may contain fields which are themselves structures, but it may not contain another copy of itself. u A structure can contain arrays, and arrays can have structures as elements. Dr. Ming Zhang

Case Study: Time Structure (Figure 6.1) (1) #include using std::cout; using std::endl; struct Time { // structure definition int hour; // 0-23 int minute; // 0-59 int second; // 0-59 }; void printMilitary( const Time & ); void printStandard( const Time & ); Dr. Ming Zhang

Case Study: Time Structure (Figure 6.1) (2) int main() { Time dinnerTime;//variable of new type Time // set members to valid values dinnerTime.hour = 18; dinnerTime.minute = 30; dinnerTime.second = 0; cout << "Dinner will be held at "; printMilitary( dinnerTime ); cout << " military time,\nwhich is "; printStandard( dinnerTime ); cout << " standard time.\n"; Dr. Ming Zhang Dinner will be held at 18:30 military time Which is 6:30 PM standard time.

Case Study: Time Structure (Figure 6.1) (3) // set members to invalid values dinnerTime.hour = 29; dinnerTime.minute = 73; cout << "\nTime with invalid values: "; printMilitary( dinnerTime ); cout << endl; return 0; } Dr. Ming Zhang Time with invalid values: 29:73

Case Study: Time Structure (Figure 6.1) (4) // Print the time in military format void printMilitary( const Time &t ) { cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":" << ( t.minute < 10 ? "0" : "" ) << t.minute; } // Print the time in standard format void printStandard( const Time &t ) { cout << ( ( t.hour == 0 || t.hour == 12 ) ? 12 : t.hour % 12 ) << ":" << ( t.minute < 10 ? "0" : "" ) << t.minute << ":" << ( t.second < 10 ? "0" : "" ) << t.second << ( t.hour < 12 ? " AM" : " PM" ); } Dr. Ming Zhang

Question 1 - Exercise/Home Work (1) u State whether the following are true or false u (a) One structure can not have two fields with the same name. u (b) One structure can not have two fields with the same type of data. u One structure must have al least two fields. u A field in a structure can not be a structure. Dr. Ming Zhang

Question 2 - Exercise/Home Work (3) Our task is to sort a file containing a company’s employee data. Each record contains information about a single employee, such as surname, given name, address, title, payroll number, age, salary. All data about a single employee can be collected in a structure. Please create a structure to fill employee data and declare an array to store information of 50 employees. Dr. Ming Zhang

Question 3 -Exercise/Home Work (5) Create a Time structure with three integer members: hour, minute and second. Define a single Time structure called lunchtime and use the dot operator to initialize the structure members with the values 12 for hour, 30 for minute and 0 for second. Print the Time in military format and standard format. Note that the print functions receive references to constant Time structures. Dr. Ming Zhang