Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Declaring and Initializing Structs | Struct Fundamentals
C++ Structures and Enumerations

bookDeclaring and Initializing Structs

To declare a struct in C++, use the struct keyword followed by its name and curly braces containing member variables. Each member is declared like a normal variable and ends with a semicolon. Don’t forget the semicolon after the closing brace to complete the declaration.

Once defined, you can create variables of that struct type just like any other data type. Struct members can be initialized individually or with brace initialization to assign all values in order.

Note
Note

Brace initialization helps prevent errors by ensuring members are set in the correct order.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> #include <string> struct Person { std::string name; int age; }; int main() { // Create and initialize a Person variable Person alice = {"Alice", 30}; // Print the values std::cout << "Name: " << alice.name << std::endl; std::cout << "Age: " << alice.age << std::endl; }

Once you have created an instance of a struct, you can access its members using the dot operator (.). The dot operator allows you to read or modify the value of a specific member field. For example, if you have a Person variable named alice, you can access the name member with alice.name and the age member with alice.age. To change a field, simply assign a new value to it using the same syntax. This approach makes it easy to update information stored in a struct after it has been initialized.

question mark

Which of the following correctly declares a struct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show an example of how to declare and use a struct in C++?

What is the difference between struct and class in C++?

How do I initialize all members of a struct at once?

Awesome!

Completion rate improved to 8.33

bookDeclaring and Initializing Structs

Свайпніть щоб показати меню

To declare a struct in C++, use the struct keyword followed by its name and curly braces containing member variables. Each member is declared like a normal variable and ends with a semicolon. Don’t forget the semicolon after the closing brace to complete the declaration.

Once defined, you can create variables of that struct type just like any other data type. Struct members can be initialized individually or with brace initialization to assign all values in order.

Note
Note

Brace initialization helps prevent errors by ensuring members are set in the correct order.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> #include <string> struct Person { std::string name; int age; }; int main() { // Create and initialize a Person variable Person alice = {"Alice", 30}; // Print the values std::cout << "Name: " << alice.name << std::endl; std::cout << "Age: " << alice.age << std::endl; }

Once you have created an instance of a struct, you can access its members using the dot operator (.). The dot operator allows you to read or modify the value of a specific member field. For example, if you have a Person variable named alice, you can access the name member with alice.name and the age member with alice.age. To change a field, simply assign a new value to it using the same syntax. This approach makes it easy to update information stored in a struct after it has been initialized.

question mark

Which of the following correctly declares a struct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
some-alt