Module 11: Structures in C++
A structure (struct) is a user-defined data type that groups multiple variables of different data types under a single name. Structures are useful for representing real-world entities such as students, employees, books, and products.
For example, instead of storing a student's name, age, and marks in separate variables, you can store them together in a structure.
1. Structure Declaration
A structure is declared using the struct keyword.
Syntax
1struct StructureName 2{ 3 dataType member1; 4 dataType member2; 5};
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6struct Student 7{ 8 string name; 9 int age; 10}; 11 12int main() 13{ 14 Student s1; 15 16 s1.name = "Ankit"; 17 s1.age = 22; 18 19 cout << "Name: " << s1.name << endl; 20 cout << "Age: " << s1.age << endl; 21 22 return 0; 23}
Output
1Name: Ankit 2Age: 22
2. Nested Structure
A nested structure is a structure that contains another structure as one of its members.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6struct Address 7{ 8 string city; 9 string state; 10}; 11 12struct Student 13{ 14 string name; 15 int age; 16 Address address; 17}; 18 19int main() 20{ 21 Student s; 22 23 s.name = "Ankit"; 24 s.age = 22; 25 s.address.city = "Lucknow"; 26 s.address.state = "Uttar Pradesh"; 27 28 cout << s.name << endl; 29 cout << s.address.city << endl; 30 31 return 0; 32}
Output
1Ankit 2Lucknow
3. Array of Structures
An array of structures stores multiple records of the same structure type.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6struct Student 7{ 8 string name; 9 int age; 10}; 11 12int main() 13{ 14 Student students[2] = 15 { 16 {"Ankit", 22}, 17 {"Rahul", 21} 18 }; 19 20 for (int i = 0; i < 2; i++) 21 { 22 cout << students[i].name << " "; 23 cout << students[i].age << endl; 24 } 25 26 return 0; 27}
Output
1Ankit 22 2Rahul 21
4. Pointer to Structure
A pointer can store the address of a structure. The arrow operator (->) is used to access members through a pointer.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6struct Student 7{ 8 string name; 9 int age; 10}; 11 12int main() 13{ 14 Student s = {"Ankit", 22}; 15 16 Student* ptr = &s; 17 18 cout << ptr->name << endl; 19 cout << ptr->age << endl; 20 21 return 0; 22}
Output
1Ankit 222
Note:
ptr->nameis equivalent to(*ptr).name.
Structure vs Class
| Structure | Class |
|---|---|
Members are public by default | Members are private by default |
| Commonly used for storing data | Used for data and behavior (OOP) |
| Supports functions | Supports functions, inheritance, and polymorphism |
Best Practices
- Use meaningful structure names.
- Group related data together.
- Use arrays of structures for multiple records.
- Pass structures by
constreference when possible to avoid unnecessary copying. - Use structure pointers for efficient access to large structures.
Practice Problems
- Create a structure for an Employee.
- Store information for five students using an array of structures.
- Create a nested structure for a Customer and Address.
- Access structure members using a pointer.
- Write a program to calculate the average marks of students stored in a structure array.
Interview Questions
- What is a structure in C++?
- What is the difference between a structure and a class?
- What is a nested structure?
- How do you create an array of structures?
- What is a pointer to a structure?
- What does the
->operator do? - When should you use a structure instead of a class?
Summary
In this module, you learned:
- What structures are and why they are useful.
- How to declare and use structures.
- How nested structures organize related data.
- How arrays of structures store multiple records.
- How pointers access structure members using the
->operator. - The differences between structures and classes.
Next Module: Module 12: Enumerations and Type Aliases — enum, enum class, typedef, using, scoped enumerations, and practical examples.