Structure.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
UNIT IV.
Copyright © 2002 Pearson Education, Inc. Slide 1.
C Language.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
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 Chapter 11 Introducing the Class Pages ( )
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Programming and Data Structure
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
True or false A variable of type char can hold the value 301. ( F )
CS Winter 2011 Further Introduction to the C programming language.
16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
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.
Advanced Data types and Sorting
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Learners Support Publications Classes and Objects.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
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.
Array, Structure and Union
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
1 CSC241: Object Oriented Programming Lecture No 02.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
CPS120: Introduction to Computer Science Lecture 15A Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
1 CS161 Introduction to Computer Science Topic #15.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Structures.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
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.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
LESSON 06.
Structs versus Classes
Programming Structures.
Lecture 4-7 Classes and Objects
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.
Classes and Objects.
Chapter 1: Introduction to Data Structures(8M)
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Submitted By : Veenu Saini Lecturer (IT)
C Programming Pointers
Pointers, Dynamic Data, and Reference Types
Structures Chapter 4.
Presentation transcript:

Structure

Structure A Structure is a collection of related data items, possibly of different types. Elements of structure are called members. A structure is heterogeneous in that it can be composed of data of different types. In contrast, array is homogeneous since it can contain only data of the same type

Declaring a Structure The structure definition tells how the structure is organized It specifies what members the structure will have. Syntax: struct structure_name { type a; type b; type c; };

Declaring a Structure

Declaring a Structure The structure declaration/definition creates the variables of type part. It does not itself create any structure variables; that is, it does not set aside any space in memory or even name any variables. This is unlike the definition of a simple variable, which does set aside memory. A structure definition is merely a specification for how structure variables will look when they are defined

Defining a Structure Variable When a structure variable is declared, space is reserved in the computer memory to hold all the member of the structure. The memory occupied by the structure variable is equal to the sum of the memory occupied by each member of the structure. The structure can be defined inside or outside the main() function If the structure is defined outside main() it’s variable can be declared inside or outside the main() If the structure is defined inside main() it’s variable can only be declared inside the main()

Defining a Structure Variable Structure variable can be declared by giving the structure name and any variable name like so Structure_name structure variable _name;

Accessing Structure Members Once a structure variable has been defined, its members can be accessed using the dot operator The structure member is written in three parts: the name of the structure variable The dot operator, which consists of a period (.) and the member name The dot operator is also called member access operator Syntax: struct_variable.struct_member

Example #include <iostream.h> #include <conio.h> struct part { int modelnumber; int partnumber; float cost; }; void main() part p1; //define a structure variable p1.modelnumber = 6244; //give values to structure members p1.partnumber = 373; p1.cost = 217.55; //display structure members cout << “Model “ << p1.modelnumber; cout << “, part “ << p1.partnumber; cout << “, costs” << p1.cost << endl; getch(); }

Initializing Structure Members structure members can be initialized when the structure variable is defined. struct part { int modelnumber; int partnumber; float cost; }; void main() part p1 = { 6244, 373, 217.55 }; //initialize the structure variable //display structure members cout << “Model “ << p1.modelnumber; cout << “, part “ << p1.partnumber; cout << “, costs” << p1.cost << endl; getch(); }

Reading values from keyboard struct part { int modelnumber; int partnumber; float cost; }; void main() part p1; cout<<“Enter model number”; cin>>p1.modelnumber; cout<<“Enter part number”; cin>>p1.partnumber; cout<<“Enter cost”; cin>>p1.cost; //display structure members cout << “Model “ << p1.modelnumber; cout << “, part “ << p1.partnumber; cout << “, costs” << p1.cost << endl; getch(); }

Array type member of structure Members of the structure can be of different types Can be simple variable, or array variables Example struct record { char name[15]; int sub[4]; }; record r ={“Ali” , {10,12,13,14}}; * complete the program by taking values from user

Structure Variable as Array A structure variable may be declared as array This is done to manage large number of records. Each variable of the array represents a complete record Example struct result { char name[15]; int id; int total; }; result r[5]; * complete the program by taking values from user