C++ Structure

C++ Structure

The collection of the different types of variables under the one name is called the Structure. These variables are called data members of the structure. The variables are the different types of data and different lengths.


The structure is like class and also user-defined data type but with some differences with class. In the structure, no privacy is available and in the class, privacy is available to the datatype.

What is the syntax of the structure in c++?

Following is the syntax of the structure

struct structname{

member1type    member1name;
member2type    member2name;
member3type    member3name;

};

here,structname is the name of the structure, and members of the structure are declared between the brackets{}.The semicolon(;) add at the end of the structure. This is the most common mistake that happened to miss the semicolon.

Take an example of a student for the understanding of how to use structure.
The student has properties like name,age, university name,Roll Number, and section. During the writing of the structure of the students, all these properties are considered to be members of the structure.

How to declare structure in c++

struct student{

string studentname;
string unveristyname;
char section;
int rollnumber;
int age;
};

The struct is the keyword.
Between the bracket{} member of the struct declare.
Student is the name of the structure.

Here,the structure of the student declare with five members studentname,universityname,section,age,rollnumber,and name

How to define the structure variable

Just like the declaration of the normal variable, structure variables also define in the same way.

Sturcturename variablename;

student Ali;
 
Here the student is the name of the structure and  Ali is the variable name.

Memory allocated to the structure

During the declaration of the structure, no memory allocated to the structure.If the structure variable created then memory allocated to the structure variable.In the student example when a variable of the student creates then memory allocated.

How much memory assigned to the structure in c++

Take an above example of the structure of the student. In this case, add all the memory allocated to the members of the student and make total memory assign to the structure of the student.

How to access the member of the structure

The dot(.)operator is used for the accessing the member of the structure.
Taking the same example of the student.
Ali.universityname=stanford university;
Ali.age=20;
Ali.rollnumber=5678;
 In this way, members of the structure access.


Example:

#include<iostream>
using namespace std;

//define and declare the structure.


struct student{
string studentname;
string unveristyname;
char section;
int rollnumber;
int age;

};

//main function
int main()
{
student Ali;                        //create the variable of structure
Ali.studentname="Ali";
Ali.age=20;
Ali.rollnumber=5678;
Ali.unveristyname="stanford";
Ali.section='A';
cout<<" student name = "<<Ali.studentname<<endl;
cout<<" age= "<<Ali.age<<endl;
cout<<" roll number= "<<Ali.rollnumber<<endl;
cout<<" section= "<<Ali.section<<endl;
cout<<" universityname= "<<Ali.unveristyname<<endl;

return 0;
}

Output:

student name = Ali;
 age= 20
 roll number= 5678
 section= A
 universityname= stanford

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout