- C++ Game Development By Example
- Siddharth Shekar
- 222字
- 2021-06-24 14:26:13
Structs
Structures or structs are used to group data together. A struct can have different data elements in it, called members, integers, floats, chars, and so on. You can create many objects of a similar struct and store values in the struct for data management.
The syntax of a struct is shown as follows:
struct name{ type1 name1; type2 name2; . . } ;
An object of a struct can be created as follows:
struct_name object_name;
An object is an instance of a struct to which we can assign properties to the data types we created when creating the struct. An example of this is as follows.
In a situation in which you want to maintain a database of students' ages and the height of a section, your struct definition will look like this:
struct student { int age; float height; };
Now you can create an array of objects and store the values for each student:
int main() { student section[3]; section[0].age = 17; section[0].height = 39.45f; section[1].age = 12; section[1].height = 29.45f; section[2].age = 8; section[2].height = 13.45f; for (int i = 0; i < 3; i++) { std::cout << "student " << i << " age: " << section[i].age << " height: " << section[i].height << std::endl; } _getch(); return 0; }
Here is the output of this:

- 新型電腦主板關(guān)鍵電路維修圖冊(cè)
- Linux KVM虛擬化架構(gòu)實(shí)戰(zhàn)指南
- 基于Proteus和Keil的C51程序設(shè)計(jì)項(xiàng)目教程(第2版):理論、仿真、實(shí)踐相融合
- 數(shù)字道路技術(shù)架構(gòu)與建設(shè)指南
- 現(xiàn)代辦公設(shè)備使用與維護(hù)
- 筆記本電腦維修不是事兒(第2版)
- The Deep Learning with Keras Workshop
- 微服務(wù)分布式架構(gòu)基礎(chǔ)與實(shí)戰(zhàn):基于Spring Boot + Spring Cloud
- 電腦高級(jí)維修及故障排除實(shí)戰(zhàn)
- Internet of Things Projects with ESP32
- 基于Proteus仿真的51單片機(jī)應(yīng)用
- FL Studio Cookbook
- 微服務(wù)實(shí)戰(zhàn)
- UML精粹:標(biāo)準(zhǔn)對(duì)象建模語(yǔ)言簡(jiǎn)明指南(第3版)
- Learning Less.js