最近在學習C++,了解到,C++中對C做了擴充,使用結構體時也可以像類一樣,規定私有數據類型和公有數據類型,同時也可以在struct中實現方法設置等等。
但為了保持面對對象的特性,建議還是使用class來描述一個類。
案例如下:
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
|
#include <iostream> #include <ctime> using namespace std ; typedef struct student { private : int a , b , c ; public : void set( int a , int b , int c) { this ->a = a ; this ->b = b ; this ->c = c ; } void prit() { cout << a << endl << b << endl << c << endl ; } }stu; stu st1 ; int main( void ) { st1.set(1,2,3); st1.prit(); return 0 ; } |
運行結果:
1
2
3
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/morixinguan/article/details/74330574