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

bookWhat is a Struct?

Prerequisites
Prerequisites
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 8.33

bookWhat is a Struct?

Swipe to show menu

Prerequisites
Prerequisites
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1
some-alt