栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

学完C再学C++(5) 类和动态内存分配

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学完C再学C++(5) 类和动态内存分配



#include 
#include 
#include
#include "study.h"//里面写的#define DAY1....


#ifdef DAY6
//课后题3,栈,
#ifndef Hfile
//头文件部分

typedef unsigned long unlong;

using namespace std;
class stack
{
private:
	enum{ MAX = 10 };
	unlong* item;
	int size;
	int top;
public:
	stack(int n = MAX);
	//防止构造时生成默认的,默认的会直接复制对象,里面指针还是没变
	stack(const stack& st);
	~stack();
	bool isempty()const;
	bool isfull()const;
	bool push(const unlong & data);
	bool pop(unlong& data);
	void show();
	//防止直接相等
	stack & operator=(const stack& st);	
};
#endif

stack::stack(int n)
{
	if (n > 10)n = 10;
	size = 0;
	item = new unlong[n];
	top = n;
	std::cout << "created a stack Maxsize" << top << endl;
}
stack::stack(const stack& st)
{
	top = st.top;
	size = st.size;
	item = new unlong[top];
	memcpy(item, st.item, sizeof(unlong) * st.top);
}
stack::~stack()
{
	delete [] item;
}
//1是空的,0是非空
bool stack::isempty()const
{
	//for (int i = 0; i < top; i++)
	//{
	//	if (item[i] != 0)return 0;
	//}
	if (size == 0)return 1;
	return 0;
}
//1是满的,0不满
bool stack::isfull()const
{
	//for (int i = 0; i < top; i++)
	//{
	//	if (item[i] == 0)return 0;
	//}
	if (size == top)return 1;
	else return 0;
}
//1是成功,0是满的的推不入
bool stack::push(const unlong& data)
{
	if (isfull())
	{
		std::cout << "full and can not to push" << endl;
		return 0;
	}
	item[size]=data;
	size++;
	std::cout << "push ;" << data << endl;
}
//1是成功,0是空的,推不出
bool stack::pop(unlong& data)
{
	if (isempty())
	{
		std::cout << "empty and can not to pop" << endl;
		return 0;
	}
	data=item[size-1];
	item[size - 1] = 0;
	size--;
	std::cout<<"pop ;"<< data< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942703.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号