AppDividend
Latest Code Tutorials

C++ Structure Example | Structure in C++ Tutorial

0

C++ Structure Example | Structure in C++ Tutorial is today’s topic. Suppose, you are given a task to store the academic information of a student and make a program for that. One thing you can do is, you can take different data types according to the requirements, as an array to store the marks, a string to take a name, int to take roll number, etc. In this case, there can be a problem, like accessing data. But what if you are given a method where you can store them all together? 

C++ Structure Example

The structure is a collection of different data types under the same name. Those data elements mean roll no, marks are named as members of the structure.

You can think of a box, where inside it all your favorite foods are stored. The structure is also the same, inside it all our requirements are stored.

#Structure declaration

You can declare the structure using the struct keyword.

struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
...
} object_names;

Here, structure_name is the name of the structure you will provide. The object_name is to identify the structure inside main(). And the member_types are the data members we will use, like roll number, name, etc.

struct Student
{
   char name[10];
   int roll_number, total_marks;
   float average;
} stn;

Remember, the structure declaration is only a declaration; no memory is allocated when we are declaring a structure. This is like a blueprint, means how the structure will look like.

Always remember, never forget to give semicolon (;) at the end of structure declaration. 

#Defining a structure variable (object)

Structure variable is a variable which is always written inside the main() to using which we can access members of the structure. Structure variable and Object_name are same. You can declare it when declaring the structure or you can declare it inside main(). But structure can be declared inside the main() or outside the main() also. 

When a structure variable is defined, then only the memory is allocated. We can define a structure variable of the structure Student inside main like the following.

student stn;

For the above example, if we consider the size of a float is 4 byte, the size of int is 4 byte, and the size of char is 1byte. Then the total size of the structure variable (stn) will be the (1*10)+(4*2)+(4*1)= 22 bytes.

You can declare multiple structure variable to a structure. Like Class and Objects, each variable will also different data for the same structure member.

For example, we can write in main()

student class1, class2;

#Access members of a Structure

Suppose you want to access the data of roll_no of structure Student ( defined above). To access any member of a structure, you have to use dot( . ) operator. See the following syntax.

structure_variable.structure_member;

For example, to access the roll number of above defined Student Structure, you have to write the following.

stn.roll_number;

For example, to access the roll number of above defined Student Structure, you have to write the following code.

stn.roll_number;

Also, for multiple structure variable, we can write like the following.

class1.roll_number=1;
class2.roll_number=1;

Refer the below code for better understanding.

See the following program.

#include <iostream>
using namespace std;
struct student
{
    string name;
    int roll, marks;
    float avg;
};
int main()
{
    student cl1, cl2; //declaring structure object

    //Assigning values to structure members using object
    cl1.name = "Debasis";
    cl1.roll = 15;
    cl1.marks = 541;
    cl1.avg = 83;

    cl2.name = "Ayush";
    cl2.roll = 12;
    cl2.marks = 654;
    cl2.avg = 92;
    //Printing the values of structure members
    cout << "\nName of the student of Class1 is: " << cl1.name;
    cout << "\nRoll: " << cl1.roll;
    cout << "\nMarks: " << cl1.marks << " Average: " << cl1.avg;

    cout << "\n\nDetails of the student of class2:\n";
    cout << "Name: " << cl2.name << "\nRoll: " << cl2.roll << "\nMarks: " << cl2.marks;
    cout << "\nAverage: " << cl2.avg << "\n";
    return 0;
}

See the following output.

 

C++ Structure Example

#Structure and Pointer

Like other types, the structure also can be pointed to by its type of pointer.

See the following example.

struct Fruit
{
	string name;
	string colour;
};

And in main() if we write the following.

Fruit fr;
Fruit* pfr;

Here, fr is an object of structure Fruit but pfr is a pointer pointing to the object of structure Fruit. So, after that, the following code should be written.

pfr=&fr;

By this, the address of fr will be assigned to pfr. Refer the below coding example for better understanding.

#include<iostream>
using namespace std;
struct fruit
{
    string name,colour;
    int weight;
};
int main()
{
    //Using multiple
    fruit f1,f2;
    fruit* pft;
    fruit* pf2;
    pft=&f1;
    pf2=&f2;
    string s;
    pft->name="Banana";
    pft->colour="Yellow";
    pft->weight=25;
    pf2->name="Apple";
    pf2->colour="Red";
    pf2->weight=50;
    //Member can be accessed by -> also
    cout<<"\nName of first fruit is: "<<pft->name;
    cout<<"\nColour of banana is: "<<pft->colour;
    cout<<"\nColour of Apple is: "<<pf2->colour;
    return 0;
}

See the following output.

 

Structure and Pointer

#Structure and Function

We can assign functions to the members of a structure and then can use them in Function. See the following example.

#include<iostream>
using namespace std;
struct voter
{
    string name,add;
    int id;
};
//declaring function
//structure object is the parameter
void display(struct voter v)
{
    cout<<"\nVoter ID: "<<v.id;
    cout<<"\nName: "<<v.name;
    cout<<"\nAddress: "<<v.add<<"\n";
}
int main()
{
    voter v1;
    v1.id=1452145;
    v1.name="Debasis Jana";
    v1.add="Kolkata, West Bengal";
    //calling the function using structure object
    display(v1);
    return 0;
}

See the following output.

 

structure_f

#Nested Structure

A nested structure means, structure inside a structure, same like nested if-else statements. So, here also we can declare a structure inside a structure and then can use them.

struct structure1
{
	Member1;
	member2;
};

struct structure2
{
    Member1;
    Member2;
    struct structure1 obj;	
};

The above syntax tells that, structure1 is nested to structure2.

#Accessing members of nested structure

In main() we have first to declare the object of structure 2, and the object of structure1 is already proclaimed in structure2.

main()
{
	structure2 st2;
	//To access member of structure2, just simply write
	st2.Member1;
	
	//To access member of structure1, we have to write
	st2.obj.Member2;
}

You will understand the concept properly by the following example below.

#include<iostream>
using namespace std;
struct salary
{
    int DA,TA,Total;
};
struct Employee
{
    string name,add;
    //declaring structure salary as a nested structure
    struct salary s;
};
void display(struct Employee E1)
{
    cout<<"\nName: "<<E1.name;
    cout<<"\nAddress: "<<E1.add;
    cout<<"\nDA+TA: "<<((E1.s.DA)+(E1.s.TA));
    cout<<"\nTotal Salary: "<<((E1.s.DA)+(E1.s.TA)+(E1.s.Total))<<"\n";

}
int main()
{
    Employee E;
    //adding values to the member of Employee structure
    E.name="Ankit";
    E.add="Pune";
    //Adding values to the member of salary structure
    E.s.DA=1400;
    E.s.TA=2800;
    E.s.Total=36000;
    //calling display function
    display(E);

}

See the following output.

 

Accessing members of nested structure

So, here we end the concept of C++ Structure Example | Structure in C++ Tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.