六章数据抽象-类.ppt

上传人:本田雅阁 文档编号:3185725 上传时间:2019-07-22 格式:PPT 页数:25 大小:166.54KB
返回 下载 相关 举报
六章数据抽象-类.ppt_第1页
第1页 / 共25页
六章数据抽象-类.ppt_第2页
第2页 / 共25页
六章数据抽象-类.ppt_第3页
第3页 / 共25页
六章数据抽象-类.ppt_第4页
第4页 / 共25页
六章数据抽象-类.ppt_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《六章数据抽象-类.ppt》由会员分享,可在线阅读,更多相关《六章数据抽象-类.ppt(25页珍藏版)》请在三一文库上搜索。

1、1,第六章 数据抽象类,胡昊 南京大学计算机系软件所,2,重要内容,3,栈过程式,#include using namespace std; /定义栈数据类型 #define STACK_SIZE 100 struct Stack int top; int bufferSTACK_SIZE; ; void init(Stack ,bool push(Stack ,4,栈过程式(使用),/使用栈类型数据 Stack st; int x; init(st); /对st进行初始化。 push(st,12); /把12放进栈。 pop(st,x); /把栈顶元素退栈并存入变量x。 或,,Stack s

2、t; int x; /对st进行初始化。 st.top = -1; /把12放进栈。 st.top+; st.bufferst.top = 12; /把栈顶元素退栈并存入变量x。 x = st.bufferst.top; st.top-;,5,栈对象式,#include using namespace std; /定义栈数据类型 #define STACK_SIZE 100 class Stack int top; int bufferSTACK_SIZE; public: Stack()top=-1; bool push(int i); bool pop(int ,bool Stack:pu

3、sh(int i) if (top=STACK_SIZE-1) cout “Stack is overflow.n”; return false; else top+; buffertop=i; return true; bool Stack:pop(int ,6,栈过程式(使用),/使用栈类型数据 Stack st; /自动地去调用st.Stack()对st进行初始化。 int x; st.push(12); /把12放进栈st。 st.pop(x); /把栈顶元素退栈并存入变量x。,st.top = -1; /Error st.top+; /Error st.bufferst.top =

4、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 ,8,Date类(成员函数外部定义),class Date public: void set(int y, int m, int d); bool is_leap_year(); void print(); private: int year,month,day; ; void Date:set(int y, int m,

5、int d) year = y; month = m; day = d; bool Date:is_leap_year() return (year%4 = 0 ,9,TPoint类,class TPoint public: void SetPoint(int x, int y); int Xcoord() return X ; int Ycoord() return Y ; void Move(int xOffset, int yOffset) ; private: int X, Y; ; void TPoint:SetPoint(int x, int y) X=x ; Y=y ; void

6、 Tpoint:Move(int xOffset, int yOffset) X+=xOffset; Y+=yOffset; ,10,对象的操作,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; /OK a.f(); /OK a.y = 1; /Error a.g(); /Error a.z = 1; /Error a.h(); /Error,1

7、1,对Date类的对象访问,#include using namespace std; #include “Date.h” int 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.is_leap_year() cout“是闰年n”; else cout“不是闰年n”; return 0; ;,12,对象的操作,class A public: int x; void f()

8、允许访问: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; /OK a.f(); /OK a.y = 1; /Error a.g(); /Error a.z = 1; /Error a.h(); /Error,13,栈链表实现,#include #include using namespace std; /定义栈数据类型 class Stack struct Node int content; Node *next;

9、*top; public: Stack()top=NULL; bool push(int i); bool pop(int ,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 ,14,this指针,class A public: void g(int i) x = i; private: int x,y,z; ; A a,b; 例如,对于下面的成员函数调用: a.g(1);编译程序将会把它编译成: A:g(

10、,15,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:f中调用函数func的参数应该如何写呢?,16,构造函数调用,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

11、=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的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; /创建动态对象数组时只能调用各

12、对象的默认构造函数。,17,常成员,引用成员和初始化列表,class A int x; const int y; int ,class A int x; const int y; int,18,析构函数,class A int x; public: A(); A(); /析构函数 ;,19,字符串类的定义(1),#include #include #include using namespace std; class String char *str; public: String() str=NULL; String(const char *p) str = new charstrlen(p

13、)+1; strcpy(str,p); String() delete str; str = NULL; int length() return strlen(str);,char ,20,字符串类的定义(2),String ,int main() String s1; String s2(“abcdefg“); s1.copy(“xyz“); s2.append(s1); for (int i=0; i= a ,21,成员对象初始化,class A int m; public: A() m = 0; A(int m1) m = m1; ; class B int n; A a; public

14、: 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);,22,拷贝构造函数,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 ,23,拷贝构造函数,class A ; class B int z; A a; public: B(); B(const B,24,静态成员,class A static int obj_count; . public: A() obj_count+; A() obj_count-; static int get_num_of_objects() return obj_count; . ; int A:obj_count=0; cout A:get_num_of_objects() endl;,25,友元,class Point public: Point(double xx, double yy) x=xx; y=yy; void Getxy(); friend double Distance(Point ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1