为什么?
程序源代码如下:
#include<iostream>
#include<string>
using namespace std;
class score{
string number;
string name;
float english;
float computer;
float math;
float total;
public:
score(string n=0,string na=0,float e=0,float c=0,float m=0)
:number(n),name(na),english(e),computer(c),math(m){}
string get_number(){return number;}
string get_name(){return name;}
float get_english(){return english;}
float get_computer(){return computer;}
float get_math(){return math;}
float get_total();
};
float score::get_total(){
total=english+computer+math;
return total;
}
int main(){
score *ptr=new score[3];
ptr[0]=score("09103099","qizhi",83,90,85);
ptr[1]=score("09103097","lijun",81,90.5,85);
ptr[2]=score("09103098","yinhui",82,90,85.5);
cout.width(10);cout<<"Number";cout.width(10);cout<<"Name";
cout.width(10);cout<<"English";cout.width(10);cout<<"Computer";
cout.width(10);cout<<"Math";cout.width(10);cout<<"Total\n";
register i;
for(i=0;i<3;i++){
cout.width(10);cout<<(ptr+i)->get_number();cout.width(10);cout<<(ptr+i)->get_name();
cout.width(10);cout<<(ptr+i)->get_english();cout.width(10);cout<<(ptr+i)->get_computer();
cout.width(10);cout<<(ptr+i)->get_math();cout.width(10);cout<<(ptr+i)->get_total()<<endl;
}
delete []ptr;
return 0;
}
问题:
此程序在VC6.0运行时出现问题;
但加上默认构造函数score(){}就可以了;
为什么?
这个默认构造函数不是可以不显示地写出来?如果没有,他系统会自动加上。。。
问题点数:10、回复次数:3Top
1 楼Flood1984(峰子)回复于 2004-12-04 18:32:55 得分 9
系统在你没有构造函数的时候才为你添加默认构造函数,
但是你的程序中已经有了一个构造函数了,所以编译器就不会为你添加默认构造函数,
Top
2 楼wuqizhi(天可心)回复于 2004-12-11 10:22:30 得分 0
哦,Top
3 楼Henry0(Westwolf|西山狼)回复于 2004-12-11 21:34:57 得分 1
哦Top




