Your SlideShare is downloading. ×
0
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Structures
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Structures

2,379

Published on

Use of structures in C++ programming

Use of structures in C++ programming

Published in: Technology, Education
2 Comments
13 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,379
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
22
Comments
2
Likes
13
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. STRUCTURES <ul><li>User defined derived data types: </li></ul><ul><li>Class </li></ul><ul><li>Structure </li></ul><ul><li>Union </li></ul><ul><li>Enumeration </li></ul><ul><li>Typedef </li></ul>
  • 2. <ul><li>It is a collection of dissimilar variables referenced under one name. </li></ul><ul><li>For eg: to store a student’s information (i.e. name, roll no, class, marks, grade ) or to store date’s information (i.e day, month, year ) : structures come to help. </li></ul><ul><li>Syntax: struct tag { type var_name; type var_name; type var_name; } structure-variables; </li></ul>STRUCTURES
  • 3. Defining a structure and declaring a structure <ul><li>Defining a structure </li></ul><ul><li>struct stutype </li></ul><ul><li>{ </li></ul><ul><li>short roll no; </li></ul><ul><li>short class; </li></ul><ul><li>float marks; </li></ul><ul><li>char grade; </li></ul><ul><li>} </li></ul><ul><li>Declaring a structure </li></ul><ul><li>stutype senior_student, junior_student; </li></ul>
  • 4. <ul><li>Defining a structure and Declaring a structure </li></ul><ul><li>struct stutype </li></ul><ul><li>{ </li></ul><ul><li>short roll_no; </li></ul><ul><li>short class; </li></ul><ul><li>float marks; </li></ul><ul><li>char grade; </li></ul><ul><li>} senior_student, junior_student; </li></ul>
  • 5. Referencing of individual structure elements using dot operator <ul><li>senior_student.roll_no; </li></ul><ul><li>senior_student.class; </li></ul><ul><li>senior_student.marks; </li></ul><ul><li>senior_students.grade; </li></ul><ul><li>junior_student.roll_no; </li></ul><ul><li>junior_student.class; </li></ul><ul><li>junior_student.marks; </li></ul><ul><li>junior_students.grade; </li></ul>
  • 6. STORAGE OF STRUCTURE ELEMENTS IN MEMORY Senior_Student marks class roll_no grade Total memory requirement for the variable senior_student of the type stutype is : 2+2+4+1 = 9 bytes
  • 7. INITIALISING STRUCTURE ELEMENTS <ul><li>senior_student.roll_no=1; </li></ul><ul><li>senior_student.class=11; </li></ul><ul><li>senior_student.marks=70; </li></ul><ul><li>senior_students.grade=‘a’; </li></ul><ul><li>OR </li></ul><ul><li>stutype senior_student = { 01, 11, 70, ‘a’}; </li></ul>
  • 8. Structure Assignments <ul><li>Note: structure assignments are possible only if both the structures are of same structure. </li></ul><ul><li>struct one { int a; } </li></ul><ul><li>struct two { int a; } </li></ul><ul><li>one s1,s3; </li></ul><ul><li>two s2; </li></ul><ul><li>cin&gt;&gt;s1.a; </li></ul><ul><li>s2=s1; // incorrect b/c though s1 and s2 have similar elements but are of different type ‘one’ and ‘two’ </li></ul><ul><li>s3=s1; // correct </li></ul>
  • 9. Nested Structures and Containership <ul><li>Containership </li></ul><ul><li>e.g. </li></ul><ul><li>struct addr </li></ul><ul><li>{ </li></ul><ul><li>int houseno; </li></ul><ul><li>char area[26]; </li></ul><ul><li>char city[26]; </li></ul><ul><li>char state[26]; </li></ul><ul><li>}; </li></ul><ul><li>struct emp </li></ul><ul><li>{ </li></ul><ul><li>int empno; </li></ul><ul><li>char name[26]; </li></ul><ul><li>char desig [16]; </li></ul><ul><li>addr address; </li></ul><ul><li>float basic; </li></ul><ul><li>} worker; </li></ul>
  • 10. Structure within a Structure ( nested structure ) <ul><li>struct stu </li></ul><ul><li>{ </li></ul><ul><li>int admn_no; </li></ul><ul><li>int roll_no; </li></ul><ul><li>char name[30]; </li></ul><ul><li>int class; </li></ul><ul><li>float marks[5]; </li></ul><ul><li>struct add </li></ul><ul><li>{ </li></ul><ul><li>int house_no; </li></ul><ul><li>char city[20]; </li></ul><ul><li>} address; </li></ul><ul><li>} nur_stu[100], jr_stu[100], sr_stu[100]; </li></ul>
  • 11. Accessing nested structure elements <ul><li>worker.empno=123; </li></ul><ul><li>worker.address.houseno =12 </li></ul><ul><li>worker.address.area=“rohini”; </li></ul><ul><li>Etc. </li></ul><ul><li>Note: Dot operator can be used more than once in a single statement to approach the nested elements. </li></ul>
  • 12. Structures and Arrays <ul><li>Arrays within a structure </li></ul><ul><li>Array of a structure </li></ul>
  • 13. Arrays within structures <ul><li>struct student </li></ul><ul><li>{ </li></ul><ul><li>int roll_no; </li></ul><ul><li>char name[21]; </li></ul><ul><li>float marks[5]; </li></ul><ul><li>} learner; </li></ul><ul><li>To access the marks of 1 st subject of the learner we need to use: </li></ul><ul><li>learner.marks[0] </li></ul>
  • 14. Need for structure is explained along with Structures and Arrays <ul><li>For eg. To store details of students (admn_no, name, roll_no, address, marks) where there are 100 nursery students, 100 junior students and 100 senior students. </li></ul><ul><li>Approach 1: </li></ul><ul><li>nur_stu_admn_no[100]; jr_stu_admn_no[100]; </li></ul><ul><li>nur_stu_name[100]; jr_stu_name[100]; </li></ul><ul><li>nur_stu_roll_no[100]; and so on total 5 * 3 = 15 array </li></ul><ul><li>nur_stu_address[100]; type variables will be created. </li></ul><ul><li>nur_stu_marks[100]; </li></ul>
  • 15. Approach 2 using structure <ul><li>struct stu </li></ul><ul><li>{ </li></ul><ul><li>int admn_no; </li></ul><ul><li>int roll_no; </li></ul><ul><li>char name[30]; </li></ul><ul><li>int class; </li></ul><ul><li>float marks[5]; </li></ul><ul><li>struct add </li></ul><ul><li>{ </li></ul><ul><li>int house_no; </li></ul><ul><li>char city[20]; </li></ul><ul><li>} address; </li></ul><ul><li>} nur_stu[100], jr_stu[100], sr_stu[100]; </li></ul>Note: The same problem can be solved using 3 variables.
  • 16. Passing structures to functions <ul><li>There are two ways: </li></ul><ul><ul><li>Passing individual structure elements </li></ul></ul><ul><ul><li>Passing the entire structure </li></ul></ul>
  • 17. Passing structure elements to functions <ul><li>E.g. </li></ul><ul><li>struct date </li></ul><ul><li>{ </li></ul><ul><li>short day; </li></ul><ul><li>short month; </li></ul><ul><li>short year; </li></ul><ul><li>} bdate; </li></ul><ul><li>Func(bdate.day, b.date.month, bdate.year); // fn call </li></ul><ul><li>Can be called like normal variables either using calll by value or call by reference. </li></ul>
  • 18. Passing entire structure to functions <ul><li>WAP to input the details of students (admn_no, name, roll_no, address, marks) where there are 100 nursery students, 100 junior students and 100 senior students. Create a function to input the details. </li></ul>
  • 19. Passing structure by reference <ul><li>WAP to input distance in feet and inches. Compute the sum of the two distances. </li></ul>
  • 20. Returning structure from functions <ul><li>WAP to input distance in feet and inches. Compute the sum of the two distances and return the sum. </li></ul>
  • 21. Reference <ul><li>It is a derived data type </li></ul><ul><li>Provides an alternative name to a variable </li></ul><ul><li>E.g int total; int &amp;sum=total; total = 100; cout&lt;&lt;“sum = “&lt;&lt;sum; cout&lt;&lt;“total = “&lt;&lt;total; </li></ul><ul><li>Output will be 100100 </li></ul>
  • 22. typedef <ul><li>typedef does not create a new data type rather provides an alternative name for the standard types. </li></ul><ul><li>reference provides an alias name for a variable and typedef provides an alias name for a data type. </li></ul><ul><li>E.g. typedef float amount ; // new name for flaot has been created by typedef </li></ul><ul><li> amount loan, saving, installment ; // variable of amount type (i.e. float type) are being created. </li></ul>
  • 23. #define preprocessor directive <ul><li>Begins with a pound/hash symbol ( # ) </li></ul><ul><li>Preprocessing phase occurs before a program is compiled </li></ul><ul><li>Allows us to define symbolic names and constants </li></ul><ul><ul><li>E.g. # include&lt;iostream.h&gt; </li></ul></ul><ul><ul><li># define PI = 3.14159 </li></ul></ul><ul><ul><li> void main() </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>int r = 10; </li></ul></ul><ul><ul><li>float cir; </li></ul></ul><ul><ul><li>cir= PI = ( r*r); </li></ul></ul><ul><ul><li>cout&lt;&lt;“Area of circle : “&lt;&lt;cir; </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li># define name= “Computer Science C++” </li></ul></ul>
  • 24. MACROS <ul><li>#define can be used to create macros . </li></ul><ul><li>Macros are expressions . </li></ul><ul><li>After the preprocessing the macro is replaced by the text. </li></ul><ul><li>The text replacement for a macro is known as macro expansion . </li></ul><ul><ul><li>E.g. #include&lt;iostream.h&gt; </li></ul></ul><ul><ul><ul><li> #define square(x) x*x </li></ul></ul></ul><ul><ul><ul><li> void main() </li></ul></ul></ul><ul><ul><ul><li> { </li></ul></ul></ul><ul><ul><ul><li>int value = 3; </li></ul></ul></ul><ul><ul><ul><li>cout&lt;&lt;square(value); </li></ul></ul></ul><ul><ul><ul><li> } </li></ul></ul></ul>
  • 25. <ul><li>While defining macros, make sure to use parenthesis to get correct results </li></ul><ul><ul><li>E.g. # define PI = 3.14159 </li></ul></ul><ul><ul><li>#define circle_area(x) PI * X *X and </li></ul></ul><ul><ul><li>You are using it as: area= circle_area(c+2); </li></ul></ul><ul><ul><li>area = 3.14159 * c + 2 * c + 2 </li></ul></ul><ul><ul><li>To get correct results: </li></ul></ul><ul><ul><li># define circle_area ( (x) ) PI * ( x ) * ( x ) </li></ul></ul>
  • 26. <ul><li>You can also define macros with multiple arguments </li></ul><ul><li>E.g. # define rectangle_area ( a, b) ( (a) * (b)) </li></ul><ul><li>rectarea = rectangle_area(x+4, y+7) would expand to: </li></ul><ul><li>rectarea= ( (x+4) * (y+7) ); </li></ul>

×