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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/567884.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle+导入emp,Oracle数据库导入导出emp文件

首先询问对方数据库的表空间名称和大小&#xff0c;然后在你的oracle中建立相应表空间&#xff0c;最后使用命令导入、导出数据。补充&#xff1a;1.要新建一个数据库&#xff1b;Oracle数据导入导出imp/exp就相当于oracle数据还原与备份。exp命令可以把数据从远程数据库服务器…

C++基础10-类和对象之友元函数

采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口。但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员, 这时可以将这些函数定义为该 函数的友元函数。除了友元函…

linux进程号为一,一步步探究linux进程中的用户ID

转载请注明来源chengyaogen.blog.chinaunix.net一、进程与权限A.进程时Linux/Unix操作系统中最重要的抽象之一B.进程是一个处于执行期的程序(目标代码存储在某种介质上)A process is a program(object code stored on some media) in the midst ofexecution.而进…

C++基础11-类和对象之操作符重载1

总结&#xff1a; 1、运算符重载的本质是函数重载 2、运算符重载可分为成员函数重载和全局函数重载(差一个参数) 3、运算符重载函数的参数至少有一个是类对象&#xff08;或类对象的引用&#xff09; 4、不可以被重载的操作符有&#xff1a;成员选择符(.) 成员对象选择符(.*) …

linux下搜狗安装目录,Linux安装搜狗输入法

本系统使用Ubuntu16.04LTS安装中文输入法Ubuntu17.04中支持IBUS&#xff0c;fcitx等输入法框架。分别介绍两种框架下的中文输入法安装&#xff1a;1.IBUS框架下的拼音输入法1.1在Ubuntu Software搜索“pinyin”&#xff0c;安装列表中第一个&#xff1b;1.2在系统设置-Language…

在Linux系统下实现进程,Linux进程学习(一)之Linux进程的基本知识和实现

最近一周学习了Linux 进程编程的知识&#xff0c;现对其总结如下。在第一部分中我们先对进程的基本概念以及在Linux 中是如何来现实进程的进行介绍Tiger-John说明 &#xff1a;许多人在学习中只注重如何编程&#xff0c;却忘了注重原理,不去深究其基本原理。其实操作系统的原理…

C++基础11-类和对象之操作符重载2

总结&#xff1a; 1、等号操作符重载和拷贝构造函数重载一般用在数据成员中需要单独在堆区开辟内存时(指针) 2、new&#xff0c;delete重载内部还是使用malloc和free 3、逗号表达式(,)、或者(||)&#xff0c;且(&&)&#xff0c;条件表达式(?:)具有短路功能。 但…

linux照片备份软件,Linux、Unix上5个惊艳开源备份软件

Linux和类Unix系统上5个惊艳的开源备份软件&#xff1a;Bacula、Amanda、Backupninja、Backuppc和UrBackup&#xff0c;这些都是既可以使用在Linux上也可以使用在Unix上面&#xff0c;他们的优点就是性能稳定&#xff0c;使用灵活。一个好的备份计划是非常必要的&#xff0c;这…

C/C++混淆点-运算符短路

主要内容如下&#xff1a; 按照C/C标准 1.9.18 节的说明&#xff0c;||,&&,?:三目运算符和逗号运算符采用短路运算&#xff0c;第一个表达式之后作为一个运算顺序点。 1 a && b; 2 a || b; 3 a ? b : c; 4 a, b; 短路运算仅对内置的行为有效&#xff0c;如…

C++基础12-类和对象之操作符重载-string练习

总结&#xff1a; 1、等号操作符重载注意事项&#xff1a; &#xff08;1&#xff09;防止自身赋值 &#xff08;2&#xff09;先将自身的额外开辟的空间回收掉 &#xff08;3&#xff09;执行深拷贝 2、注意函数的返回引用或者元素&#xff1a;如果需要连续使用 …

linux中类似findfirst的函数,findfirst函数的用法

