- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Structures
In C++, structures (or struct) are user-defined data types that allow you to group different types of variables under a single name. They are used to represent a collection of related data in a meaningful way.
Defining a Structure
To define a structure, use the struct keyword followed by the structure name and its members (variables) enclosed in curly braces {}.
Basic Syntax of a Structure
cppstruct StructureName {
data_type1 member1;
data_type2 member2;
// other members
};
StructureName: The name of the structure.member1,member2: The data members (variables) of the structure.
Example of Defining and Using a Structure
cpp#include
using namespace std;
// Defining a structure for a 'Person'
struct Person {
string name;
int age;
float height;
};
int main() {
// Creating a structure variable
Person person1;
// Assigning values to the structure members
person1.name = "John";
person1.age = 30;
person1.height = 5.9;
// Accessing and displaying structure members
cout << "Name: " << person1.name << endl;
cout << "Age: " << person1.age << endl;
cout << "Height: " << person1.height << " feet" << endl;
return 0;
}
Explanation:
- The structure
Personhas three members:name(string),age(int), andheight(float). - In the
main()function, a variableperson1of typePersonis created. - The values for
person1's members are assigned, and they are accessed using the dot (.) operator.
Initializing a Structure
You can initialize a structure in two ways:
Method 1: Using Dot Operator
You can initialize members after defining the structure variable:
cppperson1.name = "Alice";
person1.age = 25;
person1.height = 5.5;
Method 2: Using Initialization List (C++11 and later)
cppPerson person2 = {"Alice", 25, 5.5};
This directly initializes the members in the order they are declared in the structure.
Array of Structures
You can create an array of structures to store multiple records of the same type.
cpp#include
using namespace std;
struct Person {
string name;
int age;
float height;
};
int main() {
// Array of structures
Person people[2] = {
{"John", 30, 5.9},
{"Alice", 25, 5.5}
};
// Accessing array elements
for (int i = 0; i < 2; i++) {
cout << "Name: " << people[i].name << ", Age: " << people[i].age << ", Height: " << people[i].height << " feet" << endl;
}
return 0;
}
Nested Structures
Structures can also contain other structures as members. This is known as nested structures.
cpp#include
using namespace std;
struct Address {
string city;
string state;
int zip;
};
struct Person {
string name;
int age;
Address addr; // Nested structure
};
int main() {
Person person = {"John", 30, {"New York", "NY", 10001}};
// Accessing nested structure members
cout << "Name: " << person.name << endl;
cout << "Age: " << person.age << endl;
cout << "City: " << person.addr.city << endl;
cout << "State: " << person.addr.state << endl;
cout << "ZIP: " << person.addr.zip << endl;
return 0;
}
Pointers to Structures
You can create pointers to structures and access their members using the -> operator.
cpp#include
using namespace std;
struct Person {
string name;
int age;
float height;
};
int main() {
Person person = {"John", 30, 5.9};
// Pointer to structure
Person* ptr = &person;
// Accessing structure members using pointer
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
cout << "Height: " << ptr->height << " feet" << endl;
return 0;
}
Structures vs. Classes
In C++, both struct and class are used to define user-defined data types. The key difference between them is:
- By default, all members of a
structare public. - By default, all members of a
classare private.
Other than this difference, struct and class are almost the same in C++.
Example:
cppstruct Person {
string name; // Public by default
int age;
};
cppclass Person {
public: // Must specify 'public'
string name;
int age;
};
Key Points:
- Structure is a user-defined data type in C++.
- Members of a structure can be of different data types.
- Structures allow grouping related variables under a single name.
- You can create arrays of structures, nested structures, and pointers to structures.
- Structures and classes are similar in C++, except for the default access specifiers.
Structures in C++ are a powerful tool for organizing and managing related data in an efficient and meaningful way.