请前辈比较两个程序,一个有问题一个没问题,为什么?
下面两个程序做比较,为什么第一个程序没问题,第二个程序有问题?第一个程序:
#include <iostream>
#include <string.h>
class Student
{
public:
Student::Student(char *n = "zhangsan", int a = 23, int i = 1009, int g = 3, int r = 90)
{
strcpy(name,n);
age = a;
id = i;
grade = g;
result = r;
std::cout << name << " " << age << " " << id << " " << grade << " " << result << std::endl;
}
protected:
char name[30];
int age;
int id;
int grade;
int result;
};
int main()
{
Student st("lisi");
return 0;
}
第二个程序:
#include <iostream>
#include <string.h>
class Student
{
public:
Student::Student(char *n, int a, int i, int g, int r);
protected:
char name[30];
int age;
int id;
int grade;
int result;
};
int main()
{
Student st("lisi");
return 0;
}
Student::Student(char *n = "zhangsan", int a = 23, int i = 1009, int g = 3, int r = 90)
{
strcpy(name,n);
age = a;
id = i;
grade = g;
result = r;
std::cout << name << " " << age << " " << id << " " << grade << " " << result << std::endl;
}