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 primarily used to represent data that needs to 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 about structs
in C++
check the Introduction to Programming Course read more
Syntax of structs
The basic syntax for defining a structure in C++ is as follows:
struct StructName {
// Field definitions
DataType Field1;
DataType Field2;
// Method, property definitions, etc.
};
- StructName: This is the unique name given to the structure.
- DataType: Specifies the data type of the fields within the structure.
Basic example
Below 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;
Initialization of structs
structs
can be initialized in various ways, including list initialization and the use of 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 within 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
Date
struct - We define the
Person
struct, which usesDate
Practical examples
Representation of a point in a Cartesian plane
In this example, a structure is defined to represent a point in a Cartesian plane.
#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;
}
Representation of an RGB color
In this example, a structure is defined to represent a color in RGB format.
#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;
}
Representation of a date
In this example, a structure is defined to represent a date.
#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;
}
Representation of a rectangle
In this example, a structure is defined to represent a rectangle and includes 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;
}