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

- 用“芯”探核:龍芯派開發(fā)實(shí)戰(zhàn)
- Windows phone 7.5 application development with F#
- Deep Learning with PyTorch
- 單片機(jī)系統(tǒng)設(shè)計(jì)與開發(fā)教程
- SiFive 經(jīng)典RISC-V FE310微控制器原理與實(shí)踐
- 單片機(jī)開發(fā)與典型工程項(xiàng)目實(shí)例詳解
- 龍芯自主可信計(jì)算及應(yīng)用
- Managing Data and Media in Microsoft Silverlight 4:A mashup of chapters from Packt's bestselling Silverlight books
- “硬”核:硬件產(chǎn)品成功密碼
- 電腦橫機(jī)使用與維修
- USB應(yīng)用分析精粹:從設(shè)備硬件、固件到主機(jī)端程序設(shè)計(jì)
- Hands-On Markov Models with Python
- Hands-On Explainable AI(XAI) with Python
- 3D打印:從全面了解到親手制作(全彩版)
- 51單片機(jī)典型模塊開發(fā)查詢手冊