下载此文档

C 运算符重载实例.doc


文档分类:IT计算机 | 页数:约10页 举报非法文档有奖
1/10
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/10 下载此文档
文档列表 文档介绍
C++:我很帅;但是我的缺点是:我帅的不明显。什么是幸福?幸福就是猫吃鱼,狗吃肉,奥特曼打小怪兽!令堂可是令尊表姐?我是胖人,不是粗人。
示例程序代码如下
#include ""
#include <>
class stack
{
private:
int *sp, top, max;
void inflate();
public:
stack(int size = 10)
{
sp = (int *)malloc(sizeof(int) * size);
max = size;
top = 0;
}
int pop();
void push(int value);
stack & operator=(stack & rightValue);
};
//栈的容量增倍
void stack::inflate()
{
int index, *tp;
tp = (int *)malloc(sizeof(int) * max * 2);
for(index = 0; index < top; index++)
{
tp[index] = sp[index];
}
max += max;
free(sp);
sp = tp;
}
//出栈
int stack::pop()
{
if(top <= 0)
throw 1;
return sp[--top];
}
//入栈
void stack::push(int value)
{
if(top == max)
inflate();
sp[top++] = value;
}
//赋值函数
stack & stack::operator=(stack & rightValue)
{
top = ;
max = ;
sp = (int *)malloc(sizeof(int) * max);
for(int index = 0; index < max; index++)
{
sp[index] = [index];
}
return *this;
}
void main()
{
stack x(100), y, z;
z = y = x;
}
这里要注意的是赋值函数的返回值是stack &,这是为了实现链式表达式,如z = y = x;。
这一点可见《高质量c/c++编程》(林锐)一书。
2.[]的重载
语法:值类型 operator[](一个形参) 函数体
示例程序如下:
#include ""
#include <>
#include <iostream>
using namespace std;
class Array
{
private:
int *a, len;
void inflate()
{
cout << "the array is inflating..." << endl

C 运算符重载实例 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数10
  • 收藏数0 收藏
  • 顶次数0
  • 上传人miao19720107
  • 文件大小27 KB
  • 时间2021-04-13