您的位置:首页 > 其它

继承与静态成员

2015-09-20 18:49 309 查看
基类定义了static成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例。

class Person

{

public :

Person ()

{

++ _count ;

}

protected :

string _name ; // 姓名

string _sex ;
// 性别

int _age ; // 年龄

public :

static int _count; // 统计人的个数。

};

int Person :: _count =
0;

class Student : public Person

{

protected :

int _stuNum ; // 学号

};

class Graduate : public Student

{

protected :

string _seminarCourse ; // 研究科目

};

void TestPerson1 ()

{

Student s1 ;

Student s2 ;

Student s3 ;

Graduate s4 ;

cout <<" 人数 :"<< Person ::_count << endl;

Student ::_count =
0;

cout <<" 人数 :"<< Person ::_count << endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: