What is structure in c++? explanation with example.

                Structures in c++

                            structures 

A structure is a collection of multiple data types that can be referenced with single name. In structure multiple data types are combine in a single name. It is also a collection variable of different data type in a single name.  It may contain similar or different data types. 
The data items in a structure are called structure element , member , or field. It is similar to a class in that , both hold a collection of data of  a different data types.
The structure are used to join simple variable together to form complex variable. 

Different between array and structure in c++

    The basic difference between array and structure in c++ program is that the structure consist or grouped of different data type.
array is the combination of a set of variable of same data type .

The structure is used to define new data type, The user can define  a new data type that may contain different types of data. A simple variable can store only one value at a time. But a structure variable can store multiple values at the same time.

    Structure in c++ contain two type of member:
  • Data Member. 
                This member are normal in c++  variable. We can create a member a structure with variable
of different data type in c++.
  • Member Function:
                This member are also normal in c++ function. Along with variable we can also include functions inside a structure declarations.

Declaring a structure 

        A structure is declared by using the keyword struct followed by the structure name. 
The structure members are defined with their type inside the opening and closing braces{  }.
The closing braces  is ended with a semicolon. The declaration tell the compiler about the details of the structure. The compiler does not allocate any memory.
  
            The syntax for declaring a structure is as follows:

                                                           struct struct_ name 
                                                                    {
                                                                            Data-type1 Identifier1:
                                                                            Data-type2 Identifier2:
                                                                                      :
                                                                                      :
                                                                                      :
                                                                                      :
                                                                        }
                struct     ….   It is the keyword that is used to declare a structure.
                struct-name ….    It is name of the structure. The name of a structure is also known  as structure tag.
            
                 Data - type1...….. It indicate the name of first member of the structure.
                 identifier2...…. It indicates the name of first member of the structure.
                 Data-type2...….. It indicate data type of the second member of the structure.
                 Identifier2...…. It indicate the name of the second member of the structure. 

                                        The structure declaration is terminated by semicolon. The structure declaration is also called structure specifier. 
                                  


                    The following example or program declares a structure student with four member variable.

                                    example 
                                                struct student 
                                                        {
                                                            int Rollno; Mark;
                                                             float Average ;
                                                              char Grade;
                                                         }

                            The above structure contains four member variable. The above declaration has created a new data type student. A variable of type student can store four value at one time.



structure program
structure program in c++