Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda What is a Struct? | Struct Fundamentals
C++ Structures and Enumerations

bookWhat is a Struct?

Prerequisites
Pré-requisitos
Note
Definition

A struct in C++ is a user-defined type that groups multiple related variables—called members—under one name. It allows you to organize and manage data that naturally belongs together.

Structs exist to make programs clearer and easier to maintain. They are ideal for representing entities like a point in 2D space, a record in a database, or a game character's properties. By combining several related values into a single struct, you can simplify data handling and reduce clutter compared to using separate variables or arrays.

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> // Struct definition struct Point { double x; double y; }; int main() { Point p; p.x = 3.5; p.y = 7.2; std::cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << std::endl; }

While structs and classes in C++ are very similar, there are important differences to understand. The main distinction is their default access specifier: members of a struct are public by default, meaning they can be accessed directly from outside the struct, while members of a class are private by default, restricting direct access.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> #include <string> // Class definition class Person { public: std::string name; int age; }; int main() { Person person; person.name = "Alice"; person.age = 30; std::cout << "Person: " << person.name << ", Age: " << person.age << std::endl; }

Structs are usually used for simple data containers that group related values together. Classes, on the other hand, are meant for more complex types that combine data with behavior.

Note
Note

While both can include functions, constructors, and inheritance, their default access and intended roles in code are what distinguish them.

question mark

Which statement best describes a struct in C++?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 8.33

bookWhat is a Struct?

Deslize para mostrar o menu

Prerequisites
Pré-requisitos
Note
Definition

A struct in C++ is a user-defined type that groups multiple related variables—called members—under one name. It allows you to organize and manage data that naturally belongs together.

Structs exist to make programs clearer and easier to maintain. They are ideal for representing entities like a point in 2D space, a record in a database, or a game character's properties. By combining several related values into a single struct, you can simplify data handling and reduce clutter compared to using separate variables or arrays.

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> // Struct definition struct Point { double x; double y; }; int main() { Point p; p.x = 3.5; p.y = 7.2; std::cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << std::endl; }

While structs and classes in C++ are very similar, there are important differences to understand. The main distinction is their default access specifier: members of a struct are public by default, meaning they can be accessed directly from outside the struct, while members of a class are private by default, restricting direct access.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> #include <string> // Class definition class Person { public: std::string name; int age; }; int main() { Person person; person.name = "Alice"; person.age = 30; std::cout << "Person: " << person.name << ", Age: " << person.age << std::endl; }

Structs are usually used for simple data containers that group related values together. Classes, on the other hand, are meant for more complex types that combine data with behavior.

Note
Note

While both can include functions, constructors, and inheritance, their default access and intended roles in code are what distinguish them.

question mark

Which statement best describes a struct in C++?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1
some-alt