c++-static
2019-12-21 16:01:40来源:博客园 阅读 ()
c++-static
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;
}
原文链接:https://www.cnblogs.com/ygjzs/p/12076507.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ 静态成员----细谈static修饰的成员 2020-03-19
- c++中的 static 关键字 2020-02-15
- C++ static静态成员 2019-11-28
- C++基础知识-inline、const、mutable、this、static 2019-03-06
- 类型转换:static_cast、reinterpret_cast等 2019-02-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash