Home >>C++ Tutorial >C++ structs

C++ structs

Structs in C++

In order to create the instance of a class some blueprints are used that are known as  the structs in C++.  These structs are generally used for the light weighted objects such as the color, point, rectangle and many more.

The structs in C++ are of value type instead of being the reference type that is known to be just the opposite of the class. Being of the value type is useful when there is no intention of modifying the data that the user has entered just after the struct is created.

In simple words if you have to understand the C++ structure then it is basically a collection of different data types and somehow similar to the class that is known for holding the different data types.

The Syntax Of Structure

struct structure_name  
{  
     // member declarations.  
}   

Please note that in the above mentioned declaration, a structure has been declared that is being preceded by the struct keyword and that is followed by a structure name that is also known as the identifier. The member variables of various types can be declared by the programmer in the curly braces.

In order to understand it better, let’s take this following situation in to consideration:

struct Employee 
{  
    char name[20];  
     int id;  
     int age;  
}       

In the above mentioned situation, employee is a structure that consists of three variables that are name, age and id. No memory is getting allocated when the structure is declared.  The memory is allocated only when the variable of a structure is created.  Now, let’s get to this situation:

How to create the instance of Structure?

The variable of the structure can be defined as in following way:

Employee e;

In this case, e is a structure variable of type student. The memory will be allocated just after the structure variable is created. In the above mentioned case, the employee variable consists of two integer variable and one char variable hence, the calculation of the memory will be 1 byte for one char value and  2*4=8 for two integer variable. So the resultant memory occupancy of the e variable will be 9 bytes.

How to access the variable of Structure:

In order to access the variable of the structure the programmer have to simply use the instance of the structure and should follow that with the dot (.) operator and in the last the field of structure.

For instance:

e.id = 4;

In the above mentioned statement, the id field of the structure employee is getting accessed by using the dot (.) operator and the value 4 is assigned to the respected field.

Here are the examples of the C++ Struct that will give you a complete understanding of the topic:
#include <iostream>  
using namespace std;    
struct Rect      
{      
   int width, height;      
      
 };      
int main(void) 
    {    
    struct Rect rec;    
    rec.width=10;    
    rec.height=4;    
   cout<<"The Area of Rectangle = "<<(rec.width * rec.height)<<endl;    
 return 0;    
}    
Output :The Area of Rectangle = 40

Structure Vs Class

Structure Class
Access specifier will be public by default in case the access specifier has not been declared explicitly. Access specifier will be Private by default in case the access specifier has not been declared explicitly.
Here is the syntax of Structure: struct structure_name { // body of the structure. } Here is the syntax of Class: class class_name { // body of the class. }
"Structure variable" is known as the instance of the structure. "Object of the class" is known as the instance of the class.