求教c++大神。帮忙解决。
#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;
}
问题一:以上程序编译能通过吗,为什么?
问题二:成员变量nTotalObj在程序中起什么作用,它是如何实现的?
问题三:如果规定该程序的主函数和类CStudent中的成员变量的属性不允许改变,应该如何改正该程序?