Struct
StructYesIGroupData ElementA name,These data elements,As a member,YesDifferent Types and differencesLength. C ++ statement structSyntax::
Struct structure_name {
Member_type1 member_name1;
Member_type2 member_name2;
Member_type3 member_name3;
.
.
} Object_names;
A simple example is as follows:
StructProduct {
IntWeight;
FloatPrice;
};
Product apple;
Product banana, melon;
Declare struct and define multiple instances:
StructProduct {
IntWeight;
FloatPrice;
} Apple, banana, melon;
Complete struct example:
// Example about structures
# Include <iostream>
# Include < String >
# Include <sstream>
Using Namespace STD;
Struct Movies_t {
String Title;
Int Year;
} Mine, yours;
Void Printmovie (movies_t movie );
Int Main ()
{
String Mystr;
Mine. Title = " 2001 A Space Odyssey " ;
Mine. Year = 1968 ;
Cout <" Enter title: " ;
Getline (CIN, yours. Title );
Cout < " Enter year: " ;
Getline (CIN, mystr );
Stringstream (mystr)> yours. Year;
Cout < " My favorite movie is: \ n " ;
Printmovie (mine );
Cout <" And yours is: \ n " ;
Printmovie (yours );
Return 0 ;
}
Void Printmovie (movies_t movie)
{
Cout <movie. title;
Cout < " ( " <Movie. year < " ) \ N " ;
}
Struct and pointer
StructMovies_t {
StringTitle;
IntYear;
};
Movies_t amovie;
Movies_t * pmovie;
Pmovie = & amovie;
Complete example:
// Pointers to structures
# Include <iostream>
# Include < String >
# Include <sstream>
Using Namespace STD;
Struct Movies_t {
String Title;
Int Year;
};
Int Main ()
{
String Mystr;
Movies_t amovie;
Movies_t * pmovie;
Pmovie = & amovie;
Cout <" Enter title: " ;
Getline (CIN, pmovie-> title );
Cout < " Enter year: " ;
Getline (CIN, mystr );
(Stringstream) mystr> pmovie-> year;
Cout < " \ Nyou have entered: \ n " ;
Cout <pmovie-> title;
Cout <" ( " <Pmovie-> year < " ) \ N " ;
Return 0 ;
}
Internal struct
StructMovies_t {
StringTitle;
IntYear;
};
StructFriends_t {
StringName;
StringEmail;
Movies_t favorite_movie;
} Charlie, Maria;
Friends_t * pfriends = & Charlie;
Use:
Charlie. Name
Maria. favorite_movie.title
Charlie. favorite_movie.year
Pfriends-> favorite_movie.year
Union
UnionAllowDifferent Data TypesSame memory,Because theyActuallyIn MemoryOfSameLocation, which is similar to struct, but has different functions:
Union union_name {
Member_type1 member_name1;
Member_type2 member_name2;
Member_type3 member_name3;
.
.
} Object_names;
Union statementOfAllElementIn MemoryMediumPossessionSame physical space,Its size isStatementOfMaxElement size:
Union mytypes_t {
CharC;
IntI;
FloatF;
} Mytypes;
EachOneDifferent Data Types.BecauseAll elements areIndicates thatSame in memoryLocation,ModifyOne,Will affectOther elements.
Anonymous Union
Struct and normal Union
Struct{
CharTitle [50];
CharAuthor [50];
Union {
FloatDollars;
IntYen;
} Price;
} Book;
Struct and anonymous Union
Struct{
CharTitle [50];
CharAuthor [50];
Union {
FloatDollars;
IntYen;
};
} Book;
The first type of access method is as follows:
Book. Price. dollars
Book. Price. Yen
The second type of access method is as follows:
Book. dollars
Book. Yen
Enum
Basic syntax for defining enumeration types:
Enum enumeration_name {value1, value2, value3,...} object_names;
For example:
EnumWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Sunday = 0, Monday = 1 ...... Saturday = 6. That is to say, 1st enumerated values represent 0, and 2nd enumerated values represent 1, which increases by 1 in turn.
You can also specify the values of one or more enumerated values when defining them. For example:
EnumWeek {Monday =1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
Monday is equal to 1, so that Tuesday is equal to 2 and Sunday is equal to 7.
Declare enumerated variables:
Week today = Tuesday;
The enumerated value is called a lattice constant, because it cannot be changed once defined. The following usage is incorrect!
Tuesday =10;//Error! We cannot change the value of an enumerated value.