C++關(guān)于類的static函數(shù)的程序,錯(cuò)在哪里?
2 回答

慕桂英3389331
TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
1234567891011121314151617181920212223242526272829303132333435363738394041424344 | #include <iostream> using namespace std; class store{ public : store( int n, int q, double p); store(){} //加上默認(rèn)構(gòu)造函數(shù) void total(); static double average(); double display(); private : int num; int quantity; static int SumOfQuan; double price; static double sum; } ; /*全局聲明*/ int store::SumOfQuan=0; double store::sum=0.0; store::store( int n, int q, double p){ num = n; quantity = q; price = p; } void store::total(){ sum = sum + quantity*price; SumOfQuan = SumOfQuan + quantity; } double store::average(){ //static 在外面不用再加 return (sum/SumOfQuan); } double store::display(){ cout<< "總收款額: " <<sum<< " 平均售價(jià): " <<SumOfQuan<<endl; return 0.0; //返回值 } int main(){ store s[3]; //這沒有默認(rèn)構(gòu)造函數(shù) int i = 0; for (;i<3;i++){ s[i].total(); //這里是調(diào)用函數(shù) } s[0].display(); return 0; } |

ITMISS
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
dluble store::sum=0;
int store::SunOfQuan=1;//全局內(nèi)初始化
static double store::average(){
return (sum/SumOfQuan);//這要零檢測
}
- 2 回答
- 0 關(guān)注
- 750 瀏覽
添加回答
舉報(bào)
0/150
提交
取消