In C++, a struct is a data structure that allows grouping several variables under a single name.
structs are particularly useful when working with simple data types that have a predictable size and are mainly used to represent data that should be grouped.
In C++ a struct is practically the same as a class
The only difference is the default visibility, which is public in structs and private in classes.
If you want to learn more, check out the Introduction to Programming Course
Structs Syntax
The basic syntax for defining a structure in C++ is as follows:
struct StructName {
// Field definitions
DataType Field1;
DataType Field2;
// Method, property definitions, etc.
};
- StructureName: The unique name given to the structure.
- DataType: Specifies the data type of the fields within the structure.
Basic Example
The following is a basic example of how to define and use a structure in C++:
#include <iostream>
struct Point {
int X;
int Y;
Point(int x, int y) : X(x), Y(y) {}
void PrintCoordinates() const {
std::cout << "Coordinates: (" << X << ", " << Y << ")" << std::endl;
}
};
int main() {
Point point(3, 5);
point.PrintCoordinates(); // Prints "Coordinates: (3, 5)"
return 0;
}
Using Structs
Declaration and Initialization
Structures are declared and initialized similarly to variables of other data types.
Point point;
point.X = 3;
point.Y = 5;
// Initialization using the constructor
Point anotherPoint(7, 8);
Accessing Fields
The fields of a structure are accessed using the dot notation (.).
int x = point.X;
int y = point.Y;
Initializing Structs
structs can be initialized in several ways, including list initialization and using constructors.
List Initialization
Person p = {"Luis", 30, 1.75};
Constructors in Structs
Although more common in classes, structs can also have constructors.
struct Person {
std::string name;
int age;
float height;
// Constructor
Person(std::string n, int e, float a) : name(n), age(e), height(a) {}
};
Nesting Structs
structs can be nested inside other structs, allowing the creation of more complex data structures.
struct Date {
int day;
int month;
int year;
};
struct Person {
std::string name;
Date birthDate;
};
In the example,
- We define the
Datestruct - We define the
Personstruct, which usesDate
Practical Examples
Representing a Point in a Cartesian Plane
In this example, a structure representing a point in a Cartesian plane is defined.
#include <iostream>
struct Point {
int X; // X coordinate of the point
int Y; // Y coordinate of the point
Point(int x, int y) : X(x), Y(y) {}
};
int main() {
Point point(3, 4);
std::cout << "Point: (" << point.X << ", " << point.Y << ")" << std::endl; // Prints "Point: (3, 4)"
return 0;
}
Representing an RGB Color
In this example, a structure representing a color in RGB format is defined.
#include <iostream>
struct ColorRGB {
unsigned char Red; // Red component of the color
unsigned char Green; // Green component of the color
unsigned char Blue; // Blue component of the color
ColorRGB(unsigned char red, unsigned char green, unsigned char blue)
: Red(red), Green(green), Blue(blue) {}
};
int main() {
ColorRGB color(255, 0, 0); // Red color
std::cout << "RGB Color: (" << (int)color.Red << ", " << (int)color.Green << ", " << (int)color.Blue << ")" << std::endl; // Prints "RGB Color: (255, 0, 0)"
return 0;
}
Representing a Date
In this example, a structure representing a date is defined.
#include <iostream>
struct Date {
int Day; // Day of the month
int Month; // Month of the year
int Year; // Year
Date(int day, int month, int year) : Day(day), Month(month), Year(year) {}
};
int main() {
Date date(5, 6, 2023);
std::cout << "Date: " << date.Day << "/" << date.Month << "/" << date.Year << std::endl; // Prints "Date: 5/6/2023"
return 0;
}
Representing a Rectangle
In this example, a structure representing a rectangle is defined, including a method to calculate its area.
#include <iostream>
struct Rectangle {
int Width; // Width of the rectangle
int Height; // Height of the rectangle
Rectangle(int width, int height) : Width(width), Height(height) {}
int CalculateArea() const {
return Width * Height;
}
};
int main() {
Rectangle rectangle(5, 10);
int area = rectangle.CalculateArea();
std::cout << "Rectangle area: " << area << std::endl; // Prints "Rectangle area: 50"
return 0;
}
