Loading...
C++ Structures

C++ Structures

In this tutorial, you'll learn about structures in C++ programming; what is it, how to define it and use it in your program with the help of examples.


Structure

  • A structure is a user-defined data type in C++.
  • A structure creates a data type that can be used to group items of possibly different types into a single line.
  • Structure are different form array because arrays only hold data of similar data types, on the other hand structure can store data of multiple data types.
  • It is similar to a class in that, both holds a collecion of data of different data types.
  • Each element in the structure is called a member.

For example:

  • You want to store some information about someone: their name, citizenship and age.
  • You can easily create different variables name, citz, age to store these information separately
  • However, you may need to store information about many persons in the future. Now, you'd need to create different variables for each information per person: name1, citz1, age1, name2, citz2, age2.
  • You can easily visualize how big and messy the code would look.
  • Also, since no relation between the variables (information) would exist, it's going to be a daunting task.

A better approach will be to have a collection of all related information under a single name Person, and use it for every person. Now, the code looks much cleaner, readable and efficient a well.

This collection of all related information under a single name Person is a structure.


Syntax

A structure is defined with the struct keyword. A structure is a possible collection of primary data types and other structures.

  • The structure_name holds the name we want to give to our structure.
  • data_type variable is the C++ variables of different data types like int, char, float, etc.
struct structure_name {
// data_type variable 1
// data_type variable 2
// data_type variable 3
...
};

Structure in C++ can contain two types of members:

  • Data Member: These members are normal C++ variables. we can create a structure with variables of different data types in C++.
  • Member Functions: These members are normal C++ functions. Along with variables. we can also include functions inside a structure declararion.

Example:

// Data Members
char name[50];
int age;
int marks;

// Member Functions
void studentDetails(){
cout << "Name: " << name<< endl;
cout <<"Age: " << age << endl;
cout << "Marks: " << marks;
}

In the above structure, the data members are two integer variables and one character variable to store age, marks ans name of any student and member function is studentDetails() which is printing all of the above details of any student.


How to declare a structure in C++ programming?

  • The struct keyword defines a structure type followed by an identifier (name of the structure).
  • The struct members are added within curly braces. These members probably belong to different data types.

For example:

struct Person
{
char name[20];
int age;
int citizenship;
};

In the above example,

  • A structure person is defined which has three members: name, age and citizenship.
  • When a structure is created, no memory is allocated.
  • The structure definition is only the blueprint for the creating of variables. You can imagine it as a datatype.

When you define an integer as below:

int cool;

The int specifies that, variable cool can hold integer element only.

Similarly, structure definition only specifies that, what property a structure variable holds when it is defined.

Note: Remember to end the declaration with a semicolon (;)


How to define a structure variable?

  • Structure members cannot be initialized with declaration.
  • Once you declare a structure person as above.

You can define a structure variable as:

Person city;

Here, a structure variable city is defined which is of type structure Person.

When structure variable is defined, only then the required memory is allocated by the compiler.

Hence, 58 bytes of memory is allocated for structure variable city.


How to access members of a structure?

The Structure members are accessed using dot (.) operator.

Suppose, you want to access age of structure variable p and assign it 20 to it. You can perform this task by using following code below:

p.age = 20;

Example 1: C++ Program to find the area of rectangle

// C++ Program to find the area of rectangle using struct.
#include <iostream>
using namespace std;                                                         
struct Rectangle{
    int width, height;
};
                                                                      
int main(){
    struct Rectangle rec;
    rec.width = 6;
    rec.height = 4;
                                                                      
    cout << "Area of Rectangle is: " << (rec.width * rec.height) << endl;
                                                                      
    return 0;
}

Output

Area of Rectangle is: 24

In the above example Rectangle is declared which has two data members width and height

Inside main() function, a structure variable rec is defined.


Example 2: C++ Program to assign data to a structure variable and display it

// C++ Program to assign data to members of a structure variable and display it.
#include <iostream>
using namespace std;
                                                                      
struct Person{
    char name[30];
    int age;
    int citizenship;
};
                                                                      
int main(){
    Person p1;
    
    cout << "Enter Full name: ";
    cin.get(p1.name, 30);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter citizenship: ";
    cin >> p1.citizenship;
                                                                      
    cout << "\nDisplay Person Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "citizenship: " << p1.citizenship;
                                                                      
    return 0;
}

Output

Enter Full name: Sunny Sirohi
Enter age: 20
Enter citizenship: 1

Display Person Information.
Name: Sunny Sirohi
Age: 20
Citizenship: 1

Here a structure Person is declared which has three members name, age and citizenship.

Inside main() function, a structure variable p1 is defined. Then, the user is asked to enter information and data entered by the user is displayed.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Structure in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Structure & Function.

- Related Topics