Your SlideShare is downloading. ×
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,246

Published on

Use of structures in C++ programming

Use of structures in C++ programming

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

×