STRUCTURES User defined derived data types: Class Structure Union Enumeration Typedef
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
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;
Defining a structure and Declaring a structure struct stutype { short roll_no; short class; float marks; char grade; } senior_student, junior_student;
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;
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
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’};
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
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;
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];
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.
Structures and Arrays Arrays within a structure Array of a structure
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]
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];
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.
Passing structures to functions There are two ways: Passing individual structure elements Passing the entire structure
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.
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.
Passing structure by reference WAP to input distance in feet and inches. Compute the sum of the two distances.
Returning structure from functions WAP to input distance in feet and inches. Compute the sum of the two distances and return the sum.
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
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.
#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++”
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);   }
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 )
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) );

Structures

  • 1.
    STRUCTURES User definedderived data types: Class Structure Union Enumeration Typedef
  • 2.
    It is acollection 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 structureand 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 structureand Declaring a structure struct stutype { short roll_no; short class; float marks; char grade; } senior_student, junior_student;
  • 5.
    Referencing of individualstructure 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 STRUCTUREELEMENTS 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 ELEMENTSsenior_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 andContainership 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 aStructure ( 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 structureelements 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 ArraysArrays within a structure Array of a structure
  • 13.
    Arrays within structuresstruct 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 structureis 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 usingstructure 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 tofunctions There are two ways: Passing individual structure elements Passing the entire structure
  • 17.
    Passing structure elementsto 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 structureto 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 byreference WAP to input distance in feet and inches. Compute the sum of the two distances.
  • 20.
    Returning structure fromfunctions WAP to input distance in feet and inches. Compute the sum of the two distances and return the sum.
  • 21.
    Reference It isa 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 doesnot 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 directiveBegins 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 canbe 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 alsodefine 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) );