初学者,指针问题。
这是我在学习中的一个程序,当前是运行成功的。问题在注释中,可以不用关心主函数中的内容
#include <iostream.h>
#include <string.h>
#include <math.h>
class CStudent
{
private:
int number;
char name[10]; // 如果此句改为:char *name;构造函数
static int total; //该如何编写。
public:
CStudent(int num,char *na);
~CStudent();
int GetTotal();
int GetNumber();
};
CStudent::CStudent(int num,char *na) //此处是需要更改的对方
{
number=num;
strcpy(name,na);
total++;
}
CStudent::~CStudent()
{
total--;
}
int CStudent::GetNumber()
{
return number;
}
int CStudent::GetTotal()
{
return total;
}
int CStudent::total=0; //静态数据成员,在类中声明,还要在程序中定义。
void fun();
void main()
{
CStudent s1(1001,"tom");
cout<<s1.GetNumber()<<endl;
cout<<s1.GetTotal()<<endl;
CStudent s2(1002,"lily");
cout<<s2.GetNumber()<<endl;
cout<<s2.GetTotal()<<endl;
cout<<s1.GetTotal()<<endl;
fun(); //在对象作用结束后自动调用析构函数;;
cout<<s1.GetNumber()<<endl;
cout<<s1.GetTotal()<<endl;
}
void fun()
{
CStudent s3(1003, "bob" );
cout << s3.GetNumber() << endl;
cout << s3.GetTotal() << endl;
}