C++ 特殊类成员 5-- 4.1、4.2静态成员的使用实例

#include <iostream>
using namespace std;
/*---------------------------------
     17-04 4.1、4.2静态成员的使用实例
---------------------------------*/
class aspl
{
public:
aspl(float p){price=p;TotalPrice+=price;}
~aspl(){TotalPrice-=price;}
static float get(){return TotalPrice;}
private:
float price;
static float TotalPrice;
};
float aspl::TotalPrice=0;
int main()
{
float f;
aspl *p[5];
int i=0;
while(i<4)
{
cout<<"请输入第"<<i+1<<"次购进的阿司匹林的单箱价格:";
cin>>f;
p[i++]=new aspl(f);
cout<<"阿司匹林的库存总价为:"<<aspl::get()<<endl;
}
while(1)
{
cout<<"请输入卖出的阿司匹林编号";
cin>>i;
delete p[i-1];
cout<<"阿司匹林的库存总价为:"<<aspl::get()<<endl;
}

return 0;

}

运行结果:

请输入第1次购进的阿司匹林的单箱价格:10
阿司匹林的库存总价为:10
请输入第2次购进的阿司匹林的单箱价格:20
阿司匹林的库存总价为:30
请输入第3次购进的阿司匹林的单箱价格:30
阿司匹林的库存总价为:60
请输入第4次购进的阿司匹林的单箱价格:10.5
阿司匹林的库存总价为:70.5
请输入卖出的阿司匹林编号1
阿司匹林的库存总价为:60.5
请输入卖出的阿司匹林编号2
阿司匹林的库存总价为:40.5
请输入卖出的阿司匹林编号3
阿司匹林的库存总价为:10.5
请输入卖出的阿司匹林编号4
阿司匹林的库存总价为:0
请输入卖出的阿司匹林编号

猜你喜欢

转载自blog.csdn.net/paulliam/article/details/80481931