Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - STRUCTURE


So far, we have seen Data Types that are used to store an integer, a floating point number or a String.


But what if we need a Custom Data Type that could hold an integer value as well a String value.


Let us take a real example of, say a Student.


A Student can have a Name, a Roll Number and Age. Well, there can be other attributes but let's consider only three.

java_Collections

Now, the Student can be considered as a custom data type, that is going to hold Name, a Roll Number and Age of a Student


Now, if you think practically, let us say, there are two students.


i.e. A guy named John whose roll number is 65 and age is 10 and a girl named Rakhi who roll number is 50 and age is 8.

java_Collections

So, student can be considered as a Structure.

java_Collections

And, Student is just a Structure that is just a Blue print and doesn't exist physically.


And if you see the above diagram, student1 and student2 are custom type variables that exist physically (i.e. The John and Rakhi are the Students and they have physical existence).


Now, let us see, how can we implement Structures in C++.


Example :



#include <iostream>
using namespace std;

struct Student {  
	string name; 
	int roll;
	int age;  
};
 
int main() {
  
    Student student1;
   
    student1.name = "John";
    student1.roll = 65;
    student1.age = 10;

    cout << "Details for the first structure" << endl;
    
    cout << student1.name << endl;  
    cout << student1.roll << endl;  
    cout << student1.age << endl; 
   
    Student student2;
   
    student2.name = "Rakhi"; 
    student2.roll = 50;
    student2.age = 8;
  
    cout << "Details for the second  structure" << endl;  
    
    cout << student2.name << endl; 
    cout << student2.roll << endl;  
    cout << student2.age << endl;
}  


Output :



  Details for the first structure
  John
  65
  10
  Details for the second structure
  Rakhi
  50
  8

So, in the above example, we have created a Structure named Student.


struct Student {
	string name;
	int roll;
	int age;
};

The syntax of declaring a Structure is quite simple.

  1. It begins with a type keyword.

  2. Followed by the Structure name'(i.e. student in this case).

  3. Followed by the struct keyword.

  4. And the structure block should be inside braces {}.
    java_Collections

And we have a Structure named student.

java_Collections

Now, just remember, a Structure has no existence of its own. It only comes to effect once a variable is created out of it(i.e. student type variable in this case).


And that is what we have done in the next line. We have created a student type variable out of student Structure.


Student student1;

Creating a student type variable is just like creating a variable of int or string type.

java_Collections

So, the Structure type variable student1 is created.


And student1 variable is for the student named John whose roll is 65 and age is 10.


Now, it's time to assign the above values to the Structure type variable student1. And we do it using the dot operator ..


student1.name = "John";
student1.roll = 65;
student1.age = 10;

And the Structure type variable student1 is created.

java_Collections

Then we create the Structure type variable student2.


Student student2;

And student2 variable is for the girl named Rakhi whose roll is 50 and age is 8.


student2.name = "Rakhi";
student2.roll = 50;
student2.age = 8;
java_Collections


So, the Structure type variables, student1 and student2 of the Structure student has the above values.


Other way to define a Structure type variable


Example :



#include <iostream>
using namespace std;

    struct Student {  
    	string name; 
    	int roll;
    	int age;  
    };
 
int main() {
  
    Student student1{name: "John", roll: 65, age: 10 };
   
    cout << "Details for the first structure" << endl;
    
    cout << student1.name << endl;  
    cout << student1.roll << endl;  
    cout << student1.age << endl; 
   
    Student student2{name: "Rakhi", roll: 50, age: 8 };

    cout << "Details for the second  structure" << endl;  
    
    cout << student2.name << endl; 
    cout << student2.roll << endl;  
    cout << student2.age << endl;
} 


Output :



  Details for the first structure
  John
  65
  10
  Details for the second structure
  Rakhi
  50
  8

Well ! In the above code, we have used a different way to initialise the Structure variable while declaration.


Student student1{name: "John", roll: 65, age: 10 };

All we have done is, used the Structure name on the right hand side followed by braces {}.And all the values to be initialised are kept inside the braces {}.

java_Collections

But if you see a real Student. Other name, age and roll number, they have some behaviours, like they play, they speak e.t.c.


But we didn't find the behaviours implemented in the Structure.


Well ! C++ does it differently. We will be seeing next, how Go uses behaviours/functions for Structures.