static成员变量和static成员函数例程
2024-09-26 22:28:50
#include "pch.h"
#include <iostream>
using namespace std;
class goods {
public:
goods(int w) {
weight = w;
total_weight += w;
}
~goods() {
total_weight -= weight;
}
static int total_weight_fun() {//类的函数,不能访问普通成员变量和普通函数
return total_weight;
}
goods *next; private:
int weight;
static int total_weight;//类的成员
}; int goods::total_weight = ;//声明与定义静态数据成员 void purchase(goods *&front, goods *&rear, int w) {
goods *p = new goods(w);
p->next = NULL; if (front == NULL) {
front = rear = p;
}
else {
rear->next = p;
rear = rear->next;
}
} void sale(goods *&front, goods *&rear) {
if (front == NULL) {
cout << "no any goods" << endl;
return;
} goods *q = front;
front = front->next;
delete q;
cout << "sale front goods" << endl;
}
int main()
{
goods *front = NULL, *rear = NULL;
int choice;
int weight;
do {
cout << "Key in 1 is purchase,\nKey in 2 is sale,\nKey in 0 is over.\n";
cin >> choice; switch(choice) {
case :
{
cout << "input weiht:";
cin >> weight;
purchase(front, rear, weight);
break;
}
case :
{
sale(front, rear);
break;
}
case :
break; } cout << "now total weight is: "<< goods::total_weight_fun()<<endl;
} while (choice);
cout << "exit!\n";
}
最新文章
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
- VHDL学习之模块调用
- vim c++补全
- 【FTP】在自己的电脑上建立FTP服务器
- vi/vim使用指北 ---- Moving Around in a Hurry
- 推荐一个网站Stack Overflow
- Android View事件传递机制
- (2015年郑州轻工业学院ACM校赛题)H 五子棋
- C/C++变量命名规则
- poj1562--Oil Deposits
- 对于deferred的一点点理解
- windows hook (转)
- 【笔记】《通俗详细地讲解什么是P和NP问题》的概念记录
- 数据库.MongoDB.Java样例
- [MCM] 多目标优化 MOP(multi-objective programming)
- Redis + keepalived 高可用行配置检测脚本
- Python之进程 3 - 进程池和multiprocess.Poll
- win10 store 无法连接网络(原创)
- c# 生成验证码图片
- Mybatis 中 columnPrefix别名的用法
热门文章
- CS API 测试3
- springboot+beetlsql+mysql整合
- list<;?>;转换成 对应的 class
- [GO]非结构体匿名字段
- (1)WePHP 开启WePHP
- 搜狐 WEB 标准-前端技术应用规范
- CodeForces 339B Xenia and Ringroad(水题模拟)
- CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)
- SpringMVC源码解读 - RequestMapping注解实现解读 - ConsumesRequestCondition
- [LintCode笔记了解一下]41.Maximum Subarray