c++-static

0
10

static成员变量

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

class AA
{
public:
	AA(int a, int b)
	{
		m_a = a;
		m_b = b;
	}



	int getC()
	{
		m_c++;
		return m_c;
	}
	
	//静态的成员方法
	static int& getCC()
	{
		return m_c;
	}

private:
	//static修饰的静态成员变量
	static int m_c;
	int m_a;
	int m_b;
};

//静态成员变量的初始化,一定类的外边。
int AA::m_c = 100;



int main(void)
{
	AA a1(10, 20);
	AA a2(100, 200);

	cout << a1.getC() << endl;//101
	cout << a2.getC() << endl;//102

	//a1.getCC() = 200;
	AA::getCC() = 200;

	cout << a1.getC() << endl;//201
	cout << a2.getC() << endl;//202

	return 0;
}

static成员变量的练习

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

class Box
{
public:
	Box(int l, int w)
	{
		len = l;
		width = w;
	}

	int volume()
	{
		int v = len *width*hight;

		cout << "高度是" << hight << endl;
		cout << "体积是" <<  v << endl;

		return v;
	}

	static void changeHight(int h)
	{
		hight = h;
	}
private:
	int len;
	int width;
	static int hight;

};

int Box::hight = 100;



int main(void)
{
	Box b1(10, 20);
	Box b2(100, 200);

	b1.volume();
	b2.volume();

	Box::changeHight(300);

	b1.volume();
	b2.volume();


	return 0;
}

static练习求平均分

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

class Box
{
public:
	Box(int l, int w)
	{
		len = l;
		width = w;
	}

	int volume()
	{
		int v = len *width*hight;

		cout << "高度是" << hight << endl;
		cout << "体积是" <<  v << endl;

		return v;
	}

	static void changeHight(int h)
	{
		hight = h;
	}
private:
	int len;
	int width;
	static int hight;

};

int Box::hight = 100;



int main(void)
{
	Box b1(10, 20);
	Box b2(100, 200);

	b1.volume();
	b2.volume();

	Box::changeHight(300);

	b1.volume();
	b2.volume();


	return 0;
}

static成员变量占用大小

static不占实际空间

#include	<iostream>
using namespace	std;

class	C1
{
public:
	int	i;		//4
	int	j;		//4
	int	k;		//4
};	//12

class	C2
{
public:
	int	i;
	int	j;
	int	k;
	static int	m;																			//4
public:
	int	getK()	const	{ return	k; }		//4
	void	setK(int	val)	{ k = val; }	//4
};


struct	S1
{
	int	i;
	int	j;
	int	k;
};	//12

struct	S2
{
	int	i;
	int	j;
	int	k;
	static int	m;
};	//12?

int	main()
{
	cout << "c1	:	" << sizeof(C1) << endl;
	cout << "c1	:	" << sizeof(C2) << endl;

	C2 c1, c2;

	//c1.getK();  //为什么会return c1.k 
	//c2.getK(); // 为什么会return c2.k

	cout << " ----- " << endl;

	cout << "c1	:	" << sizeof(S1) << endl;
	cout << "c1	:	" << sizeof(S2) << endl;

	return 0;
}

static仓库进出练习

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


class Goods
{
public:
	Goods()
	{
		weight = 0;
		next = NULL;
		cout << "创建了一个重量为" << weight << "的货物" << endl;
	}

	Goods(int w) {
		//需要创建一个w的货物,并且仓库加上这个重量
		weight = w;
		next = NULL;
		total_weight += w;
		cout << "创建了一个重量为" << weight << "的货物" << endl;
	}

	~Goods() {
		//仓库减少这个货物的重量
		cout << "删除了一箱重量是" << weight << "的货物" << endl;
		total_weight -= weight;
	}


	static int get_total_weight()
	{
		return total_weight;
	}

	Goods *next;
private:
	int weight;//重量
	static int total_weight;//仓库的总重量
};

int Goods::total_weight = 0;


void buy(Goods * &head, int w)
{
	//创建一个货物 重量是w
	Goods *new_goods = new Goods(w);

	if (head == NULL) {
		head = new_goods;
	}
	else {
		new_goods->next = head;
		head = new_goods;
	}

}

void sale(Goods * &head)
{
	if (head == NULL) {
		cout << "仓库中已经没有货物了。。" << endl;
		return;
	}

	Goods *temp = head;
	head = head->next;

	delete temp;
	cout << "saled." << endl;
}



int main(void)
{
	int choice = 0;
	Goods *head = NULL;
	int w;

	do {
		cout << "1 进货" << endl;
		cout << "2 出货" << endl;
		cout << "0 退出" << endl;

		cin >> choice;
		switch (choice)
		{
		case 1:
			//进货
			cout << "请输出要创建货物的重量" << endl;
			cin >> w;
			buy(head, w);
			break;
		case 2:
			//出货
			sale(head);
			break;
		case 0:
			//退出
			return 0;
		default:
			break;
		}

		cout << "当前仓库的总重量是"<<Goods::get_total_weight() << endl;

	} while (1);
	
	return 0;
}

<

发布回复

请输入评论!
请输入你的名字