STRUCTURE IN C++ 1
A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your current program using which items of different data types can be can be processed as a single unit. Difference between an Array and a Structure The Array contains elements of a single data type (homogeneous elements) where as a Structure can contain elements of different data types (heterogeneous members). The elements of the Array are contiguous in memory whereas the elements of a Structure may not be contiguous. 2
Syntax to define a Structure: For Example: struct structurename datatype membername1; datatype membername2;... ; struct Student int rollno; char name[20]; float marks; ; Definition of a structure ( i. e. a new user defined data type ) does not allocate any memory. Memory is allocated only when we create variables of this new data type. ( For Eg. Bytes consumed by each variable of Student type=26 Bytes) 3
Student S1, S2 ; // variables of Student type S1.rollno = 101; strcpy ( S1.name, aman ); S1.marks = 95; cout << Enter details for Student 2 ; cout << Enter Student Rollno ; cin >> S2.rollno ; cout << Enter Student Name ; gets ( S2.name ) ; cout << Enter Student Marks ; cin >> S2.marks ; 101 aman 95 S1 cout << S1.rollno << endl; cout << S1.name << endl; cout << S1.marks << endl; 4
Structure variables can also be declared by clubbing Structure definition and Variable declaration together struct Student int rollno; char name[20]; float marks; S1, S2 ; //global variables of Student type The Structure name/tag can be omitted from the above definition, but the drawback is no more variables of its type can be created later on. struct int rollno; char name[20]; float marks; S3, S4 ; // only variables of this structure type 5
Initialization of structure members can be done at the time of creation of structure variable struct date int day; int month; int year; ; date d1= 31,12,2016; date d2= 15,11,2017; // initializing d1 // initializing d2 31 12 2016 d1 15 11 2017 d2 6
We can assign a variable of a structure as a whole to another variable of the same structure type where in each member of the structure variable receives the corresponding members value from the assigned variable struct X int a; float b; S1; struct Y int c; float d; S2, S3; 2 4 S1 3 6 S2 3 6 S3 cin>>s1.a>>s1.b; cin>>s2.c>>s2.d; S1=S2; S3=S2; // Error since different structure types // valid member by member assignment 7
When any member of the structure is a structure variable itself, then it is a nested structure. struct date 101 int day; int month; ravi int year; 55.5 ; struct Student int rollno; char name[20]; 10 float marks; date dob; // variable of date data type ; 08 2001 Student stu1 ; cin >> stu1.rollno ; gets ( stu1.name); stu2 cin >> stu1.marks; cin >> stu1.dob.day >> stu1.dob.month >> stu1.dob.year ; Student stu2= 101, ravi, 55.5, 10, 08, 2001 ; // initialization of stu2 While Initialization in nested structures, internal braces are optional and are ignored by compiler. 8
struct Student int rollno; char name[20]; int marks[5]; ; Student stu ; cin >> stu.rollno ; gets ( stu.name); for( int i=0; i<5 i++) cin >> stu.marks[i]; 101 Sumit 90 95 100 85 92 S.marks[0] S.marks[1] S.marks[2] S.marks[3] S.marks[4] stu 9
Structure can be passed to functions by value or by reference. When passed by value the called function creates its own variables of the specified structure type and assigns the actual variables values member by member to it. When passed by reference the formal structure variables are just an alias name of the actual variables. /* Program to add two times given in hours and minutes using functions */ struct time int hh; int mm; ; time Add(time,time); //fn prototype time t1, t2, t3; cout<<"enter First Time " <<endl; cout<<" Hours "; cin>>t1.hh; cout<<" Minutes "; cin>>t1.mm; cout<<"enter Second Time "<<endl; cout<<" Hours " ; cin>>t2.hh; cout<<" Minutes "; cin>>t2.mm; t3 = Add( t1, t2 ); //fn call cout<<"total Time is " <<t3.hh<<" Hours : " <<t3.mm<<" Minutes"<<endl; time Add( time ft, time st) time tt; tt.hh = ft.hh + st.hh + ( ft.mm + st.mm )/ 60; tt.mm = ( ft.mm + st.mm ) % 60; return tt; 10
#include<iostream.h> struct distance int feet; int inch; ; struct volume distance length; distance breadth; distance height; ; float cubicfeet( volume v) float l= v.length.feet + (float)v.length.inch/12; float b= v.breadth.feet + (float)v.breadth.inch/12; float h= v.height.feet + (float)v.height.inch/12; return l*b*h; volume V; cout<<"enter Length " <<endl; cout<<" Feet "; cin>>v.length.feet; cout<<" Inches "; cin>>v.length.inch; cout<<"enter Breadth " <<endl; cout<<" Feet "; cin>>v.breadth.feet; cout<<" Inches "; cin>>v.breadth.inch; cout<<"enter Height " <<endl; cout<<" Feet "; cin>>v.height.feet; cout<<" Inches "; cin>>v.height.inch; float ans= cubicfeet(v); cout<< "volume is "<<ans<< " cubic feet"; 11
/* Program to display the Average marks of each student */ #define MAX 3 struct student long rollno; char name[20]; float marks[5]; ; void Average(student[]); //prototype student S[MAX]; for ( int i=0 ; i < MAX ; i++ ) cout<<"student "<<(i+1)<<endl; cout<<"enter rollno :"; cin>>s[i].rollno; cout<<"enter name :"; gets(s[i].name); for(int j=0; j<5 ;j++) cout<<"enter marks" <<(j+1) <<" :"; cin>>s[i].marks[j]; Average(S); void Average( student st[]) for ( int i=0 ; i < MAX ; i++ ) cout<<"--------------------"<<endl; cout<<"student "<<(i+1)<<endl; cout<<"rollno :"<<st[i].rollno<<endl; cout<<"name :"<<st[i].name<<endl; int sum=0; for(int j=0; j<5 ;j++) sum+=st[i].marks[j]; cout<<"average Marks :"<<(float)sum/5<<endl; 12