-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic.cpp
More file actions
2606 lines (2383 loc) · 68.4 KB
/
Basic.cpp
File metadata and controls
2606 lines (2383 loc) · 68.4 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// # include <iostream>
// # include <cstdlib>
// using namespace std;
// int main()
// {
// cout << "Just a test." << endl;
// int a;
// int b;
// cin >> a >> b;
// cout << a + b << endl;
// system("pause");
// return 0;
// }
//#include <iostream>
//#include <string>
//#include <unordered_map>
//using namespace std;
//
//int main()
//{
// std::unordered_map<std::string, std::string> my_uMap {
// {"student1", "a"},
// {"student2", "b"},
// };
//
// string str = my_uMap.at("student1");
// cout << "str = " << str << endl;
//
// for (auto iter = my_uMap.begin(); iter != my_uMap.end(); iter++) {
// // pair 类型键值对分为2部分
// cout << iter->first << " " << iter->second << endl;
// }
//
// return 0;
//}
//#include <stdio.h>
//
//// 定义结构体 Student
//struct Student {
// // 结构体包含的成员变量
// char *name;
// int age;
// float score;
//};
//
//// 显示结构体的成员变量
//void display(struct Student stu) {
// printf("%s 的年龄是 %d, 成绩是 %.2f \n", stu.name, stu.age, stu.score);
//}
//
//int main() {
// struct Student stu1;
//
// // 为结构体的成员变量赋值
// stu1.name = "小明";
// stu1.age = 12;
// stu1.score = 92.5;
//
// // 调用函数
// display(stu1);
//
// return 0;
//}
//#include <stdio.h>
//
//// 通过 class 关键字定义类
//class Student {
//public:
// // 类包含的变量
// char *name;
// int age;
// float score;
//
// // 类包含的函数
// void say() {
// printf("%s 的年龄是 %d, 成绩是 %.2f \n", name, age, score);
// }
//};
//
//int main() {
// // 通过类来定义变量,即创建对象
// class Student stu1; // 也可以省略关键字 class
//
// // 为类的成员变量赋值
// stu1.name = "小明";
// stu1.age = 14;
// stu1.score = 79.5;
//
// //调用类的成员函数
// stu1.say();
//
// return 0;
//}
//#include <stdio.h>
//
//// 将类定义在命名空间中
//namespace Diy {
// class Student {
// public:
// char *name;
// int age;
// float score;
//
// public:
// void say() {
// printf("%s 的年龄是 %d, 成绩是 %f \n", name, age, score);
// }
// };
//}
//
//int main() {
// Diy::Student stu1;
// stu1.name = "小明";
// stu1.age = 15;
// stu1.score = 92.5f;
// stu1.say();
//
// return 0;
//}
//#include <cstdio>
//
//int main() {
// std::printf("test\n");
// return 0;
//}
//#include <iostream>
//#include <string>
//
//int main() {
// // 声明命名空间std
// using namespace std;
// 在 main() 函数中声明命名空间 std,它的作用范围就位于 main() 函数内部,
// 如果在其他函数中又用到了 std,就需要重新声明
//
// // 定义字符串变量
// string str;
//
// // 定义int变量
// int age;
//
// // 从控制台获取用户输入
// cin >> str >> age;
//
// // 将数据输出到控制台
// cout << str << "已经成立" << age << "年了" << endl;
//
// return 0;
//}
//#include <iostream>
//
//void func() {
// using namespace std;
// cout << "test" << endl;
//}
//
//int main() {
// using namespace std;
//
// cout << "hello" << endl;
// func();
//
// return 0;
//}
// scanf printf
//#include <iostream>
//
//using namespace std;
//
//int main() {
// int x;
// float y;
//
// cout << "Input an int number and a float number:" << endl;
// cin >> x >> y;
//
// cout << "The int number is x = " << x << endl;
// cout << "The float number is y = " << y << endl;
//
// return 0;
//}
//#include <stdio.h>
//
//int main() {
// int a;
// scanf("%d", &a);
//
// int b;
// scanf("%d", &b);
//
// int c = a + b;
// printf("%d\n", c);
//
// return 0;
//}
//#include <stdio.h>
//
//int main() {
// int a, b, flag;
//
// scanf("%d %d", &a, &b);
// flag = a > b;
// printf("flag = %d\n", flag);
//
// return 0;
//}
//#include <iostream>
//using namespace std;
//
//int main() {
// int a, b;
// bool flag; // 定义布尔变量
// cin >> a >> b;
// flag = a > b;
// cout << "flag = " << flag << endl;
//
// return 0;
//}
//#include <stdlib.h>
//
//int *p = (int*) malloc (sizeof(int) * 10); // 分配10个int型的内存空间
//free(p)
//int *p = new int; // 分配1个int型的内存空间
//delete p; // 释放内存
//
//int *p1 = new int[10]; // 分配10个int型的内存空间
//delete[] p; // 用new[]分配的内存需要用delete[]释放,它们是一一对应的
//#include <iostream>
//using namespace std;
//
//// 内联函数,交换两个数的值
//inline void swap(int *a, int *b) {
// int temp;
// temp = *a;
// *a = *b;
// *b = temp;
//}
//
//int main() {
// int m, n;
// cin >> m >> n;
//
// cout << m << ", " << n << endl;
// swap(&m, &n);
// cout << m << ", " << n << endl;
//
// return 0;
//}
//#include <iostream>
//using namespace std;
//
//// 交换int变量的值
//void Swap(int *a, int *b) {
// int temp = *a;
// *a = *b;
// *b = temp;
//}
//
//// 交换float变量的值
//void Swap(float *a, float *b) {
// float temp = *a;
// *a = *b;
// *b = temp;
//}
//
//// 交换char变量的值
//void Swap(char *a, char *b) {
// char temp = *a;
// *a = *b;
// *b = temp;
//}
//
//// 交换bool变量的值
//void Swap(bool *a, bool *b) {
// bool temp = *a;
// *a = *b;
// *b = temp;
//}
//
//int main() {
// int n1 = 100, n2 = 200;
// Swap(&n1, &n2);
// cout << n1 << ", " << n2 << endl;
//
// float f1 = 1.2, f2 = 3.2;
// Swap(&f1, &f2);
// cout << f1 << ", " << f2 << endl;
//
// char c1 = 'a', c2 = 'b';
// Swap(&c1, &c2);
// cout << c1 << ", " << c2 << endl;
//
// bool b1 = false, b2 = true;
// Swap(&b1, &b2);
// cout << b1 << ", " << b2 << endl;
//
// return 0;
//}
//#include <iostream>
//using namespace std;
//
//// 类通常定义在函数外面
//class Student {
//public:
// // 类包含的变量
// char *name;
// int age;
// float score;
//
// // 类包含的函数
// void say() {
// cout << name << "的年龄是" << age << ", 成绩是" << score << endl;
// }
//};
//
//int main() {
// Student stu;
// stu.name = "Mark";
// stu.age = 12;
// stu.score = 93.5f;
// stu.say();
//
// Student *pStu = &stu;
// pStu->name = "Jane";
// pStu->age = 15;
// pStu->score = 90.5f;
// pStu->say();
//
// // 栈内存是程序自动管理的,不能使用 delete 删除在栈上创建的对象;
// // 堆内存由程序员管理,对象使用完毕后可以通过 delete 删除。
// // 在实际开发中,new 和 delete 往往成对出现,以保证及时删除不再使用的对象,防止无用内存堆积。
// Student * pStu1 = new Student;
// pStu1->name = "Paul";
// pStu1->age = 16;
// pStu1->score = 89.5f;
// pStu1->say();
// delete pStu1;
//
// return 0;
//}
// 在类体中和类体外定义成员函数是有区别的:在类体中定义的成员函数会自动成为内联函数,在类体外定义的不会。
// 当然,在类体内部定义的函数也可以加 inline 关键字,但这是多余的,因为类体内部定义的函数默认就是内联函数。
//class Student {
//public:
// // 成员变量
// char *name;
// int age;
// float score;
//
// void say(); // 函数声明
//};
//
//void Student::say() {
// cout << name << "的年龄是" << age << ", 成绩是" << score << endl;
//}
//
//#include <iostream>
//using namespace std;
//
//// 类的声明
//class Student {
//private:
// char *m_name;
// int m_age;
// float m_score;
//
//public:
// void setname(char *name);
// void setage(int age);
// void setscore(float score);
// void show();
//};
//
//void Student::setname(char *name) {
// m_name = name;
//}
//
//void Student::setage(int age) {
// m_age = age;
//}
//
//void Student::setscore(float score) {
// m_score = score;
//}
//
//void Student::show() {
// cout << m_name << "的年龄是" << m_age << ", 成绩是" << m_score << endl;
//}
//
//int main() {
// // 在栈上创建对象
// Student stu;
// stu.setname("Jack");
// stu.setage(15);
// stu.setscore(93.5f);
// stu.show();
//
// // 在堆上创建对象
// Student *pStu = new Student;
// pStu->setname("Jane");
// pStu->setage(17);
// pStu->setscore(96.5f);
// pStu->show();
//
// return 0;
//}
// 类的声明和成员函数的定义都是类定义的一部分,
// 在实际开发中,我们通常将类的声明放在头文件中,而将成员函数的定义放在源文件中。
//#include <iostream>
//using namespace std;
//
//class Student {
//private:
// char *m_name;
// int m_age;
// float m_score;
//public:
// // 声明构造函数
// Student(char *name, int age, float score);
// // 声明普通成员函数
// void show();
//};
//
//// 定义构造函数
//Student::Student(char *name, int age, float score) {
// m_name = name;
// m_age = age;
// m_score = score;
//}
//
//// 定义普通成员函数
//void Student::show() {
// cout << m_name << "的年龄是" << m_age << ", 成绩是" << m_score << endl;
//}
//
//int main() {
// // 创建对象时向构造函数传参
// Student stu("Jack", 18, 93.5f);
// stu.show();
//
// Student *pStu = new Student("Jane", 17, 94.5f);
// pStu->show();
//
// return 0;
//}
//#include <iostream>
//using namespace std;
//
//class Student {
//private:
// char *m_name;
// int m_age;
// float m_score;
//public:
// Student();
// Student(char *name, int age, float score);
// void setname(char *name);
// void setage(int age);
// void setscore(float score);
// void show();
//};
//
//Student::Student() {
// m_name = nullptr;
// m_age = 0;
// m_score = 0.0;
//}
//
//Student::Student(char *name, int age, float score) {
// m_name = name;
// m_age = age;
// m_score = score;
//}
//
//void Student::setname(char *name) {
// m_name = name;
//}
//
//void Student::setage(int age) {
// m_age = age;
//}
//
//void Student::setscore(float score) {
// m_score = score;
//}
//
//void Student::show() {
// if(m_name == nullptr || m_age <= 0) {
// cout << "成员变量还未初始化" << endl;
// } else {
// cout << m_name << "的年龄是" << m_age << ", 成绩是" << m_score << endl;
// }
//}
//
//int main() {
// // 调用构造函数 Student(char *, int, float)
// Student stu("Karl", 19, 93.5f);
// stu.show();
//
// // 调用构造函数 Student()
// Student *pStu = new Student();
// pStu->show();
// pStu->setname("Nane");
// pStu->setage(15);
// pStu->setscore(96.5f);
// pStu->show();
//
// return 0;
//}
// 默认构造函数 Student(){}
// 成员变量的初始化顺序与初始化列表中列出的变量的顺序无关,它只与成员变量在类中声明的顺序有关。
//#include <iostream>
//using namespace std;
//
//class Student {
//private:
// char *m_name;
// int m_age;
// float m_score;
//public:
// Student(char *name, int age, float score);
// void show();
//};
//
//// 采用初始化列表
//Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score) {
// // TODO
//}
//
//void Student::show() {
// cout << m_name << "的年龄是" << m_age << ", 成绩是" << m_score << endl;
//}
//
//int main() {
// Student stu("Jane", 18, 90.5f);
// stu.show();
// Student *pStu = new Student("Mack", 18, 89.5f);
// pStu->show();
//
// return 0;
//}
// 初始化 const 成员变量的唯一方法就是使用初始化列表
//class VLA {
//private:
// const int m_len;
// int *m_arr;
//public:
// VLA(int len);
//};
//
//// 必须使用初始化列表来初始化m_len
//VLA::VLA(int len): m_len(len) {
// m_arr = new int[len];
//}
// 与C语言中 malloc()、free() 最大的一个不同之处在于:
// 用 new 分配内存时会调用构造函数,用 delete 释放内存时会调用析构函数
//#include <iostream>
//using namespace std;
//
//class VLA {
//public:
// VLA(int len); // 构造函数
// ~VLA(); // 析构函数
//public:
// void input(); // 从控制台输入数组元素
// void show(); // 显示数组元素
//private:
// int *at(int i); // 获取第i个元素的指针
//private:
// const int m_len; // 数组长度
// int *m_arr; // 数组指针
// int *m_p; // 指向数组第i个元素的指针
//};
//
//VLA::VLA(int len): m_len(len) { // 使用初始化列表来给m_len赋值
// if (len > 0) {
// m_arr = new int[len]; // 分配内存
// } else {
// m_arr = nullptr;
// }
//}
//
//VLA::~VLA() {
// delete[] m_arr; // 释放内存
//}
//
//void VLA::input() {
// for (int i = 0; m_p = at(i); i++) {
// cin >> *at(i);
// }
//}
//
//void VLA::show() {
// for (int i = 0; m_p = at(i); i++) {
// if (i == m_len - 1) {
// cout << *at(i) << endl;
// } else {
// cout << *at(i) << ", ";
// }
// }
//}
//
//int * VLA::at(int i) {
// if (!m_arr || i < 0 || i >= m_len) {
// return nullptr;
// } else {
// return m_arr + i;
// }
//}
//
//int main() {
// // 创建一个有n个元素的数组(对象)
// int n;
// cout << "Input array length: ";
// cin >> n;
// VLA *parr = new VLA(n);
//
// // 输入数组元素
// cout << "Input " << n << " numbers: ";
// parr->input();
//
// // 输出数组元素
// cout << "Elements: ";
// parr->show();
//
// // 删除数组(对象)
// delete parr;
//
// return 0;
//}
//
//在所有函数之外创建的对象是全局对象,它和全局变量类似,位于内存分区中的全局数据区,
//程序在结束执行时会调用这些对象的析构函数。
//在函数内部创建的对象是局部对象,它和局部变量类似,位于栈区,函数执行结束时会调用这些对象的析构函数。
//new 创建的对象位于堆区,通过 delete 删除时才会调用析构函数;如果没有 delete,析构函数就不会被执行。
//#include <iostream>
//#include <string>
//using namespace std;
//
//class Demo {
//public:
// Demo(string s);
// ~Demo();
//private:
// string m_s;
//};
//
//Demo::Demo(string s): m_s(s) {
//
//}
//
//Demo::~Demo() {
// cout << m_s << endl;
//}
//
//void func() {
// // 局部对象
// Demo obj1("1");
//}
//
//// 全局对象
//Demo obj2("2");
//
//int main() {
// // 局部对象
// Demo obj3("3");
//
// // new创建的对象
// Demo *pObj4 = new Demo("4");
//
// func();
//
// cout << "main" << endl;
//
// return 0;
//}
//1
//main
//3
//2
//
//Process returned 0 (0x0) execution time : 0.021 s
//Press any key to continue.
// #include <iostream>
// #include <string>
// #include <cstdlib>
// using namespace std;
// class Student {
// public:
// void setname(string name);
// void setage(int age);
// void setscore(float score);
// void show();
// private:
// string name;
// int age;
// float score;
// };
// void Student::setname(string name) {
// this->name = name;
// }
// void Student::setage(int age) {
// this->age = age;
// }
// void Student::setscore(float score) {
// this->score = score;
// }
// void Student::show() {
// cout << this->name << "的年龄是" << this->age << ",成绩是" << this->score << endl;
// }
// int main() {
// Student *pStu = new Student;
// pStu->setname("Mac");
// pStu->setage(19);
// pStu->setscore(88.5f);
// pStu->show();
// system("pause");
// return 0;
// }
//this 是 const 指针,它的值是不能被修改的,一切企图修改该指针的操作,如赋值、递增、递减等都是不允许的。
//this 只能在成员函数内部使用,用在其他地方没有意义,也是非法的。
//只有当对象被创建后 this 才有意义,因此不能在 static 成员函数中使用(后续会讲到 static 成员)。
// static 成员变量属于类,不属于某个具体的对象,即使创建多个对象,也只为 m_total 分配一份内存,
// 所有对象使用的都是这份内存中的数据。当某个对象修改了 m_total,也会影响到其他对象。
// class Student {
// public:
// Student(char *name, int age, float score);
// void show();
// public:
// static int m_total; // 静态成员变量
// private:
// char *m_name;
// int m_age;
// float m_score;
// };
// int Student::m_total = 0; // static 成员变量必须在类声明的外部初始化
// // static 成员变量的内存既不是在声明类时分配,也不是在创建对象时分配,而是在(类外)初始化时分配。
// Student::m_total = 10; // 通过类访问static成员变量
// // 通过对象来访问static成员变量
// Student stu("Karl", 15, 93.5f);
// stu.m_total = 20;
// // 通过对象指针来访问static成员变量
// Student *pStu = new Student("Lee", 17, 90);
// pStu->m_total = 20;
// static 成员变量不占用对象的内存,而是在所有对象之外开辟内存,即使不创建对象也可以访问。
// 具体来说,static 成员变量和普通的 static 变量类似,都在内存分区中的全局数据区分配内存.
// #include <iostream>
// #include <cstdlib>
// #include <string>
// using namespace std;
// class Student {
// public:
// Student(string name, int age, float score);
// void show();
// private:
// static int m_total; // 静态成员变量
// private:
// string m_name;
// int m_age;
// float m_score;
// };
// // 初始化静态成员变量
// int Student::m_total = 0;
// Student::Student(string name, int age, float score): m_name(name), m_age(age), m_score(score) {
// m_total++; // 操作静态成员变量
// }
// void Student::show() {
// cout << m_name << " age is " << m_age << ", score is " << m_score << "(Total " << m_total << " students)" << endl;
// }
// int main() {
// // 创建匿名对象
// (new Student("Tim", 15, 90))->show();
// (new Student("Tony", 16, 92))->show();
// (new Student("Tom", 17, 93))->show();
// system("pause");
// return 0;
// }
// 之所以使用匿名对象,是因为每次创建对象后只会使用它的 show() 函数,不再进行其他操作。
// 不过使用匿名对象无法回收内存,会导致内存泄露,在中大型程序中不建议使用。
// 1) 一个类中可以有一个或多个静态成员变量,所有的对象都共享这些静态成员变量,都可以引用它。
// 2) static 成员变量和普通 static 变量一样,都在内存分区中的全局数据区分配内存,到程序结束时才释放。
// 这就意味着,static 成员变量不随对象的创建而分配内存,也不随对象的销毁而释放内存。
// 而普通成员变量在对象创建时分配内存,在对象销毁时释放内存。
// 3) 静态成员变量必须初始化,而且只能在类体外进行。例如:
// int Student::m_total = 10;
// 初始化时可以赋初值,也可以不赋值。如果不赋值,那么会被默认初始化为 0。全局数据区的变量都有默认的初始值 0,
// 而动态数据区(堆区、栈区)变量的默认值是不确定的,一般认为是垃圾值。
// 4) 静态成员变量既可以通过对象名访问,也可以通过类名访问,但要遵循 private、protected 和 public 关键字的访问权限限制。
// 当通过对象名访问时,对于不同的对象,访问的是同一份内存。
// 在类中,static 除了可以声明静态成员变量,还可以声明静态成员函数。普通成员函数可以访问所有成员(包括成员变量和成员函数),
// 静态成员函数只能访问静态成员。
// 静态成员函数与普通成员函数的根本区别在于:普通成员函数有 this 指针,可以访问类中的任意成员;
// 而静态成员函数没有 this 指针,只能访问静态成员(包括静态成员变量和静态成员函数)。
//#include <iostream>
//using namespace std;
//
//class Student {
//public:
// Student(char *name, int age, float score);
// void show();
//public: // 声明静态成员函数
// static int getTotal();
// static float getPoints();
//private:
// static int m_total; // 总人数
// static float m_points; // 总成绩
//private:
// char *m_name;
// int m_age;
// float m_score;
//};
//
//int Student::m_total = 0;
//float Student::m_points = 0.0;
//
//Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score) {
// m_total++;
// m_points += score;
//}
//
//void Student::show() {
// cout << m_name << "的年龄是" << m_age << ", 成绩是" << m_score << endl;
//}
//
//// 定义静态成员函数
//int Student::getTotal() {
// return m_total;
//}
//
//float Student::getPoints() {
// return m_points;
//}
//
//int main() {
// (new Student("J", 15, 90.5))->show();
// (new Student("K", 18, 90.5))->show();
//
// int total = Student::getTotal();
// float points = Student::getPoints();
// cout << "当前共有" << total << "名学生,总成绩是" << points << ", 平均分是" << points/total << endl;
//
// return 0;
//}
// 和静态成员变量类似,静态成员函数在声明时要加 static,在定义时不能加 static。
// 静态成员函数可以通过类来调用(一般都是这样做),也可以通过对象来调用
//class Student {
//public:
// Student(char *name, int age, float score);
// void show();
//
// // 声明常成员函数
// char *getname() const;
// int getage() const;
// float getscore() const;
//private:
// char *m_name;
// int m_age;
// float m_score;
//};
//
//Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score) {
// // TODO
//}
//
//void Student::show() {
// cout << m_name << "的年龄是" << m_age << ", 成绩是" << m_score << endl;
//}
//
//// 定义常成员函数
//char * Student::getname() const {
// return m_name;
//}
//
//int Student::getage() const {
// return m_age;
//}
//
//float Student::getscore() const {
// return m_score;
//}
//
//区分一下 const 的位置:
//函数开头的 const 用来修饰函数的返回值,
//表示返回值是 const 类型,也就是不能被修改,例如const char * getname()。
//函数头部的结尾加上 const 表示常成员函数,这种函数只能读取成员变量的值,
//而不能修改成员变量的值,例如char * getname() const。
// 由于常量一旦被创建后其值就不能再改变,所以常量必须在定义的同时赋值(初始化),
// 后面的任何赋值行为都将引发错误。一如既往,初始化常量可以使用任意形式的表达式,如下所示:
//#include <stdio.h>
//
//int getNum() {
// return 100;
//}
//
//int main() {
// int n = 90;
// const int MaxNum1 = getNum(); // 运行时初始化
// const int MaxNum2 = n; // 运行时初始化
// const int MaxNum3 = 80; // 编译时初始化
//
// printf("%d, %d, %d \n", MaxNum1, MaxNum2, MaxNum3);
// return 0;
//}
//const int *p1;
//int const *p2;
//int * const p3;
// 在最后一种情况下,指针是只读的,也就是 p3 本身的值不能被修改;在前面两种情况下,
// 指针所指向的数据是只读的,也就是 p1、p2 本身的值可以修改(指向不同的数据),
// 但它们指向的数据不能被修改。
// 指针本身和它指向的数据都有可能是只读的,下面的两种写法能够做到这一点:
//const int * const p4;
//int const * const p5;
// 在C语言中,单独定义 const 变量没有明显的优势,完全可以使用#define命令代替。const 通常用在函数形参中,
// 如果形参是一个指针,为了防止在函数内部修改指针指向的数据,就可以用 const 来限制。
//#include <stdio.h>
//#include <string.h>
//
//// 查找字符串中某个字符出现的次数
//// size_t是一种无符号的整型数,它的取值没有负数,在数组中也用不到负数,而它的取值范围是整型数的双倍。
//size_t strnchr(const char *str, char ch) {
// int i, n = 0, len = strlen(str);
//
// for (i = 0; i < len; i++) {
// if (str[i] == ch) {
// n++;
// }
// // printf("%c, %c, %d\n", ch, str[i], n);
// }