//---------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
请按任意键继续. . .