定義結構

想要建立只有資料的物件時,使用 struct;其他狀況一律使用 class。

結構是變數的集合,變數可以不是不同型態,例如有的是int,有的是float,結構中每一個變數都是該結構的成員。

定義結構有以下三種方式

方式1

宣告結構

關鍵字struct宣告,緊號於struct後的是結構名稱,變數的宣告都在大括號{}內,大括號{}結尾一定要加上分號;

struct 結構名 {
  變數宣告
};
1
2
3
4
5
6
struct Student {
  //學生姓名
  char* name;
  //學號
  int id;
};

結構放置位置

放在main()上面,或者放在標頭檔。

定義結構變數

結構可想像成一種新的資料型態,int也是一種資料型態,定義結構變數會在記憶體配置空間。

結構名 變數名;
1
2
3
4
int main() {
  Student student;
  return 0;
}

存取結構成員

存取方式有二種

點運算子

使用點運算子(dot operator)指派或修改結構成員的值。

1
2
3
  Student student;
  student.name = "Mary";
  student.id = 1;

cout讀取結構成員。

1
2
  cout << student.name << endl;
  cout << student.id << endl;

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
struct Student {
  //學生姓名
  char* name;
  //學號
  int id;
};
int main() {
  Student student;
  student.name = "Mary";
  student.id = 1;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}
Mary
1

大括號

使用大括號修改結構。

結構名 = {值1,值2,值3, ...};
1
student = {"Bill",2};

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
struct Student {
  //學生姓名
  char* name;
  //學號
  int id;
};
int main() {
  Student student;
  student.name = "Mary";
  student.id = 1;
  cout << "Before:" << endl;
  cout << student.name << endl;
  cout << student.id << endl;
  student = {"Bill",2};
  cout << "After:" << endl;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}

方式2

同時宣告結構與定義結構變數

在大括號的結尾輸入結構變數,在main()函數不用定義結構變數,直接使用結構變數。

struct 結構名{
	變數宣告
}結構變數;
1
2
3
4
5
6
struct Student {
  //學生姓名
  char* name;
  //學號
  int id;
}student;

存取結構成員

在main()函數不用定義結構變數,直接使用結構變數。

1
2
3
4
5
6
7
int main() {
  student.name = "Mary";
  student.id = 1;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
struct Student {
  //學生姓名
  char* name;
  //學號
  int id;
} student;
int main() {
  student.name = "Mary";
  student.id = 1;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}

方式3

宣告結構

使用typedef(型別定義)關鍵字,後面緊跟著struct關鍵字,再來是大括號{},變數的宣告在大括號{}中,結構名放在大括號結尾,最後加上分號;結束。

typedef struct {
	變數宣告
} 結構名;
1
2
3
4
5
6
typedef struct {
  //學生姓名
  char* name;
  //學號
  int id;
} Student;

定義結構變數

結構名 變數名;
1
Student student;

存取結構成員

方式一相同。

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
typedef struct {
  //學生姓名
  char* name;
  //學號
  int id;
} Student;
int main() {
  Student student;
  student.name = "Mary";
  student.id = 1;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}

results matching ""

    No results matching ""