继承与静态成员
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;
}
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;
}
相关文章推荐
- netbeans中按钮实现
- Delphi图像处理控件
- 使用php导入excel文件
- Servet3.0于Servlet2.5比较
- linux下centos安装android sdk最新全面教程【可行】
- 编写DLL导出类时,类中用到ADO,出现类型重定义的问题解决及原因
- Android中AIDL及其使用
- 因果图法
- 算法
- 数据库增删改查
- Automatic overvoltage protection
- Jmeter正则表达式提取器的使用方法(转)
- 如何在netbeans中插入图片背景
- 计算器开发
- UVa455——Periodic Strings
- POJ 1979 Red and Black
- multiprocessing module in python(转)
- jffs2制作与烧写
- 求浮点数是否相等
- ajax的总结( 一)