Structure:
Struct is a special kind of class, and the only difference from class is that the default access permission for a class is private, and the default access permission for a struct is public. In addition, the main reason for the existence of structure is to maintain compatibility with C language.
When do you use structs instead of classes? Used primarily to save data. And there is no type of operation.
It is often common to have data members of structs, so it is easier to use structs
1#include <iostream>2#include <string>3 using namespacestd;4 5 structstudent{6 stringname;7 LongID;8 stringsex;9 Doublescore;Ten }; One voidMain () { A Student S; - //accessing data members of the struct body -S.name ="Zhang San"; theS.sex ="male"; -cout << s.name <<"\ n"<<s.sex <<Endl; - //using pointers to access data members of the struct body -student*SS; +SS = &s; -Ss->score= About; +ss->sex="female"; ASs->name="John Doe"; atcout << Ss->name <<"\ n"<< Ss->sex <<"\ n"<< Ss->score <<Endl; - //Two variables of the same struct type that can be assigned to each other - Student S1; -S1 =s; -cout << S1.name <<"\ n"<< S1.sex <<Endl; -}
Structure of C + +