首页 > 其他 > 详细

继承与静态成员

时间:2015-09-20 07:00:52      阅读:185      评论:0      收藏:0      [点我收藏+]

基类定义了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;

}


继承与静态成员

原文:http://10622551.blog.51cto.com/10612551/1696429

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!