C++基础09-货物售卖和MyArray实现
1、货物出货与进货
#if 0
#include<iostream>
using namespace std;
/*
某商店经销一种货物。货物购进和卖出时以箱为单位。各箱
的重量不一样,因此商店需要记录目前库存的总重量,现在用
C++模拟商店货物购进和卖出的情况
*/
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;}static int get_total_wight() {return total_weight;}~Goods() {//需要删除一个w的货物,并且仓库减这个重量cout << "删除了一个重量为:" << weight << "的货物" << endl;total_weight -= weight;}
public:Goods *next;
private:int weight;static int total_weight;
};
int Goods::total_weight = 0;
void buy(Goods *&head, int w) {//创建一个货物 重量为wGoods *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;int w;Goods *head = NULL; //利用链表管理 无头指针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;break;default:break;}cout << "当前仓库的总重量是:" <<Goods::get_total_wight()<< endl;} while (1);
}
#endif
我的思路:
#if 0
#include<iostream>
using namespace std;
/*
某商店经销一种货物。货物购进和卖出时以箱为单位。各箱
的重量不一样,因此商店需要记录目前库存的总重量,现在用
C++模拟商店货物购进和卖出的情况
*/
class Goods {
public:Goods(int num, int weight) {m_num = num;m_weight = weight;}void get_in_good() {m_sum_weight += m_num*m_weight;}void get_out_good(int num, int weight) {m_sum_weight -= num*weight;}static int get_m_sum_weight(){return m_sum_weight;}
private:int m_num;int m_weight;static int m_sum_weight;
};
int Goods::m_sum_weight = 0;
int main() {Goods g1(1, 20);Goods g2(2, 30);Goods g3(3, 50);g1.get_in_good();g2.get_in_good();g3.get_in_good();g2.get_out_good(1, 30);cout << Goods::get_m_sum_weight() << endl;return 0;
}
#endif
2、数组类的封装
MyArray.h
#pragma once
#include<iostream>
using namespace std;
class MyArray
{
public:friend ostream& operator<<(ostream& os, const MyArray &obj);friend istream& operator>>(istream& os, MyArray &obj);friend bool operator==(const MyArray &a1, const MyArray& a2); //全局函数bool operator!=(const MyArray& another); //成员函数MyArray();MyArray(int length);MyArray(const MyArray &obj);MyArray& operator=(const MyArray &obi);int& operator[](int index) const; void setData(int index, int value);int getData(int index);int getLength() const;~MyArray();
private:int m_length;int *m_space; //指向堆上空间 数组首地址
};
MyArray.cpp
#include "MyArray.h"MyArray::MyArray() {cout << "MyArray()" << endl;m_length = 0;m_space = NULL;
}
MyArray::MyArray(int length){/*if (length < 0){m_length = 0;m_space = new int[length];}else {m_length = length;m_space = new int[length];}*///等价于if (length < 0){length = 0;}m_length = length;m_space = new int[length];
}
MyArray::MyArray(const MyArray &obj) {if (obj.m_length>=0){ if (m_space != NULL) {delete[] m_space;m_space = NULL;}this->m_length = obj.m_length;this->m_space = new int[this->m_length];for (int i = 0; i < m_length; i++) //数据元素深拷贝{this->m_space[i] = obj.m_space[i];}}
}
MyArray::~MyArray() {if (m_space != NULL) {delete[] m_space;m_space = NULL;}
}
MyArray& MyArray::operator=(const MyArray &another) {if (this == &another){return *this;}if (this->m_space != NULL) {delete this->m_space;this->m_space = NULL;this->m_length = 0;}if (another.m_length >= 0){this->m_length = another.m_length;this->m_space = new int[this->m_length];for (int i = 0; i < m_length; i++) //数据元素深拷贝{this->m_space[i] = another.m_space[i];}}return *this;
}
int& MyArray::operator[](int index) const{return this->m_space[index];
}void MyArray::setData(int index, int value) {if (m_space != NULL) {//a1.setData(i, i);m_space[index] = value;}}
int MyArray::getData(int index) {return m_space[index];
}
int MyArray::getLength() const {return m_length;
}
bool MyArray::operator!=(const MyArray& another) {/*if (this->getLength() != another.getLength()) {return true;}for (int i = 0; i < this->getLength(); i++) {if (this->m_space[i] != another.m_space[i])return true;}return false;*///等价于return !(*this == another);
}
ostream& operator<<(ostream& os, const MyArray &obj) { //防止被修改添加const后报错//原因分析:在调用getLengt()传入的是&obj,并不是const类型//安全性高的转换为安全性低的会报错//下面的operator[](&obj)传入的是&obj,并不是const类型 所以报错//修改方法就是加上在getLength() 函数后添加const//修改方法就是加上在operator[](int index)函数后添加constos << "遍历整个数组" << endl;//for (int i = 0; i <obj.m_length; i++) 无法修改for (int i = 0; i <obj.getLength(); i++){//os << obj.m_space[i] << " ";//等价于os << obj[i] << " ";}return os;
}
//istream& operator >> (istream& is,MyArray &obj) {
// cout << "请输入数组元素个数:";
// cin >> obj.m_length;
// obj.m_space = new int[obj.m_length];
// for (int i = 0; i < obj.m_length; i++)
// {
// int j;
// cin >> j;
// obj.m_space[i] = j;
// }
// return is;
//
//}
istream& operator>>(istream& is, MyArray &array) {cout << "请输入:" << array.getLength() << "个数" << endl;for (int i = 0; i < array.getLength(); i++){cin >> array[i];}return is;
}
bool operator==(const MyArray &a1, const MyArray& a2) {if (a1.getLength() != a2.getLength()) {return false;}for (int i = 0; i < a1.getLength(); i++){if (a1.m_space[i] != a2.m_space[i])return false;}return true;
}
main.cpp
#include<iostream>
using namespace std;
#include"MyArray.h"int main() {MyArray a1(10);for (int i = 0; i < a1.getLength(); i++){//a1.setData(i, i);a1[i] = i + 10; //space[i]=i+10}cout << "print a1" << endl;for (int i = 0; i < a1.getLength(); i++){//cout<<a1.getData(i)<<" ";cout << a1[i] << " " ;}MyArray a2 = a1;cout << endl<<"print a2" << endl;for (int i = 0; i < a2.getLength(); i++){//cout<<a1.getData(i)<<" ";cout << a2[i] << " " ;}cout << endl;MyArray a3;a3 = a1;cout << endl << "print a3" << endl;for (int i = 0; i < a3.getLength(); i++){//cout << a3.getData(i) << " ";cout << a3[i] << " " ;}cout <<endl<< "<<操作符重载" << endl;cout << a3 << endl;MyArray array1(3);cin >> array1;cout << array1 << endl;if (array1 == a1) {cout << "相等" << endl;}elsecout << "不相等" << endl;if (array1 != a1) {cout << "不相等" << endl;}elsecout << "相等" << endl;
}
C++基础09-货物售卖和MyArray实现相关推荐
- Hive基础09、HQL查询语句
Hive基础09.HQL查询语句 目录 Hive基础08.HQL查询语句 1.基础查询语句 2.数组查询 3.map 4.struct 5.聚合查询语句 HQL查询内容全: 第一部分: Hive查询语 ...
- else列表推导式 if python_python3基础09列表推导式|迭代器|生成器|匿名函数
"pythonic生物人"的第46篇分享. python3匿名函数.推导式.迭代器和生成器用法. 目录 1.匿名函数(lambda)2.推导式(comprehensions) 列表 ...
- Python全栈开发【基础-09】深浅拷贝+while循环
专栏介绍: 本专栏为Python全栈开发系列文章,技术包括Python基础.函数.文件.面向对象.网络编程.并发编程.MySQL数据库.HTML.JavaScript.CSS.JQuery.boots ...
- 【Java】基础09
一.线程 1.1 多线程原理 通过多线程执行时序图 来体现一下多线程程序的执行流程. 代码如下: 自定义线程类: public class MyThread extends Thread{ /** 利 ...
- 阻容感基础09:电感器原理(1)-电感器模型
说在开头:关于中子星(1) 有一个年轻的印度学生来到英国求学,这个小伙子叫钱德拉塞卡,那时候印度能上大学的人不多,他考入英国剑桥大学的三一学院读博士.钱德拉塞卡在船上一直思考关于恒星末日的问题:一个年 ...
- C语言基础09——数据在内存中的存储。整型的存储、大小端讲解、浮点数的存储、杨辉三角、找凶手、猜名次
目录 数据类型 基本内置类型 类型的基本分类 整型在内存中的存储 计算机中整数的三种表示方法:原码.反码.补码 大小端 练习 浮点型在内存中的存储 为什么以下程序输出结果与想象不同? 浮点数存储规则 ...
- 阻容感基础09:电感器原理(4)-趋肤效应和气隙磁芯
说在开头:关于宇宙背景辐射 1909年意大利人马可尼发明了无线电通信后,到了30年代无线电通信慢慢发展成为了一个热门产业,那时大多还只依靠收音机,所以广播行业(比如,讲讲一些小笑话啥的)得到了大力发展 ...
- Elasticsearch基础09——search查询API
es的其他查询方式 term查询的API主要用于结构化的数据,比如keyword或者number或者date.对于text字段不适用term的查询.和之前介绍的分词查询不同,term的查询一般都是精确 ...
- 开关电源基础09:传递函数和波特图
说在开头:关于自然常数e 我们在之前聊到过,毕达哥拉斯学派的弟子希伯索斯发现了无理数(无限不循环小数,不能用分数表示),并为此付出了生命的代价,同时为我们打开了无理数的大门(史称"第一次数学 ...
最新文章
- Attention Is All You Need (transformer)
- linux下交叉编译libusb的方法及编译一个使用了libusb库的test程序的方法
- 30天敏捷结果(15) - 保持一个最佳状态的大脑
- CodeForces - 1543D2 RPD and Rap Sheet (Hard Version)(交互+模拟)
- Easy Tech:什么是I帧、P帧和B帧?
- 单片机中断机制对日常生活的启示_单片机原理部分课后习题解
- wordpress index.php 跳转,wordpress点击内容页跳转到其他url的解决方法
- 16 寸MacBook Pro比14 寸风扇更强大,更耐用
- 接口与事件之图形界面的认证登录
- centos7 mysql升级漏洞5.7.30
- 真分数化简为最简分数(6/8==3/4)
- 采用16线激光雷达和轮式里程计调用cartographer室内融合定位
- 在计算机中无符号整数和有符号整数,无符号整数和有符号整数怎么区分?
- 牛客在线编程101-93 盛水最多的容器
- 怎么释放C盘空间?清理C盘空间的4大方法分享!
- pycharm启动图片修改
- 2.5寸硬盘和3.5寸硬盘的区别和适用场景
- i5-10200h怎么样
- Android 使用腾讯sdk播放视频
- 停车场系统连接服务器,智能停车场汽车牌照识别系统和道闸系统的安装
热门文章
- android 时间应用程序,Android在首次启动时需要更多时间启动应用程序
- verilog找不到模块_工欲善其事,必先利其器 verilog编辑器搭建
- Vue 监视属性 watch
- html文件怎么导出stl文件,3D建模软件导出STL文件的小技巧(一)
- jmeter修改redis_jmeter如何访问redis服务缓存
- Linux——less指令常用操作
- 怎样查看电脑系统版本_用什么软件查维修记录 思域HATCHBACK怎样查询维保记录_汽车事故车查询...
- java 重定向关键字_SpringMVC 转发、重定向
- 怎么加载文件_Java虚拟机从入门到入土之JVM的类加载机制
- php memcached 队列,redis获取所有队列_memcached