Structures in C++
In this Tutorial we will study and understand the concept of Structures in C++ and:
- Concept and Uses of Structures
- Defining a Structure using struct keyword
- Accessing the member values using . operator
- Array of Structures
- Passing Structure as arguments to a function
Concept and Uses of Structures in C++
A Structure in C++ is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. It is a user defined data type which allows you to combine data items of different kinds.
Imagine you want to store details of a person for e.g. his name, age, height, date of birth and weight. In a typical scenario you will have to create 5 variables to store these values, but what if you have to store details of 20 people. Then you will have to create 100 variables and that will be very tedious and inefficient. In such scenarios we can create a struct datatype named Person and include all the details as member variables of this datatype to create a custom datatype. We can then simply create 20 variables of this new structure datatype or a single array of size 20.
Defining a Structures in C++
To define a structure, you must use the struct keyword. The struct statement defines a new data type, with more than one member, for your program. Following is the syntax for creating a structure in C++
struct [type_name] { member definition; member definition; ... member definition; } [one or more structure variables];
Example of structure Definition
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.
Example of Accessing members of a structure
cout<<book.title; cout<<book.author; cout<<book.subject; cout<<book.id;
Sample Program of Structures in C++
#include <iostream> using namespace std; struct Person { char name[50]; int age; float salary; }; int main() { Person p1; cout << "Enter Full name: "; cin.get(p1.name, 50); cout << "Enter age: "; cin >> p1.age; cout << "Enter salary: "; cin >> p1.salary; cout << "\nDisplaying Information." << endl; cout << "Name: " << p1.name << endl; cout <<"Age: " << p1.age << endl; cout << "Salary: " << p1.salary; return 0; }
Enter age: 33
Enter Salary: 55000
Displaying Information.
Name: Jim Morris
Age: 33
Salary: 55000
Array of Structures
Because structures are types, they can also be used as the type of arrays to construct tables or databases of them:
Sample Program of Array of Structures in C++
#include <iostream> #include <string> using namespace std; struct Customer { int id; string name; }; int main() { // creating array of struct type Customer Customer customerRecords[2]; // initializing values customerRecords[0] = {25, "Bob Jones"}; customerRecords[1] = {26, "Jim Smith"}; /* initialization can also be done like this: customerRecords[0].id=25; customerRecords[1].id=26; customerRecords[0].name="Bob Jones"; customerRecords[1].name="Jim Smith"; */ // printing values for(int i=0;i<2;i++) { cout<<"Customer-"<<(i+1)<<" ID: "<<customerRecords[i].id<<endl; cout<<"Customer-"<<(i+1)<<" Name: "<<customerRecords[i].name<<endl; } return 0; }
Customer-1 Name: Bob Jones
Customer-2 ID: 26
Customer-2 Name: Jim Smith
Passing Structure as arguments to a function
You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. You would access structure variables in the similar way as you have accessed in the above example:
#include <iostream> #include <cstring> using namespace std; void printBook( struct Books book ); struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; // Declare Book1 of type Book struct Books Book2; // Declare Book2 of type Book // book 1 specification strcpy( Book1.title, "C++ Tutorials"); strcpy( Book1.author, "Simple Snippets"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 21231; // book 2 specification strcpy( Book2.title, "PHP programming"); strcpy( Book2.author, "Jon Dough"); strcpy( Book2.subject, "PHP"); Book2.book_id = 6495; // Print Book1 info printBook( Book1 ); // Print Book2 info printBook( Book2 ); return 0; } void printBook( struct Books book ) { cout << "Book title : " << book.title <<endl; cout << "Book author : " << book.author <<endl; cout << "Book subject : " << book.subject <<endl; cout << "Book id : " << book.book_id <<endl; }
Book author : Simple Snippets
Book subject : C++ Programming
Book id : 21231
Book title : PHP programming
Book author : Jon Dough
Book subject : PHP
Book id : 6495