




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第六章 数据笼统类胡昊南京大学计算机系软件所重要内容栈过程式#include using namespace std;/定义栈数据类型#define STACK_SIZE 100struct Stack int top; int bufferSTACK_SIZE;void init(Stack &s) s = -1;bool push(Stack &s, int i) if(s=STACK_SIZE-1) cout “Stack is overflow.n; return false; else s+; s.buffers = i; return true; bool pop(
2、Stack &s, int &i) if(s=-1) cout “Stack is empty.n; return false; else i=s.buffers; s-; return true; 栈过程式运用/运用栈类型数据Stack st; int x;init(st); /对st进展初始化。push(st,12); /把12放进栈。pop(st,x); /把栈顶元素退栈并存入变量x。或,Stack st; int x;/对st进展初始化。st = -1;/把12放进栈。st+;st.bufferst = 12; /把栈顶元素退栈并存入变量x。x = st.buffers
3、t;st-;栈对象式#include using namespace std;/定义栈数据类型#define STACK_SIZE 100class Stack int top; int bufferSTACK_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+; buffertop=i; return true; boo
4、l Stack:pop(int &i) if (top = -1) cout “Stack is empty.n; return false; else i = buffertop; top-; return true; 栈过程式运用/运用栈类型数据Stack st; /自动地去调用st.Stack()对st进展初始化。int x;st.push(12); /把12放进栈st。st.pop(x); /把栈顶元素退栈并存入变量x。st = -1; /Errorst+; /Errorst.bufferst = 12; /ErrorDate类class Date public: void s
5、et(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() coutyear.month.dayendl; private: int year,month,day;Date类成员函数外部定义class Date public: void set(int y, int m, int d); bool is_leap_year(); void print(); private
6、: int year,month,day;void Date:set(int y, int m, int d) year = y; month = m; day = d;bool Date:is_leap_year() return (year%4 = 0 & year%100 != 0) | (year%400=0);void Date:print() coutyear.month.dayendl;TPoint类class TPoint public: void SetPoint(int x, int y); int Xcoord() return X ; int Ycoord()
7、return Y ; void Move(int xOffset, int yOffset) ; private: int X, Y;void TPoint:SetPoint(int x, int y) X=x ; Y=y ;void Tpoint:Move(int xOffset, int yOffset) X+=xOffset; Y+=yOffset;对象的操作class A public: int x; void f() 允许访问:x,y,z,f,g,h private: int y; void g() 允许访问:x,y,z,f,g,h protected: int z; void h(
8、) 允许访问:x,y,z,f,g,h ;.A a;a.x = 1; /OKa.f(); /OKa.y = 1; /Errora.g(); /Errora.z = 1; /Errora.h(); /Error对Date类的对象访问#include using namespace std;#include “Date.hint main() int y, m, d; coutymd; Date some_date; /创建一个Date类的对象d some_date.set(y,m,d); /设置对象d的日期值 some_date.print(); /输出d所表示的日期 if(some_date.i
9、s_leap_year() cout“是闰年n; else cout“不是闰年n; return 0;对象的操作class A public: int x; void f() 允许访问:x,y,z,f,g,h private: int y; void g() 允许访问:x,y,z,f,g,h protected: int z; void h() 允许访问:x,y,z,f,g,h ;.A a;a.x = 1; /OKa.f(); /OKa.y = 1; /Errora.g(); /Errora.z = 1; /Errora.h(); /Error栈链表实现#include #include us
10、ing namespace std;/定义栈数据类型class Stack struct Node int content; Node *next; *top; public: Stack()top=NULL; bool push(int i); bool pop(int &i);bool Stack:push(int i) Node *p=new Node; if (p=NULL) cout content=i; p-next=top; top=p; return true; bool Stack:pop(int &i) if (top = NULL) cout next;
11、i=p-content; delete p; return true; this指针class A public: void g(int i) x = i; private: int x,y,z;A a,b;例如,对于下面的成员函数调用:a.g(1);编译程序将会把它编译成:A:g(&a,1);this指针void func(A *p) .class A public: int x; void f() func(?); void q(int i) x = i; f(); ;A a,b;假设要求:当调用a.f()时,在A:f中调用func(&a);当调用b.f()时,在A:f中调
12、用func(&b);那么,A:f中调用函数func的参数应该如何写呢? 构造函数调用class A.public:A();A(int i);A(char *p);A a1; /调用默许构造函数。也可写成:A a1=A(); 但不能写成:A a1();A a2(1); /调A(int i),也可写成:A a2=A(1); 或 A a2=1; A a3(“abcd); /调A(char *),也可写成:A a3=A(“abcd); 或 A a3=“abcd; A a4; /调用a0、a1、a2、a3的A()A b5=A(),A(1),A(abcd),2,xyz; /调用b0的A()、b1的
13、A(int)、/b2的A(char *)、 /b3的A(int)和b4的A(char *)。A *p1=new A; /调用默许构造函数。A *p2=new A(2); /调A(int i)。A *p3=new A(xyz); /调A(char *)。A *p4 =new A20; /创建动态对象数组时只能调用各对象的默许构造函数。常成员,援用成员和初始化列表class A int x; const int y; int &z=y; /Error,这里是阐明,而不是定义。 public: A() x = 0; /OK y = 1; /Error,y是常量成员,其值不能改动。 ; cla
14、ss A int x; const int y; int& z; public: A(): y(1),z(x) /成员初始化表 x = 0; ;析构函数class Aint x;public:A();A(); /析构函数.;字符串类的定义1#include #include #include using namespace std;class String char *str; public: String() str=NULL; String(const char *p) str = new charstrlen(p)+1; strcpy(str,p); String() delet
15、e str; str = NULL; int length()return strlen(str); char &char_at(int i) if(i= strlen(str) cerr 超出字符串范围!n; exit(-1); return stri; char *get_str() return str; String ©(const char *p) char *p1=new charstrlen(p)+1; strcpy(p1,p); delete str; str = p1; return *this; String ©(const Stri
16、ng &s) return copy(s.str); 字符串类的定义2 String &append(const char *p) char *p1=new charstrlen(str)+strlen(p)+1; strcpy(p1,str); strcat(p1,p); delete str; str = p1; return *this; String &append(const String &s) return append(s.str); int compare(const char *p) return strcmp(str,p); int com
17、pare(const String &s) return strcmp(str,s.str); ; int main() String s1; String s2(abcdefg); s1.copy(xyz); s2.append(s1); for (int i=0; i= a & s2.char_at(i) = z) s2.char_at(i) = s2.char_at(i)-a+A; if (s2pare(ABCDEFGXYZ) = 0) cout OKn; cout s1.get_str() endl s2.get_str() endl; return 0;成员对象初始化
18、class A int m; public: A() m = 0; A(int m1) m = m1; ;class B int n; A a; public: B() n = 0; B(int n1) n = n1; B:B(int n1, int m1): a(m1) n = n1; ;B b1,b2(1), b3(1,2);拷贝构造函数class A int x,y; char *p; public: A(char *str) x = 0; y = 0; p = new charstrlen(str)+1; strcpy(p,str); A() delete p; .; A:A(const A& a)x = a.x;y = a.y;p = new cha
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业机械设计课件
- 农业布局概述课件讲解稿
- 洗浴老人免责协议范文8篇
- 农业安全产的知识培训课件
- 冒号和上引号的课件
- 化工仓库管理安全培训课件
- 化安全培训课件
- 中秋月饼营销解决方案(3篇)
- 内容营销方案分析模板(3篇)
- 大学生创新创业大赛项目计划书
- 农业现代化种植技术培训课件
- 中城汽车(山东)有限公司审计报告
- 大学博士竞赛试题及答案
- 钢结构彩钢瓦施工工艺与技术交底
- 2025版煤矿安全规程宣贯培训课件
- 梁启超家教家风课件
- 第5课 我们说方言教学设计-2025-2026学年小学地方、校本课程浙教版(2024)人·自然·社会
- (2025秋新版)青岛版科学三年级上册全册教案
- 顾客联络服务 人工与智能客户服务协同要求 编制说明
- 2025年全国通信专业技术人员职业水平考试(通信专业实务·传输与接入·无线)历年参考题库含答案详解(5套)
- 医院汽车管理办法
评论
0/150
提交评论