C++TutorialsUncategorized

C++ Structure

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc.

Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct.

C++ Structure is a collection of different data types. It is similar to the class that holds different types of data.

Syntax of structure

struct structure_name  
{  
     // member declarations.  
}   

In the above declaration, a structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types. Consider the following situation:

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

In the above case, Student is a structure that contains three variables name, id, and age. When the structure is declared, no memory is allocated. When the variable of a structure is created, then the memory is allocated. Let’s understand this scenario.

How to create the instance of Structure?

Structure variable can be defined as:

Student s;

Here, s is a structure variable of type Student. When the structure variable is created, the memory will be allocated. Student structure contains one char variable and two integer variables. Therefore, the memory for one char variable is 1 byte and two ints will be 2*4 = 8. The total memory occupied by the s variable is 9 bytes.

How to access the variable of Structure:

The variable of the structure can be accessed by simply using the instance of the structure followed by the dot (.) operator and then the field of the structure.

For example:

s.id = 4;  

In the above statement, we are accessing the id field of the structure Student by using the dot(.) operator and assigns the value 4 to the id field.

C++ Struct Example

Let’s see a simple example of struct Rectangle which has two data members width and height.

#include <iostream>    
using namespace std;    
 struct Rectangle      
{      
   int width, height;      
      
 };      
int main(void) {    
    struct Rectangle rec;    
    rec.width=8;    
    rec.height=5;    
   cout<<"Area of Rectangle is: "<<(rec.width * rec.height)<<endl;    
 return 0;    
}    

Output:

Area of Rectangle is: 40

C++ Struct Example: Using Constructor and Method

Let’s see another example of struct where we are using the constructor to initialize data and method to calculate the area of rectangle.

#include <iostream>    
using namespace std;    
 struct Rectangle    {      
   int width, height;      
  Rectangle(int w, int h)      
    {      
        width = w;      
        height = h;      
    }      
  void areaOfRectangle() {       
    cout<<"Area of Rectangle is: "<<(width*height); }      
 };      
int main(void) {    
    struct Rectangle rec=Rectangle(4,6);    
    rec.areaOfRectangle();    
   return 0;    
}    

Output:

Area of Rectangle is: 24

Structure v/s Class

StructureClass
If the access specifier is not declared explicitly, then by default access specifier will be public.If access specifier is not declared explicitly, then by default access specifier will be private.
Syntax of Structure:

struct structure_name
{
// body of the structure.
}
Syntax of Class:

class class_name
{
// body of the class.
}
The instance of the structure is known as the “Structure variable”.The instance of the class is known as “Object of the class”.

Advantage of structure in C++

  • Complexity can be reduced using the concepts of divide and conquer.
  • Logical structures ensure a clear flow of control.
  • Increase productivity by allowing multiple programmers to work on different parts of the project independently at the same time.
  • Modules can be reused many times, thus it saves time, reduces complexity, and increase reliability.
  • Easier to update/fix the program by replacing individual modules rather than a larger amount of code.
  • Ability to either eliminate or at least reduce the necessity of employing the GOTO statement.

Disadvantage of structure in C++

  • If the complexity of an IT project goes beyond the limit, it becomes hard to manage.
  • Change of one data structure in a code necessitates changes at many other places. Therefore, the changes become hard to track.
  • The structure is slower because it requires storage space for all the data.
  • You can retrieve any member at a time in structure whereas you can access one member at a time in the union.
  • The structure occupies space for each and every member written in inner parameters while the union occupies space for a member having the highest size written in inner parameters.
  • Structure supports flexible array. Union does not support a flexible array.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button