以前写的一个简单的货架程序
2006-12-19 03:09
260 查看
过去的c++作业现在把它贴出来
其中主要是用到了拷贝构造函数和赋值运算符重载以及深拷贝这些知识。程序虽然很简单,但还是有一些价值的。
/**//* version: 1.0
author: LSK
date: 2006.10.26
*/
#include <iostream.h>
#include <string.h>
#define max 20
class Gooles //定义Gooles类也就是商品类
...{
private: //私有数据成员日期
char* date;
int length; //私有数据成员日期长度
public:
Gooles(char* string)
...{
if(string)
...{
length = strlen(string);
date = new char[length + 1];
strcpy(date,string);
}
else
...{
length = 0;
date = 0;
}
}
void printGooles()
...{
cout<<"正在向货架中添加,商品的生产日期是: "<<date<<" ";
}
Gooles& operator = (const Gooles& str) //赋值运算符重载
...{
delete date; //防止内存泄漏
length = strlen(str.date);
date = new char[length + 1];
strcpy(date, str.date);
return *this;
}
Gooles(Gooles& str) //拷贝构造函数
...{
length = str.length;
date = new char[length + 1];
strcpy(date, str.date);
}
Gooles() //必须提供Gooles()的无参构造函数
...{
date = 0;
length = 0;
}
~Gooles()
...{
delete[]date;
}
};
class MyStack //定义货架类
...{
private:
Gooles thing[max]; //对象数组作为私有数据成员
int top; //栈顶指针
public:
MyStack()
...{
top = 0;
}
Gooles pop()
...{
top--;
return thing[top];
}
void push(const Gooles& element)
...{
thing[top++] = element; //调用了赋值运算符重载
}
bool ifEmpty()
...{
if(top == 0)
return true;
else
return false;
}
Gooles* getTop()
...{
if(top == 0)
...{
cout<<"货架已空"<<" ";
return 0;
}
else
return &thing[top-1];
}
~MyStack()
...{
}
};
main()
...{
Gooles first("2006.4.3"); //分别实例化货物
Gooles second("2006.5.6");
Gooles thread("2006.6.5");
Gooles fhouth("2006.7.3");
Gooles fifth("2006.9.10");
Gooles sixth("2006.10.10");
MyStack mystack,mystore;
mystack.push(first); //将货物逐个放入缓冲的堆栈中
mystack.push(second);
mystack.push(thread);
mystack.push(fhouth);
mystack.push(fifth);
mystack.push(sixth);
cout<<"货架程序启动.....新货先被添加进货架底部" <<endl;
int i=0;
while(!mystack.ifEmpty())
...{
Gooles temp=mystack.pop(); //调用了拷贝构造函数对对象temp赋值
mystore.push(temp);
Gooles *p=mystore.getTop();
cout<<"进入货架的顺序是:"<<++i <<endl;
p->printGooles();
}
}
其中主要是用到了拷贝构造函数和赋值运算符重载以及深拷贝这些知识。程序虽然很简单,但还是有一些价值的。
/**//* version: 1.0
author: LSK
date: 2006.10.26
*/
#include <iostream.h>
#include <string.h>
#define max 20
class Gooles //定义Gooles类也就是商品类
...{
private: //私有数据成员日期
char* date;
int length; //私有数据成员日期长度
public:
Gooles(char* string)
...{
if(string)
...{
length = strlen(string);
date = new char[length + 1];
strcpy(date,string);
}
else
...{
length = 0;
date = 0;
}
}
void printGooles()
...{
cout<<"正在向货架中添加,商品的生产日期是: "<<date<<" ";
}
Gooles& operator = (const Gooles& str) //赋值运算符重载
...{
delete date; //防止内存泄漏
length = strlen(str.date);
date = new char[length + 1];
strcpy(date, str.date);
return *this;
}
Gooles(Gooles& str) //拷贝构造函数
...{
length = str.length;
date = new char[length + 1];
strcpy(date, str.date);
}
Gooles() //必须提供Gooles()的无参构造函数
...{
date = 0;
length = 0;
}
~Gooles()
...{
delete[]date;
}
};
class MyStack //定义货架类
...{
private:
Gooles thing[max]; //对象数组作为私有数据成员
int top; //栈顶指针
public:
MyStack()
...{
top = 0;
}
Gooles pop()
...{
top--;
return thing[top];
}
void push(const Gooles& element)
...{
thing[top++] = element; //调用了赋值运算符重载
}
bool ifEmpty()
...{
if(top == 0)
return true;
else
return false;
}
Gooles* getTop()
...{
if(top == 0)
...{
cout<<"货架已空"<<" ";
return 0;
}
else
return &thing[top-1];
}
~MyStack()
...{
}
};
main()
...{
Gooles first("2006.4.3"); //分别实例化货物
Gooles second("2006.5.6");
Gooles thread("2006.6.5");
Gooles fhouth("2006.7.3");
Gooles fifth("2006.9.10");
Gooles sixth("2006.10.10");
MyStack mystack,mystore;
mystack.push(first); //将货物逐个放入缓冲的堆栈中
mystack.push(second);
mystack.push(thread);
mystack.push(fhouth);
mystack.push(fifth);
mystack.push(sixth);
cout<<"货架程序启动.....新货先被添加进货架底部" <<endl;
int i=0;
while(!mystack.ifEmpty())
...{
Gooles temp=mystack.pop(); //调用了拷贝构造函数对对象temp赋值
mystore.push(temp);
Gooles *p=mystore.getTop();
cout<<"进入货架的顺序是:"<<++i <<endl;
p->printGooles();
}
}
相关文章推荐
- 自己以前用C#写的简单升级程序源码(比如更新一个安装包或者压缩文件),带进度条,支持续传
- 判断闰年的一个程序。。以前我咋没写的这么简单呢。。
- 以前写过的一个简单的AJAX程序
- 写一个简单的程序,实现添加多本图书信息,和遍历输出图书信息
- linux下qt的安装和一个最简单的小程序hello world
- 我的WCF之旅(1):创建一个简单的WCF程序
- 一个简单木马程序的编写
- 脱离IDE的一个简单C#程序(下)
- Singleton单例模式是最简单的设计模式,它的主要作用是保证在程序执行生命周期中,使用了单类模式的类仅仅能有一个实例对象存在。
- 一个WinForm程序配置信息的简单模型和维护工具——设计说明
- 郭克华手机编程教学视频----我的练习源码(6)实战:实现一个简单的监听程序
- 一个简单的统计学分系统的小程序
- VS2013开发一个简单的asmx接口程序
- 用Python实现一个简单的能够发送带附件的邮件程序的教程
- 网络编程学习笔记二(实现一个基于简单TCP的用户注册程序)
- apache cxf笔记之一个简单的JAX-WS服务程序
- 一个简单的jsp程序
- 一个简单的socket服务器但程序
- 写了一个整人程序,较简单,有兴趣者可以看看
- 一个简单的投票程序