#include <iostream.h>#include <string.h>class CStudent{public:CStudent(char *n, int a);~CStudent();static void SetAge(int age);private:char *name;int age;static int nTotalObj;};int CStudent::nTotalObj = 0;CStudent::CStudent(char *n, int a):age(a){int nLen = strlen(n);name = new char[nLen+1];strcpy(name,n);name[nLen] = ’\0’;nTotalObj++;}CStudent::~CStudent(){delete[] name;nTotalObj--;}void CStudent::SetAge(int age){this->age = age;}void main(){CStudent stu1("张三",25);CStudent str2("李四",26);cout<<"CStudent::nTotalObj="<<CStudent::nTotalObj<<endl;}
2 回答
已采纳
慕粉3426063
TA贡献1条经验 获得超0个赞
nTotalObj是静态数据成员,每次定义一个新的CStudent对象后,nTotalObj的值加1,释放一个CStudent对象后,nTotalObj减1。
nTotalObj表示当前定义的CStudent对象个数。
- 2 回答
- 0 关注
- 1972 浏览
添加回答
举报
0/150
提交
取消