1 / 23

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

zamora
Télécharger la présentation

Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Structure tag employee emp1; It is a user defined data type. We can create structure variables to hold values. E.g employee emp1; (int a1;) Note: If you omit structure tag, structure variables can be created at the Time of declaring a structure. It cannot be done later on.

  2. 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)

  3. Referencing Structure Elements: Once a structure variable has been Defined its members can be accessed through the use of dot operator. 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;

  4. Structure Class 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. e.g. struct emp class emp { int no; { int no; float sal; float sal; char name[20]; char name[20]; }; };

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

  6. 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; • }

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

  8. Example: Program #include<iostream.h> #include<stdio.h> 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 }

  9. 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

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

  11. 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”}; Second Method: Initialize a structure within the body of the program e.g. struct emp { int empno; char empname[20]; }; void main() { emp e1={1, “puja”};

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

  13. 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 }

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

  15. Global structure variable Local 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”);}//NOT OK

  16. 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. To be done in LAB • 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: • 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.

  17. Passing entire structure members to a function BY VALUE: void func( emp e1)//pass by value { e1.empno=12; strcpy(e1.empname, “gupta”); } 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; }

  18. Passing entire structure members to a function BY REFERENCE: void func( emp &e1)//pass by reference { e1.empno=12; strcpy(e1.empname, “gupta”); } 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; }

  19. Passing individual structure members to a function BY VALUE: void func( ? num) { num=12; } 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; }

  20. Passing individual structure members to a function BY REFERENCE: void func( int ? num) { num=12; } 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; }

  21. 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 }

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

  23. 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 the Replacement text, then the macro is expanded. During pre processing SQR(9) will be Replaced by (9*9) #define SQR(x) (x * x) void main() {cout<<SQR(9);}

More Related