楼主:
a2318aa (CASH 翔)
2016-02-17 20:31:50各位c的高手,小弟在綀习写物件、类别的建构式的时候,遇到了一个问题
#include<iostream>
using namespace std;
class student{
public:
int id,chinese,math;
student(int vid,int vchinese,int vmath);
void show(){
cout<<"id:"<<"\t"<<id<<"\n"<<"math:"<<"\t"<<math<<"\n"<<"chinese:"<<"\t"<<chinese<<"\n";
};
};
student::student(int vid=7,int vchinese=60,int
vmath=60):id(vid),chinese(vchinese),math(vmath)
{
}
int main(){
student marry;
marry.show();
student jack(2318,95,98);
jack.show();
return 0;
}
以上是全部的程式内容,在最后的 student jack(2318,95,98); 中,为何不能写作
student jack;
jack(2318,95,98);
还是说要以其它形式才能将它分开写?
小弟目前还是新手,许多细节还不是很清楚
各位高手,如果有其它的建议,也请和我分享,谢谢~
作者:
LPH66 (-6.2598534e+18f)
2016-02-17 20:37:00要分开只能另写一个成员函数, 宣告后呼叫该成员函数初始化但“宣告后就能用”这性质就不见了, 要记得呼叫初始化函式>s89227 并不只, 唯一限制是某参数若给默认, 其后通通都要给; 不过对于成员函式, 默认值是写在宣告处不是定义处也就是并不是像原 PO 这样写在定义这里, 要写在上面