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转载请标明出处.