关于构造函数的,谁能帮我改一下
#include <iostream.h>class student
{
private:
int id;
float chinese,english,math;
public:
student();
student(int m_id,float m_chinese,float m_english,float m_math);
void show();
};
student::student()
{
id=0;
chinese=english=math=0;
}
student(int m_id,float m_chinese,float m_english,float m_math)
{
id=m_id;
chinese=m_chinese;
english=m_english;
math=m_math;
}
student::show()
{
cout<<id<<endl;
cout<<chinese<<endl;
cout<<english<<endl;
cout<<math<<endl;
}
int main()
{
student s1;
student s2(1,60,70,80);
s1.show();
s2.show();
return 0;
}
与这个有什么区别(这个是正确的):
#include <iostream.h>
class student
{
private:
int id;
float chinese,english,math;
public:
student()
{
id=1;
chinese=100;
english=100;
math=100;
}
void show();
};
void student::show()
{
cout<<id<<endl;
cout<<chinese<<endl;
cout<<english<<endl;
cout<<math<<endl;
}
int main()
{
class student s1,s2;
s1.show();
s2.show();
return 0;
}
是因为构造函数不能再类外初始化吗?
[ 本帖最后由 ku_klox 于 2010-6-20 21:46 编辑 ]