#include
#include
using namespace std;
class Book
{
public:
Book(string name,double price){
_name=name;
_price=price;
_bookNum++;
_sumPrice+=price;
};
void display(){
cout<<"name:"<<_name<<endl;
cout<<"price:"<<_price<<endl;
cout<<"totle number:"<<_bookNum<<endl;
cout<<"totle price:"<<_sumPrice<<endl;
}
private:
string _name;
double _price;
static int _bookNum;
static double _sumPrice;
};
int Book::_bookNum=0;
double Book::_sumPrice=0.0;
int main()
{
Book b1("c++ Primer",105);
Book b2("c++ Primer plus",98);
b1.display();
b2.display();
return 0;
}
这样就好了,静态成员一定要先初始化。