第17章特殊类成员_静态成员的使用

#include <iostream>
using namespace std;

class aspl
{
public:
	aspl(float p)
	{   
		price = p; 
	    TotalPrice+=p;
	}

	static float getTotalPrice()
	{
	     return TotalPrice;
	}
	~aspl(){ TotalPrice-=price;}
private:
	float price;
	static float TotalPrice;
};

float aspl::TotalPrice = 0;


int main()
{
	float f;
	cout<<"总库存价格为:";
	cout<<aspl::getTotalPrice()<<endl;
	aspl *p[5];  //指定一个指向aspl的数组指针
	int i=0;
	for(i=0; i<5; i++)
	{
		cout<<"请输入第"<<i<<"次进入的单箱价格:";
		cin>>f;	
		p[i] = new aspl(f);
	    cout<<"现在的总库存价格为:"<<aspl::getTotalPrice()<<endl;
	}
	cout<<"输入已经完成"<<endl;
    
	cout<<"请输入卖出的单号:";
	cin>>i;
	if(i>= 5){
	   cout<<"单号不存在"<<endl;
	}else{
	   delete p[i];
	}	
	cout<<"现在的库存为:"<<aspl::getTotalPrice()<<endl;
	return 0;
}

  

posted @ 2012-09-16 22:45  简单--生活  阅读(155)  评论(0编辑  收藏  举报
简单--生活(CSDN)