形参赋初始值的问题
class Student
{
string m_strName;
public:
Student();
Student(string _name); //Student(string _name='Leo')为什么编译不通过呢?
Student(const Student & stu);
~Student();
void SetName(string _name); //void SetName(string _name='Leo')同上
string GetName();
};
Student::Student()
{
m_strName="";
cout<<"Student()"<<endl;
}
Student::Student(string _name)
{
m_strName=_name;
cout<<"Student(string _name)"<<endl;
}
Student::Student(const Student& stu)
{
cout<<"Student(const Student & stu)"<<endl;
}
Student::~Student()
{
cout<<"~Student"<<endl;
}
void Student::SetName(string _name)
{
m_strName=_name;
cout<<"SetName(_name)"<<endl;
}
string Student::GetName()
{
return m_strName;
cout<<"GetName()"<<endl;
}
int main(void)
{
// 通过new方式实例化对象*stu
Student *stu = new Student();
// 更改对象的数据成员为“慕课网”
stu->SetName("慕课网");
// 打印对象的数据成员
cout<<stu->GetName()<<endl;
return 0;
}如上注释所示,为什么在申明函数的时候给形参赋值通不过呢?我记得前面的课程里好像有在声明函数的时候赋值的
希望前辈指点,谢谢