預設呼叫無參建構函式 用的非常到位
阿新 • • 發佈:2019-01-03
//---------main.cpp------------ #include <iostream> #include <string> using namespace std; class StudentID { private: int value; public: StudentID() { static int nextStudentID = 0; //很巧妙的利用了建構函式 注意建構函式的執行順序 nextStudentID++; value = nextStudentID; std::cout<<"Assigning student ID is :"<<value<<endl; } }; class Student { private: string name; StudentID id; public: Student(string n = "noNanme") { std::cout<<"Constructing student "<<n<<endl; name = n; } }; int main(int argc,char **argv) { Student a("Jeck"); return 0; }
Assigning student ID is :1
Constructing student Jeck
請按任意鍵繼續. . .