c++——靜態成員靜態函數

#include<iostream>
using namespace std;

class Box 
{
public:
	static int objectCount;
	Box(double l=2.0,double b=2.0,double h = 2.0) 
	{
		cout << "constructor called." << endl;
		length = l;
		breadth = b;
		height = h;
		objectCount++;
	}
	double Volume() 
	{
		return length * breadth*height;
	}

	static int getCount() 
	{
		return objectCount;
	}
private:
	double length;
	double breadth;
	double height;
};

int Box::objectCount = 0;

int main() 
{
	cout << "initial stage count:" << Box::getCount() << endl;

	Box Box1(2.9,2.0,3.4);
	Box Box2(2.2, 2.0, 3.4);
	Box Box3(2.2, 2.0, 3.4);

	cout << "Total Objects:" << Box::objectCount << endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章