23
Structure is a collection of variables of different data typ It is a derived data type. loyee pno; mpname[20],empadd[30]; 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 member employee emp1; Structure variable Structure tag u omit structure tag, structure variables can be cr laring a structure. It cannot be done later on.

Structure

  • Upload
    zamora

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

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; };. Keyword. Structure tag(optional). Structure members. Structure variable. - PowerPoint PPT Presentation

Citation preview

Page 1: Structure

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 variableStructure tag

Note: If you omit structure tag, structure variables can be created at theTime of declaring a structure. It cannot be done later on.

Page 2: Structure

C++ does not reserve memory for a structure until you declare aStructure 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)

Page 3: Structure

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.

Page 4: Structure

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.

Structure Class

e.g.struct emp class emp{ int no; { int no; float sal; float sal; char name[20]; char name[20];}; };

Page 5: Structure

1. It is a group of related data items It is group of items of sameof any data type. data type.

Structure Arrays

e.g.struct employee float arr[10];{ int empno; char name[20];};

Page 6: Structure

Example: Programstruct stud{ int admno; char name[20];};

void main(){ stud nikhil,shubham; // CREATING STRUCTURE VARIABLEScin>>nikhil.admno;;// INPUTTING DETAILSgets(nikhil.name);cin>>shubham.admno;gets(shubham.name);

PROCESSINGcout<<nikhil.admno<<nikhil.name; //OUTPUTTING DETAILScout<<shubham.admno<<shubham.name;}

Page 7: Structure

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]

Admnoname

Admnoname

Admnoname

Admnoname

Admnoname

Array of Structures

Page 8: Structure

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

Page 9: Structure

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.empnoe1.empnamee1.d1.dde1.d1.mme1.d1.yy

Page 10: Structure

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];}

Page 11: Structure

Initializing Structure Elements- It can be done in 3 ways.

First Method: When you declare a structure at the same time initializinge.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

Page 12: Structure

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}};

Page 13: Structure

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}

Page 14: Structure

Global structure

e.g struct emp{ int empno; char empname[20];};

void main(){ emp e1; }//OK

void abc(){emp e2;}//OK

Local structure

e.gvoid main(){

struct emp{ int empno;

char empname[20];};

emp e1;//OK }

void abc(){emp e2;}//NOT OK

Page 15: Structure

Global structure variable Local structure variable

e.g struct emp{ int empno; char empname[20];};

emp e1;//GLOBALvoid 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;//LOCALe1.empno=3; }//OK

void abc(){strcpy(e1.empname,”puja”);}//NOT OK

Page 16: Structure

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

Page 17: Structure

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”);}

Page 18: Structure

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”);}

Page 19: Structure

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;}

Page 20: Structure

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;}

Page 21: Structure

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}

Page 22: 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;}

Page 23: Structure

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 3.1414 void main(){ cout<<pi*2; pi=pi*2;//illegal}

In a macro with arguments , the arguments are substituted in theReplacement text, then the macro is expanded.

#define SQR(x) (x * x) void main(){cout<<SQR(9);}

During pre processing SQR(9) will beReplaced by (9*9)