Page 1 :
CHAPTER, , Structures and Unions, , , , , , Principles of Object Oriented Programming, >» Unit Outcomes, , | - Use the structure in C++ program for solving the given problem, i > Topics and Sub-Topics, - Structures in C++, , , , , , , , css sss : :, Syllabus Topic : Structures in C++ — The memory space required to store one variable of structure, , eS SS is equal to the memory space required by all the elements, ; ee independently to be stored in memory., Struct Let us see the syntax of a structure declaration and, declaration of a variable of the structure type.
Page 2 :
® Syntax of declaration of a variable of the structure :, , struct structure_name structure_variable_name;, For example, to declare a variable of the above defined, Structure student, we can write it as:, struct student s];, , ~— The variables declared inside the structure are called as the, member elements of the structure. For example name, roll, , number and fees are member elements of the structure, student,, , Structure is normally used when a set of data items, Corresponding to a common thing are to be stored together., For example, when information of a student is to be stored,, we need to store his name, roll number, fees etc. In this case, we can make a structure element and store the corresponding, data of every student in a Separate structure element., , To access a member element of a structure variable we use, the dot operator (““”), also called as the period operator. The, Syntax of accessing a structure element is given below:, , structure_variable_name.varaible_name;, , For example, in the above case to access the fees of the, , student “si”, the period operator will be used as shown, below:, , sl.fees;, , The concept of structure will be clear with an example., , » Program 4.1.1: Write a Program to store and display, the name, roll number and fees of a student using, , Structure. ‘, , , , , , , , , , , , , Structures an, , Explanation :, , The declaration of a Structure for student was already, discussed; the same structure is used here., , Also the declaration of a variable of structure was discussed, the variable s1 is declared here in the same manner., , ~The student’s name is Accepted using gets() function, while, other parameters using “cin”. The parameters are access!, Using the petiod operator as just discussed. Also to displ, , the variables of the structure elements we have used, , , , Write a pragram to store and displ!, Tuns Scored and wickets taken of a cricket
Page 3 :
NG C++(MSBTE), , <el.name<<end], elnins< <end]<<"Wickets, , Tuns scored and wickets, , m to the previous one, only the data types are, , 1 Write a program to store and display, hes played and goals scored of a, fusing structure., , 3, , , , , , Structures and Unions, ee, , char name{20}:, int matches, goals;, a, void main (), <, clrser();, struct hockey ely, , cout<<"Enter the hockey player's name, matches played, and goals scored:";, , #ets(cl.name);, , cin> >cl.matches> >cl.goals;, , cout<<"The hockey player's details are as, follows:"<<endl< <"Name:''< <cl.name< <endl, <<''Matches played:"<<el.matches< <endl<<"Goals, seoréd;"<<c] goals <<endl;, , getch();, , }, , Output:, , Enter the hockey player's name, matches played and goals, scored:V, Raghunath, , 67, , 41, , The hockey player's details are as follows:, , Name:V. Raghunath, , Matches played:67, , Goals scored:41, , >» Program 4.1.4: Write a program to define a structure, , ‘Tender’ having data members tender-no., cost and, company-name. Accept and display this data for two, , variable of this structure. S-14, 4 Marks, , #include<iostream.h>, #include<conio.h>, #include<stdio.h>, , struet Tender, , , , ww TechKnowledge, , Publications
Page 4 :
2 . =, Object Oriented Progré : a | ; Write a program to store., 3 : s in three subjects ang roll y iia, , using structure. Display the om, “Ut y, , namie, roll number, , 4.2 __ Arrays of Structure, , An array of structure can be, declaring array of any other data type., For example, an array of the structure ;, as shown below:, , struct student s[100];, This array can now store informatioi, , Array of structure must be used when m, structure are required.” ee: to store, students or information of 11 cricket play, , Let us see some program examples for array
Page 5 :
‘accessed using the period operator ie, , , , Programming using C++ (MSBTE), , ts of an array of structure are accessed in a similar, the elements of any other array. The mem, , , , , ber, , ., , , , , , = Structures and Unions, , ram to store the name,, n of ‘n’ cricketers using, , >» Program 4.2.2: Write a prog!, runs scored and wickets take!, , , , , , , , , , ater, et cricketer ¢[100];, , , , aba ee, coui< <"Enter the number of ericke, , , , , , ein>>n:, fori=O;i<=n-Lit+), {, , and wickets taken:";, , gets(c[i].name);, , 4 TechKnowledge, , Publications