函数名称: findfirst函数原型: int findfirst(char *fname,struct ffblk *ptr,int attrib)函数功能: 寻找与fname相匹配的第一个文件名称函数返回:参数说明: ptr-保存查找到的文件信息所属文件: #include #include int main(){struct ffblk ffblk;int d…

C/C++混淆点-逗号运算符

在C中&#xff0c;逗号是很常用的。作为一个运算符它虽然不常用&#xff0c;但我们也应该学会它的用法。 1.如&#xff1a;a3*4,4*5,5*6; 由于“”的优先级高于“&#xff0c;”&#xff0c;所以程序从左向右运行&#xff0c;即先运行a3*4。之后的4*5&#xff0c;5*6仅仅运行&a…

linux 添加重定向域名,Linux系统中Nginx的安装并进行域名认证和重定向

Linux系统中Nginx的安装并进行域名认证和重定向本文主要介绍Linux系统中Nginx的安装并进行域名认证和重定向&#xff0c;希望通过本知识点的讲解对大家今后的学习和工作有所帮助&#xff0c;下面进行具体介绍&#xff1a;12.6 Nginx安装cd /usr/local/srcwget http://nginx.org…

C/C++混淆点-转义字符

从表中可以看出&#xff0c;在C语言中有三种转义字符&#xff0c;它们是&#xff1a;一般转义字符、八进制转义字符和十六进制转义字符。 所有的转义字符只代表一个字符 1. 一般转义字符 这种转义字符&#xff0c;虽然在形式上由两个字符组成&#xff0c;但只代表一个字符。…

linux如何安装python环境变量中,Windows和linux环境下python安装及环境变量的配置

安装包的安装无需双版本存在情况下安装1)下载源码包2)解压源码包并进入文件夹./configure ; make && make install3)使用python -V查看是否安装成功&#xff0c;安装成功会显示python版本信息需要双版本安装python版本1)同上&#xff0c;需要下载3.6源码包2)解压源码包…

C/C++混淆点-strcat和strcpy区别

一、原因分析 假设&#xff1a; char * strNULL; strnew char[11];你想为字符串str开辟一个存储十个字符的内存空间&#xff0c;然后你现在有两个字符串&#xff1a;char * c1"abc"和char * c2"123";你想将这两个字符串都拼接在str字符串中&#xff0c;你…

linux有读EC RAM的工具吗,Step to UEFI (179)Shell下 EC Ram 读取工具

最近工作需要在 UEFI Shell 下Check EC Ram 的设定&#xff0c;发现手上只有 Windows 下的读取工具(RW Everything)。于是研究了一下如何在Shell 读取 EC Ram。根据【参考1】读取的流程如下&#xff1a;Port 66 CommandsThere are also some EC commands that use ports 0x66 …

C++基础13-类和对象之继承1

在 C中可重用性(software reusability)是通过继承(inheritance)这一机制来实现的。 如果没有掌握继承性,就没有掌握类与对象的精华。 总结&#xff1a; 1、只要是父类中的private成员&#xff0c;不管是什么继承方式&#xff0c;儿子都访问不了&#xff0c;但它是存在在儿子之…

线程锁定CPU linux,linux 线程与CPU绑定

看到很多程序都是根据CPU个数来创建线程个数&#xff0c;当时很不理解他们之间的关系&#xff0c;请教了项目组的同事后才有了大致了解。1. 相关系统函数下面的函数可以通过man命令查询到。SYNOPSIS#define _GNU_SOURCE#include int pthread_setaffinity_np(pthread_t thread, …

C++基础13-类和对象之继承2

总结&#xff1a; 1、子类对象可以当做父类对象使用 2、子类对象可以直接赋值给父类对象 3、子类对象能够直接初始化父类对象 4、父类指针可以直接指向子类对象 5、凡是继承过来的属性和函数都可以在子类中用this-> 进行访问 6、默认构造函数并不会初始化数据成员 7、如果…