最近在學習C++,了解到,C++中對C做了擴充,使用結構體時也可以像類一樣,規定私有數據類型和公有數據類型,同時也可以在struct中實現方法設置等等。
但為了保持面對對象的特性,建議還是使用class來描述一個類。
案例如下:
#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
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答