什么是類?
一系列事物的抽象,對于c++而言,萬事萬物都可以是類。
類包括:屬性+行為
屬性:事物特征->數(shù)據(jù)類型描述;
行為事物的操作->函數(shù)描述;
什么是對象?
類的具體化,類的實例化,抽象->具象;
類的特點(diǎn):封裝、繼承、派生、多態(tài)。
類的定義
創(chuàng)建方法:class
class 類名{
//權(quán)限限定詞
public:
protected://保護(hù)屬性
private://當(dāng)不做繼承時,數(shù)據(jù)成員寫成私有屬性
};//一定有一個分號
權(quán)限限定詞作用:類外只能訪問public屬性下面的東西(接口),類外訪問類中數(shù)據(jù)只能通過對象訪問(static成員除外)
若類中申明類外實現(xiàn),需要用類名限定(告訴別人函數(shù)屬于哪個類的)
沒有寫在權(quán)限限定詞下的屬性,默認(rèn)私有屬性。
權(quán)限限定詞只是限定類外對類中的訪問,對類內(nèi)無權(quán)限之分。
結(jié)構(gòu)體中默認(rèn)屬性是共有屬性
創(chuàng)建對象
普通對象、對象數(shù)組(使用較少)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#include <iostream> #include <string> using namespace std; class MM { public : void print() { cout << name << "\t" << age << endl; } void initData(string nname, int nage) { name = nname; age = nage; } protected : //新標(biāo)準(zhǔn),可以在類中給數(shù)據(jù)直接初始化 string name= "默認(rèn)值" ; int age=0; }; int main() { //沒有寫構(gòu)造函數(shù)的情況下,和C語言的創(chuàng)建方式是一樣的 MM mm; mm.print(); //沒有初始化數(shù)據(jù) MM mmArray[4]; //一般很少用對象數(shù)組 //mmArray[0]----mmArray[3] //數(shù)組: 多個變量名有規(guī)律,內(nèi)存連續(xù)的變量的集合 for ( int i = 0; i < 4; i++) { mmArray[i].initData(string( "name" ) + to_string(i), i + 19); mmArray[i].print(); } MM* p = new MM; p->initData( "張三" , 18); p->print(); delete p; p = nullptr; return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <iostream> #include <string> using namespace std; class GirlFriend { void print() { cout << "不在限定詞下的屬性" << endl; cout << "默認(rèn)為私有屬性" << endl; } public : //共有屬性 //成員函數(shù) //類中實現(xiàn)函數(shù) void printData() { cout << m_name << "\t" << m_age << endl; } //為了訪問不能訪問的部分,通常提供一些接口 void initData(string name, int age); protected : //保護(hù)屬性 //數(shù)據(jù)成員 string m_name; private : //當(dāng)前類不做繼承處理,數(shù)據(jù)成員寫成私有屬性 int m_age; }; //類外實現(xiàn)類中函數(shù),需要類名限定,告訴別人這個函數(shù)是哪里來的 void GirlFriend::initData(string name, int age) { //Lisa.initData("Lisa", 19); name="Lisa" age=19 m_name = name; //Lisa.m_name=Lisa m_age = age; //Lisa.m_age=19; //mm.initData("MM", 29); name="MM" age=29 //mm.m_name=MM; //mm.age=29 } struct MM { int num; //默認(rèn)屬性是公有屬性 protected : string name; private : int age; }; void testMM() { //MM mm = { 1001,"name",28 }; MM mm; mm.num = 103; //mm.name = "Ilove"; //mm.age = 13; } int main() { GirlFriend Lisa; Lisa.initData( "Lisa" , 19); Lisa.printData(); //類外只能訪問public //Lisa.m_name = "Lisa"; //Lisa.m_age = 18; GirlFriend mm; mm.initData( "MM" , 29); mm.printData(); //mm.print(); --->不能訪問私有屬性 return 0; } |
成員訪問(初始化)
1.提供共有接口來初始化數(shù)據(jù)(傳參)
2.通過提供共有接口返回值方式初始化數(shù)據(jù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <iostream> #include <string> using namespace std; class MM { public : //傳參 void initData(string name, int age) { m_name = name; m_age = age; } //返回引用 string& getName() { return m_name; } int & getAge() { return m_age; } void print() { cout << m_name << "\t" << m_age << endl; } protected : //默認(rèn)初始化 string m_name= "默認(rèn)值" ; int m_age=0; //不做初始化是一個垃圾值 }; int main() { MM girl; girl.initData( "girl" , 19); girl.print(); MM mm; mm.getName() = "mm" ; mm.getAge() = 18; mm.print(); MM boy; boy.print(); return 0; } |
c++有頭鏈表與c對比:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#include <iostream> #include <string> using namespace std; #if 0 struct Node { int data; struct N ode* next; }; struct Node* createList() { Node* headNode = new Node; headNode->next = nullptr; return headNode; } struct Node* createNode( int data) { Node* newNode = new Node; newNode->data = data; newNode->next = nullptr; return newNode; } void insertData(Node* headNode, int data) { Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode; } void printList(Node* headNode) { Node* pMove = headNode->next; while (pMove != nullptr) { cout << pMove->data<< " " ; pMove = pMove->next; } cout << endl; } void testListC() { Node* list = createList(); insertData(list, 10); insertData(list, 20); printList(list); } #endif #if 0 struct Node { int data; Node* next; }; class List { public : void createList() { headNode = new Node; headNode->next = nullptr; } void insertData( int data) { Node* newNode = new Node; newNode->data = data; newNode->next = nullptr; newNode->next = headNode->next; headNode->next = newNode; } void printList() { Node* pMove = headNode->next; while (pMove != nullptr) { cout << pMove->data << " " ; pMove = pMove->next; } cout << endl; } protected : Node* headNode; //用一個指針表示整個表頭 }; void testList1() { List* pList = new List; //C++第一步:創(chuàng)建對象 pList->insertData(10); pList->insertData(20); pList->printList(); } #endif class Node { public : Node*& getNext() { return next; } int & getData() { return data; } protected : int data; Node* next; }; class List { public : void createList() { headNode = new Node; headNode->getNext() = nullptr; } void insertData( int data) { Node* newNode = new Node; newNode->getNext() = nullptr; newNode->getData() = data; newNode->getNext() = headNode->getNext(); headNode->getNext() = newNode; } void printList() { Node* pMove = headNode->getNext(); while (pMove != nullptr) { cout << pMove->getData() << "\t" ; pMove = pMove->getNext(); } cout << endl; } protected : Node* headNode; }; void testList2() { //List list; //list.insertList(10); List* pList = new List; //C++第一步:創(chuàng)建對象 pList->createList(); pList->insertData(10); pList->insertData(20); pList->printList(); } int main() { //testListC(); testList2(); return 0; } |
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注服務(wù)器之家的更多內(nèi)容!
原文鏈接:https://blog.csdn.net/JustCallMeYuan/article/details/121483379