Full Transcript

Introduction to Programming Lecture 10 : Structs Askar Khaimuldin Senior-lecturer [email protected] Content Structures in C++ Array of structures Structure pointers Nested structures Structure functio...

Introduction to Programming Lecture 10 : Structs Askar Khaimuldin Senior-lecturer [email protected] Content Structures in C++ Array of structures Structure pointers Nested structures Structure functions Structure Member Alignment Bonus topic (template functions) Structures in C++ A structure is a set of variables that are referenced under the same name It ensures a convenient way to store information as one single unit It holds variables of different data types All data members are public by default All data members of a structure are logically related One of the possible ways to create a user-defined type It is a blueprint from which instances can be created Structures in C++ General form of structure declaration A variable of structure can be declared during structure declaration and afterwards A structure can be both global and local Hint: Every pointer variables inside a structure should point to properly allocated place in memory Every member of structure is accessed using dot (.) Structures in C++ (Example) Array of structures The most common usage of structures is in arrays of structures To declare an array of structures, you must first define a structure and then declare an array variable of that type It is possible to create a dynamic array of structures Hint: If dynamic memory was used for any of the structure members, don`t forget to free that memory Structure pointers Structure pointers are useful in two primary cases: Passing a structure to a function Dynamic allocation It is possible to create a dynamic array of structures All data members are accessed using arrow operator (->) or direct dereferencing Hint: If dynamic memory was used for any of the structure members, don`t forget to free that memory Nested structures A structure can contain an instance of another structure (association) An order of structures` declarations matters Example 1: A student can have a name, his/her age and gender, whereas a group can have group name and a set of students Example 2: A car can have a brand name and model name, whereas a driver can have own name and a car Structure functions A structure can contain member functions (structure functions) All member functions are accessible through instance only Structure function is such a function that logically connected to its structure A member function has direct access to all data members and other member functions of its structure It defines an instance behavior Structures (continued) All data members are initialized to their default values All member functions can be declared without their definitions A separate definition of member function can only be written using Scope Operator (::) Structure Member Alignment Every data type in C/C++ will have alignment requirement Structure may not be in consecutive bytes of memory Byte-alignment (2 or 4 bytes) may cause "holes“ Suppose 2-byte boundary for structure members Use structure with a char and a short char in first byte short on a 2-byte boundary Value in 1-byte hole undefined The task is to decrease the number of holes A type with maximum size is increasing step Bonus topic (template functions) In C++, a template is a special tool used for operating with generic types Templates are expanded at compiler time It eliminates writing the same code for different data types General form: template where T, P, etc. are just fictitious names for incoming types or template (newer version) The datatype for T is set at compile time Example functions: sort(), swap(), max, etc. Literature Deitel P.J. and Deitel H.M. 2017. C++ How to Program, 10th global edition (Chapter 22.2) Herbert Schildt. 2003. The Complete Reference C++, 4th edition (Chapter 7) Bonus topic : Herbert Schildt. 2003. The Complete Reference C++, 4th edition (Chapter 18) Good luck

Use Quizgecko on...
Browser
Browser