大家帮我看一下,没错没警告,运行出错!
#include <iostream>#include <string>
using namespace std;
class Student
{
private:
int age;
char *name;
public:
static int count;
Student();
Student(int m,char *n);
~Student();
void Print()const;
};
int Student::count=0;
Student::Student()
{
age=0;
name="NULL";
}
Student::Student(int m,char *n)
{
age=m;
name=new char[strlen(n)+1];
strcpy(name,n);
count++;
}
Student::~Student()
{
delete []name;
count--;
}
void Student::Print()const
{
if(name==NULL)
{
cout<<"name=Noname";
cout<<"age="<<age<<endl;
}
else
cout<<"name="<<name<<","<<"age="<<age<<endl;
}
int main()
{
cout<<"count="<<Student::count<<endl;
Student s1, *p=new Student(23,"zhanghong");
s1.Print();
p->Print();
delete p;
s1.Print();
Student stu[4];
cout<<"count="<<Student::count<<endl;
return 0;
}