struct
struct是一組資料元素一個名字,這些資料元素,作為成員,可以有不同類型和不同長度。C++聲明struct文法如下:
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
簡單樣本如下:
struct product {
int weight;
float price;
} ;
product apple;
product banana, melon;
聲明struct並定義多個執行個體:
struct product {
int weight;
float price;
} apple, banana, melon;
完整struct例子:
// 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和指標
struct movies_t {
string title;
int year;
};
movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;
完整樣本如下:
// 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;
}
內部struct
struct movies_t {
string title;
int year;
};
struct friends_t {
string name;
string email;
movies_t favorite_movie;
} charlie, maria;
friends_t * pfriends = &charlie;
使用如下:
charlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_movie.year
union
union允許以不同的資料類型訪問相同的記憶體,因為他們實際上是在記憶體中的相同位置,它的聲明和struct差不多,但功能完全不同:
union union_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
union聲明的所有元素在記憶體中佔有相同的物理空間,其大小是該聲明的最大元素的大小:
union mytypes_t {
char c;
int i;
float f;
} mytypes;
每一個不同的資料類型。由於所有的元素都指的是記憶體中的同一位置,修改的內容之一,會影響其他元素的內容。
匿名union
struct和正常union
struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
} price;
} book;
struct和匿名union
struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
};
} book;
第一種類型的訪問方式,如下:
book.price.dollars
book.price.yen
第二種類型的訪問方式,如下:
book.dollars
book.yen
enum
定義枚舉類型的基本文法:
enum enumeration_name { value1, value2, value3, . .} object_names;
例如:
enum Week {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
其中SUNDAY = 0,MONDAY = 1……SATURDAY = 6。也就是說,第1個枚舉值代表0,第2個枚舉值代表1,這樣依次遞增1。
也可以在定義時,直接指定某個或某些枚舉值的數值,例如:
enum Week {MONDAY = 1,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY};
其中MONDAY等於1,這樣,TUESDAY就將等於2,直接到SUNDAY等於7。
聲明枚舉變數:
Week today = TUESDAY;
枚舉值,我們就稱為格舉常量,因為它一經定義以後,就不可再改變,以下用法是錯誤的!
TUESDAY = 10; //錯誤!我們不能改變一個枚舉值的數值。