下载此文档

第六章 数据抽象-类【精品ppt】.ppt


文档分类:IT计算机 | 页数:约25页 举报非法文档有奖
1/25
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/25 下载此文档
文档列表 文档介绍
1第六章数据抽象-类胡昊南京大学计算机系软件所 2重要内容 3栈-过程式#include < iostream > using namespace std; // 定义栈数据类型#define STACK_SIZE 100 struct Stack { int top; int buffer[STACK_SIZE ]; }; void init(Stack &s) { = -1; } bool push(Stack &s, int i) { if( ==STACK_SIZE-1) { cout << “ Stack is overflow.\n ”; return false; } else { ++; [ ] = i; return true; } } bool pop(Stack &s, int &i) { if( ==-1) { cout << “ Stack is empty.\n ”; return false; } else { i= [ ]; --; return true; } }4栈-过程式(使用) // 使用栈类型数据 Stack st ; int x; init(st ); // 对 st 进行初始化。 push(st,12); // 把 12 放进栈。 pop(st,x ); // 把栈顶元素退栈并存入变量 x。或, Stack st ; int x; // 对 st 进行初始化。 = -1; // 把 12 放进栈。 ++; [ ] = 12; // 把栈顶元素退栈并存入变量 x。 x = [ ]; --; 5栈-对象式#include < iostream > using namespace std; // 定义栈数据类型#define STACK_SIZE 100 class Stack { int top; int buffer[STACK_SIZE ]; public: Stack(){top =-1;} bool push(int i); bool pop(int &i); }; bool Stack::push(int i) { if (top==STACK_SIZE-1) { cout << “ Stack is overflow.\n ”; return false; } else { top++; buffer[top ]=i; return true; } } bool Stack::pop(int &i) { if (top == -1) { cout << “ Stack is empty.\n ”; return false; } else { i = buffer[top ]; top--; return true; } }6栈-过程式(使用) // 使用栈类型数据 Stack st ; // 自动地去调用 () 对 st 进行初始化。 int x; (12); // 把 12 放进栈 st 。 (x ); // 把栈顶元素退栈并存入变量 x。 = -1; //Error ++; //Error [ ] = 12; //Error 7 Date 类 class Date { public: void set(int y, int m, int d) { year = y; month = m; day = d; } bool is_leap_year () { return (year%4 == 0 && year%100 != 0) || (year%400==0); } void print() { cout <<year<<"."<<month<<"."<<day<< endl ; } private: int year,month,day ; };8 Date 类(成员函数外部定义) class Date { public:

第六章 数据抽象-类【精品ppt】 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数25
  • 收藏数0 收藏
  • 顶次数0
  • 上传人薄荷牛奶
  • 文件大小0 KB
  • 时间2016-05-20