Pages

Struct in C ++

 

struct in C ++ is a very useful tool provided to group items of different types together. Suppose you want to store or do some process with the data of some university students. There are various data components associated with a university student e.g. name, ID, GPA, etc. All of these entities are of different type i.e. name is a string, ID is an integer, GPA is a floating point number, etc. So you can’t store them in an array. For this purpose, Learn C Online introduces you the concept of struct in C ++ which is provided to group such items of different types.

A struct in C ++  can be defined as a group of a number of components where all of the components may be of same or different types and are accessed by names.
The components of a struct in C ++ are called data members of the struct. Its general syntax in C ++ is


         struct structName{

             dataType1 dataMember1;

             dataType2 dataMember2;

                       .

                       .

                       .

             dataTypeN dataMemberN;
         };
 

struct is a reserved word for using struct in C ++. Although the data members of a struct in C ++ are enclosed in braces, yet they are not considered to form a compound statement. Therefore a semicolon (;) is a part of the syntax and is essential to end the struct statement.

Lets quickly define a struct

               struct Student{

                   string firstName;

                   string lastName;

                   int ID;

                   float GPA;

                   int semester;
               };


Once a data type is defined, we can declare variables of that type. Let’s define two variables Student, student1 and student2.


Student student1;

Student student2;

We can also declare variables when we define the struct.


struct Student{

          string firstName;

         string lastName;

int ID;

   float GPA;

      int semester;

     } student1,student2;



Accessing Members of a struct in C ++

In arrays, we access its components directly by writing the name of the array along with component’s location (index). Similarly, the data members of a struct in C ++ are also accessible but the method of accessing is slightly different. There you accessed components by writing the array name and index separated with square brackets [ ]. 

But for a struct in C ++, you access data members by writing struct variable name and the data member name, separated by a dot ( . ). Its general syntax is

structVariableName.dataMemberName

 In C ++, the dot ( . ) is an operator called member access operator.
In this way, you can read, write, input or output the data members of the struct.


Assignment of a struct in C ++

We can assign the value of one struct  to another struct in C ++ of the same type using assignment operator (=). For example, the statement

                          student1 = student2;

copies the components of student2 into student1.

In fact the above statement is equivalent to the following set of statements:

   student1.firstName = student2.firstName;
   student1.lastName = student2.lastName;
   student1.ID = student2.ID;
   student1.semester = student2.semester;
   student1.GPA = student2.GPA;


Comparison of a struct in C ++

Unlike assignment, you can’t compare variables of a struct in C ++  as a whole by writing a relational operator between two variables of a struct in C ++. Rather, you would have to do this member wise.

For example, if you want to know that whether student1 and student2 refer to the same student, you can’t write

if (student1==student2)

Rather you would have to compare the first names, last names and ID’s of both of them separately (comparison of first names, last names and ID’s would be sufficient) as follows: 
  


if (student1.firstName==student2.firstName && student1.lastName==student2.lastName && student1.ID==student2.ID)


Functions and struct in C ++

We know that arrays are passed to a function only by reference and also that a function can’t return a value of type array. Contrarily,

1) variables of struct in C ++ can be passed as parameters to a function, either by  value or by reference

2) A function can return a value of type struct in C ++




More About struct in C ++


0 comments:

Post a Comment