静态成员.cpp

来自「C++源代码集」· C++ 代码 · 共 41 行

CPP
41
字号
# include <stdio.h>

class Pumpkin
{
private:
	int weight;
	static int total_weight;
	static int total_number;
public:
	Pumpkin(int w)
	{
	   weight = w;
	   total_weight +=w;
	   total_number++;
	}
	~Pumpkin()
	{
	   total_weight -=weight;
	   total_number--;
	}
	void display()
	{
	   printf("The pumpkin weights %d pounds\n",
		   weight);
       printf("The pumpkin total_weight %d pounds\n",
		   total_weight);
       printf("The pumpkin total_number %d pounds\n",
		   total_number);
	}
};

int Pumpkin::total_weight=0;
int Pumpkin::total_number=0;

int main()
{
    Pumpkin p1(55),p2(20),p3(12);
	p1.display();
	p2.display();
	p3.display();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?