Structures in C++
In this tutorial, we will learn about the Structures in C+ +.
we will learn about
- how to declare the structure
- declaration of a structure variable and accessing of structure members
- how to pass structures to function
- structure pointer
- accessing of structure variable by using a structure pointer.
so, we will also see some examples to understand the concept in a better way.
Structures in C++
Structure is a collection of variables referenced under one name and providing a convenient means of keeping related information together. The variables that form the structure are called members of a structure.
Functions and data declared within a structure are PUBLIC.
Structure declaration in C++
for declaration of structure, used keyword struct followed by structure_name,
So, General form of structure declaration is,
struct structure_name{
members;
} structure_variables;
let’s see an example,
struct Addition{ int x,y; //members of structure int sum[10]; }a,b;
so, Here we declared a structure named Addition, structure member is x and y. Structure declaration is terminated by semicolon because a structure declaration is a statement.
NOTE: In C++, once the structure has been declared, you may declare variables of its type by using of its type name, without preceding it with the keyword struct. but in C, a structure’s name does not define a complete type name so you need to struct keyword to declare variables but in C++ its define the complete type name. so, we may or may not use the keyword struct for variables declaration.
so next, we can see structure variables.
Structure variables
Stucture variable is like as an object or instances of a structure. The general form is struct structure_name variable_name;
struct Addition a;// a is structure variable
Addition a; // declaration without struct keyword
because in c++ you may use keyword struct or not.
we may also declare the variables when we declare the structure. for example,
struct Addition{ int x,y; //member of structure int sum[10]; }a,b,c;// a,b and c are structure variables
It is important to understand that each structure variable contains its own copy of the structure’s members.
for example, the value of x stored in a variable(a) may be different from the value in variable (b).
as a result, we can say that the change of x in the variable(a) does not affect the value of x in the variable(b).
so next, we can see accessing of structure members.
Accessing structure members
The general form of accessing the structure members,
structure_variable.structure_members; // use the dot operator to access the structure members.
a.x=100;
assign the value to x where a is structure variable and x is structure members.
cout<<a.x<<endl; //output is 100
for array sum[10],
a.sum[i]=a.x+a.y; //i may be any index
further next, we can see how to assign one variable to other variable.
Structure assignments
The information contained in one structure may be assigned to another structure of same type(same name) by using assignement operator = .
for variable(a),
a.x=100; a.y=200;
for variable(b)
b=x;
we simply assign one variable to other
cout<<b.x; //output will be 100
cout<<b.y; //output will be 200
further next, we can see array of structures.
Array of structures in C++
The general form to declare the array of structures is,
struct structure_name array_name[size];
struct Addition var[100]; //declare the array var of structure named Addition with size 100
So, Let,s see the Code implementation in c++ of the above concept.
//structure in c++ #include<iostream> using namespace std; //declaration of structure Addition struct Addition{ int x,y; //member of structure int sum[10]; }; int main(){ //declare the variables of structure Addition a,b; //access the member of structure for assigning the value a.x=10; a.y=20; cout<<"value of x and y of variable(a) is: "<<endl; cout<<"x is : "<<a.x<<" "<<"and y is: "<<a.y<<endl; b.x=50; b.y=50; cout<<"value of x and y of variable(b) is: "<<endl; cout<<"x is : "<<b.x<<" "<<"and y is: "<<b.y<<endl; cout<<endl; a.sum[0]=a.x+a.y; b.sum[0]=b.x+b.y; cout<<"value of sum[0] for variable(a) is: "<<a.sum[0]<<endl; cout<<"value of sum[0] for variable(b) is: "<<b.sum[0]<<endl; cout<<endl; //assign the value of one structure variable(a) to the structure variable(c) Addition c; c=a; cout<<"value of x and y of variable(c) is: "<<endl; cout<<"x is: "<<c.x<<" "<<"and y is: "<<c.y<<endl; cout<<endl; //array of structure Addition arr[3]; arr[0]=a; arr[1]=b; arr[2]=c; cout<<"value of each variable in array of structure :"<<endl; for(int i=0;i<3;i++){ cout<<"for index "<<i<<endl; cout<<"x is: "<<arr[i].x<<" "<<"and y is: "<<arr[i].y<<endl; } }
Output
value of x and y of variable(a) is: x is : 10 and y is: 20 value of x and y of variable(b) is: x is : 50 and y is: 50 value of sum[0] for variable(a) is: 30 value of sum[0] for variable(b) is: 100 value of x and y of variable(c) is: x is: 10 and y is: 20 value of each variable in array of structure : for index 0 x is: 10 and y is: 20 for index 1 x is: 50 and y is: 50 for index 2 x is: 10 and y is: 20
so next, we can see how to pass the member by value and by reference.
Passing structures to function in C++
firstly, Passing the member of structure by value to function,
func1(a.x); //passses value of x of variable a
func2(a.sum[2]); //passes value of sum[2] of variable a
then next, Passing the member of structure by reference to function,
func1(&a.x); //passes address of x of variable a
func2(&a.sum[2]);//passes address of sum[2] of variable a
and then next, Passing entire structure variable by value
func3(a);//passes the structure variable(a) by value
void func3(struct Addition d){ } //function func3
so next, we can see how to accessing of members by using structure pointers.
Structure pointers
C/C++ allows pointers of structures just as a pointer to any other type.
so, The general form is,
struct structure_name *pointer_name;
for example,
struct Addition *add_pointer; or
Addition *add_pointer;
in c++ you may use struct keyword or not
so we can access the structure member by pointer using -> operator, so
add_pointer=&a ;// place the address of variable(a) to pointer
add_pointer ->x ; //access the value of x stored in variable a
So, Let,s see the Code implementation in c++ of the above concept.
//structure in c++ #include<iostream> using namespace std; //declaration of structure Addition struct Addition{ int x,y; //member of structure int sum[10]; }; void func(int x,int y){ cout<<"value of x and y of variable(a) is: "<<endl; cout<<"x is:"<<" "<<x<<" "<<"and y is:"<<" "<<y<<endl; } void func1(int *x,int *y){ cout<<"value of x and y of variable(a) is: "<<endl; cout<<"x is:"<<" "<<*x<<" "<<"and y is:"<<" "<<*y<<endl; } void func2(Addition a){ cout<<"value of x and y of variable(a) is: "<<endl; cout<<"x is:"<<" "<<a.x<<" "<<"and y is:"<<" "<<a.y<<endl; } int main(){ Addition a; a.x=10; a.y=20; cout<<"passes the a.x and a.y to function by value:"<<endl; func(a.x,a.y);//paases by value cout<<endl; cout<<"passes the a.x and a.y to function by address:"<<endl; func1(&a.x,&a.y);//passes by address cout<<endl; cout<<"passes the entire variable(a)"<<endl; func2(a);//passes the entire structure cout<<endl; //structure pointer Addition *p; p=&a; cout<<"accessing the variable(a) by using structure pointer"<<endl; cout<<"value of x and y of variable(a)"<<endl; cout<<"x is:"<<" "<<p->x<<" "<<"and y is:"<<" "<<p->y<<endl; }
Output
passes the a.x and a.y to function by value: value of x and y of variable(a) is: x is: 10 and y is: 20 passes the a.x and a.y to function by address: value of x and y of variable(a) is: x is: 10 and y is: 20 passes the entire variable(a) value of x and y of variable(a) is: x is: 10 and y is: 20 accessing the variable(a) by using structure pointer value of x and y of variable(a) x is: 10 and y is: 20
you may also learn,
Solution of N-Queen problem in C++ using Backtracking
Leave a Reply