Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structure.
Structures Spring 2013Programming and Data Structure1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
1 Lab Session-12 CSIT121 Fall 2004 Structures and Their Usage Passing Struct Variables to Functions.
Operator Overloading in C++
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.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Presentation on Structure. Structure Structure - It is a collection of variables referenced under one name. The keyword struct tells the compiler that.
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.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Advanced Data types and Sorting
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
Learners Support Publications Classes and Objects.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
11 Introduction to Object Oriented Programming (Continued) Cats.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Structures, Classes and Objects Handling data and objects Unit - 03.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
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++
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
CO1401 Program Design and Implementation
Objectives Identify the built-in data types in C++
Student Book An Introduction
Structures - Part II aggregate operations arrays of type struct
DATA HANDLING.
S. Kiran, PGT (CS) KV, Malleswaram
Pass by Reference, const, readonly, struct
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object Oriented Programming Using C++
Chapter 1: Introduction to Data Structures(8M)
Submitted By : Veenu Saini Lecturer (IT)
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Functions Chapter No. 5.
Presentation transcript:

Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char empname[20],empadd[30]; float empsal; }; It is a user defined data type. We can create structure variables to hold values. E.g employee emp1; (int a1;) Keyword Structure tag(optional) Structure members employee emp1; Structure variable Structure tag Note: If you omit structure tag, structure variables can be created at the Time of declaring a structure. It cannot be done later on.

C++ does not reserve memory for a structure until you declare a Structure variable. e.g. struct emp { int empno; char empname[20]; }; Memory is not reserved emp e1; Memory will now be reserved for structure emp = sizeof(emp) = 22 (2 of int + 20 of char)

Accessing structure variables with dot operator e.g. employee emp1; To input the values in emp1 cin>>emp1.empno (structure variable.structure member) gets(emp1.empname); gets(emp1.empadd); cin>>emp1.empsal; Referencing Structure Elements: Once a structure variable has been Defined its members can be accessed through the use of dot operator.

1.It is declared with keyword struct It is declared with keyword class. 2. Its members are public by default. Its members are private by default. 3. It are usually used to hold data.It are used to hold both data and functions. StructureClass e.g. struct empclass emp { int no;{ int no; float sal; float sal; char name[20]; char name[20];};

1.It is a group of related data items It is group of items of same of any data type. data type. StructureArrays e.g. struct employee float arr[10]; { int empno; char name[20]; };

Example: Program struct stud { int admno; char name[20]; }; void main() { stud nikhil,shubham; // CREATING STRUCTURE VARIABLES cin>>nikhil.admno;;// INPUTTING DETAILS gets(nikhil.name); cin>>shubham.admno; gets(shubham.name); PROCESSING cout<<nikhil.admno<<nikhil.name; //OUTPUTTING DETAILS cout<<shubham.admno<<shubham.name; }

If you have to have 42 such students then you will not create 42 structure variables but you will create an array of structures. struct stud { int admno; char name[20]; }; stud s[42];// ARRAY of Structure of 42 students S[0] s[1] s[2] s[3] s[4]……………………………………..s[41] Admno name Admno name Admno name Admno name Admno name Array of Structures

Example: Program #include struct stud { int admno; char name[20]; }; void main() { stud s[42]; // CREATING an array of STRUCTURE for(int I=0;I<42;I++) { cin>>s[I].admno;;// INPUTTING DETAILS gets(s[I].name); }PROCESSING for(I=0;I<42;I++) {cout<<s[I].admno<<s[I].name; //OUTPUTTING DETAILS }

NESTED STRUCTURE(structure within structure) If you need, you can include an already defined structure as a member in another structure. Example: struct dob { int dd,mm,yy; }; struct emp { int empno; char empname[20]; dob d1; } e1; Accessing Nested structure: e1.empno e1.empname e1.d1.dd e1.d1.mm e1.d1.yy

Arrays within structure : when structure element happens to be an array. E.g. struct student { int rollno; float marks[5];// Arrays within structure }; How to Access: Student s1; cin>>s1.rollno; for(int I=0;I<5;I++) cin>>s1.marks[I]; student s[50]; for(I=0;I<50;I++) { cin>>s[I].rollno; for(j=0;j<5;j++) cin>>s[I].marks[j]; }

Initializing Structure Elements- It can be done in 3 ways. First Method: When you declare a structure at the same time initializing e.g. struct emp {int empno; char empname[20]; } e1={1, “puja”}; e.g. struct emp {int empno; char empname[20]; }; void main() { emp e1={1, “puja”}; Second Method: Initialize a structure within the body of the program

Third way: Initializing individual members of the structure e.g. void main() { emp e1; e1.empno=1; strcpy(e1.empname, “puja”); } Initializing structure within structure Example: struct dob { int dd,mm,yy; }; struct emp { int empno; char empname[20]; dob d1; }; emp e2={1, “puja”,{30,11,98}};

Structure Assignment (Copy Structure) You can copy the members of one structure variable to those of another structure variables as long as both structures have the same format. Only assignment is (=) possible for 2 similar structure, other operations like comparison (==, !=) are not defined for similar Structures. e.g. struct emp { int empno; char empname[20]; }e1,e2; void main() { e1.empno=12; strcpy(e1.empname, “puja”); e2=e1;// copy member by member of e1 to e2 }

Global structure e.g struct emp { int empno; char empname[20]; }; void main() { emp e1; }//OK void abc() {emp e2;}//OK Local structure e.g void main() { struct emp { int empno; char empname[20]; }; emp e1;//OK } void abc() {emp e2;}//NOT OK

Global structure variableLocal structure variable e.g struct emp { int empno; char empname[20]; }; emp e1;//GLOBAL void main() { e1.empno=3; }//OK void abc() {strcpy(e1.empname,”puja”);}//OK e.g struct emp { int empno; char empname[20]; }; void main() {emp e1;//LOCAL e1.empno=3; }//OK void abc() {strcpy(e1.empname,”puja”);}//NO T OK

Assignment Q1. WAP to create a structure with the name atom having atomic no. and atomic weight as its members. The program should input the Structure members and display them on the screen. Q2. Details of the employee comprise of 3 items, such as name, date of birth and salary. Date of birth comprises of 3 members such as day, month and year. Define nested structure to accept the details of 10 employees who gets lowest salary. Q3. Declare a structure of accounts of 10 customers with the following Fields: Name of the depositor, account no, type of account(‘s’ or ‘c’), balance amount. WAP to perform the following: 1.Initialise structure members 2. To deposit money 3. Withdraw Money after checking minimum balance (Rs. 500/) 4. To display the Information of particular customer according to account no. To be done in LAB

Passing entire structure members to a function BY VALUE: struct emp { int empno; char empname[20]; }; void main() { emp e1; e1.empno=1; strcpy(empname, “puja”); func(e1); cout<<e1.empno; cout<<e1.empname; } void func( emp e1)//pass by value { e1.empno=12; strcpy(e1.empname, “gupta”); }

Passing entire structure members to a function BY REFERENCE: struct emp { int empno; char empname[20]; }; void main() { emp e1; e1.empno=1; strcpy(empname, “puja”); func(e1); cout<<e1.empno; cout<<e1.empname; } void func( emp &e1)//pass by reference { e1.empno=12; strcpy(e1.empname, “gupta”); }

Passing individual structure members to a function BY VALUE: struct emp { int empno; char empname[20]; }; void main() { emp e1; e1.empno=1; strcpy(empname, “puja”); func(e1.empno); cout<<e1.empno; cout<<e1.empname; } void func( ? num) { num=12; }

Passing individual structure members to a function BY REFERENCE: struct emp { int empno; char empname[20]; }; void main() { emp e1; e1.empno=1; strcpy(empname, “puja”); func(e1.empno); cout<<e1.empno; cout<<e1.empname; } void func( int ? num) { num=12; }

Returning Structure from Functions: Functions can return structure also Example: struct distance{ int feet, inches;}; void main() { distance length1,length2,total; …………………………….. total=sumdist(length1,length2); cout<<total.feet<<total.inches; } distance sumdist(? l1, ? l2) { distance l3; l3.feet=l1.feet+l2.feet+(l1.inches+l2.inches)/12; l3.inches=(l1.inches+l2.inches)%12; return l3; // RETURNING A STRUCTURE }

Symbolic Constants: They are used to assign values to variables that cannot be changed During program execution. The significance is that if we have to Modify the value of a symbolic constant in a program, the modification has to be done at one place, where constant is declared. void main() { const int x=100; const float pi=3.1414; pi=pi*2;//illegal cout<<pi*2; }

Macro Macro is an operation defined in a #define preprocessor directive. A Macro may be defined with or without arguments. A macro without arguments is processed like a symbolic constant. #define pi void main() { cout<<pi*2; pi=pi*2;//illegal } In a macro with arguments, the arguments are substituted in the Replacement text, then the macro is expanded. #define SQR(x) (x * x) void main() {cout<<SQR(9);} During pre processing SQR(9) will be Replaced by (9*9)