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

bookWhat is a Struct?

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you give examples of when to use a struct versus a class?

What are some other differences between structs and classes in C++?

Can you explain how access specifiers work in more detail?

Awesome!

Completion rate improved to 8.33

bookWhat is a Struct?

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1
some-alt