CH2. ARRAYS.ppt
《CH2. ARRAYS.ppt》由会员分享,可在线阅读,更多相关《CH2. ARRAYS.ppt(72页珍藏版)》请在麦多课文档分享上搜索。
1、CH2. ARRAYS,10/10/2018,2,2.1 Abstract Data Types and the C+ Class,2.1.1 C+ Class : represents an ADT Consists of four components class name data members member functions levels of program access public : anywhere private : within its (friend) class a function protected : within its class friend,10/1
2、0/2018,3,2.1 Abstract Data Types and the C+ Class(Cont),#ifndef RECTANGLE_H #define RECTANGLE_H / In the header file Rectangle.h class Rectangle public: / the following members are public / The next four members are member functions Rectangle(); / constructor Rectangle(); / destructor int GetHeight(
3、); / returns the height of the rectangle int GetWidth(); / returns the width of the rectangle private: / the following members are private / the following members are data members int x1, y1, h, w; / (x1, y1) are the coordinates of the bottom left corner of the rectangle / w is the width of the rect
4、angle; h is the height of the rectangle ; #endif,Program 2.1 : Definition of the C+ class Rectangle,10/10/2018,4,2.1 Abstract Data Types and the C+ Class(Cont),2.1.2 Data Abstraction and Encapsulation in C+ Data encapsulation of C+ class all data members are private (or protected) external access to
5、 data members are by member functions Separation of specification and implementation of member functions specification (function prototype) name of functions type of function arguments type of function result Implementation placed in a source file of the same name can be included inside its class de
6、finition,10/10/2018,5,2.1 Abstract Data Types and the C+ Class(Cont),/ In the source file Rectangle.C #include “Rectangle.h“ / The prefix “Rectangle:“ identifies GetHeight() and / GetWidth() as member functions / belonging to class Rectangle. It is required because the member functions / are impleme
7、nted outside their class definition int Rectangle:GetHeight() return h; int Rectangle:GetWidth() return w;,Program 2.2 : Implementation of operations on Rectangle,10/10/2018,6,2.1 Abstract Data Types and the C+ Class(Cont),2.1.3 Declaring class objects in the same way as variables Invoking member fu
8、nctions using component selection operators dot(.) : direct selection arrow : indirect selection through a pointer,10/10/2018,7,2.1 Abstract Data Types and the C+ Class(Cont),/ In a source file main.C #include #include “Rectangle.h“ main() Rectangle r, s; / r and s are objects of class Rectangle Rec
9、tangle *t = ,Program 2.3 : A C+ code fragment demonstrating how Rectangle objects are declared and member functions invoked,10/10/2018,8,2.1 Abstract Data Types and the C+ Class(Cont),2.1.4 Special Class Operations Constructor a member function which initializes data members of an object If provided
10、 for a class, automatically executed when an object of that class is created must be public the name must be identical to the name of the class must not specify a return type or return a value,Rectangle:Rectangle(int x, int y, int height, int width) x1=x; y1=y; h=height; w=width; ,Program 2.4 : Defi
11、nition of a constructor for Rectangle,10/10/2018,9,2.1 Abstract Data Types and the C+ Class(Cont),initialize Rectangle object using constructor Rectangle r(1, 3, 6, 6); Rectangle *s = new Rectangle(0, 0, 3, 4); initialize using a default constructor Rectangle r;,Rectangle:Rectangle (int x=0, int y=0
12、, int height=0, int width=0) : x1 (x), y1(y), h(height), w(width) ,Program 2.5 : A default constructor,10/10/2018,10,2.1 Abstract Data Types and the C+ Class(Cont),Destructor a member function which deletes data members automatically invoked when a class object goes out of scope or is deleted must b
13、e public its class name prefixed with if a data member is a pointer, only the space of the pointer is returned Operator overloading polymorphism : same operator for different situations for example, algorithm comparing two floats is different from algorithm comparing two ints,10/10/2018,11,2.1 Abstr
14、act Data Types and the C+ Class(Cont),int Rectangle:operator=(const Rectangle ,Program 2.6 : Overloading operator = for class Rectangle,10/10/2018,12,2.1 Abstract Data Types and the C+ Class(Cont),this represents a pointer to the object that invoked a member function *this represents the object,10/1
15、0/2018,13,2.1 Abstract Data Types and the C+ Class(Cont),Ostream ,Program 2.7 : Overloading operator for class Rectangle,10/10/2018,14,2.1 Abstract Data Types and the C+ Class(Cont),2.1.5 Miscellaneous Topics static class data member a global variable for its class there is only one copy of a static
16、 data member and all class objects share it declaration does not constitute a definition,10/10/2018,15,2.1 Abstract Data Types and the C+ Class(Cont),2.1.6 ADTs and C+ classes They are similar Some operators in C+, when overloaded for user defined ADTs, are declared outside the C+ class definition o
17、f the ADT,10/10/2018,16,2.1 Abstract Data Types and the C+ Class(Cont),class NaturalNumber / An ordered subrange of the integers starting at zero and ending at / the maximum integer (MAXINT) on the computer public: NaturalNumber Zero(); / returns 0 Boolean IsZero(); / if *this is 0, return TRUE; oth
18、erwise, return FALSE NaturalNumber Add(NaturalNumber y); / return the smaller of *this+y and MAXINT; Boolean Equal(NaturalNumber y); / return TRUE if *this=y; otherwise return FALSE NaturalNumber Successor(); / if *this is MAXINT return MAXINT; otherwise return *this+1 NaturalNumber Substract(Natura
19、lNumber y); / if *thisy, return 0; otherwise return *this-y ;,ADT 1.2 : Abstract data type NaturalNumber,10/10/2018,17,2.2 Array As Abstract Data Type,Array a set of pairs ADT for array provides operations retrieves a value stores a value C+ Array index starts at 0 C+ does not check bounds for an ar
20、ray index example float examplen; ith element: examplei and *(example+i),10/10/2018,18,2.2 Array As Abstract Data Type(Cont),Class GeneralArray / objects: A set of pairs where for each value of index / in IndexSet there is a value of type float. / IndexSet is a finite ordered set of one or more dime
21、nsions, / for example, 0, ., n-1 for one dimension, / (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2) for two / dimensions, etc. public: GeneralArray(int j, RangeList list, float initValue = defaultValue); / The constructor GeneralArray creates a j dimensional array / of floats; the ra
22、nge of the kth dimension is given by the / kth element of list. For each index i in the index set, insert / into the array.,10/10/2018,19,2.2 Array As Abstract Data Type(Cont),float Retrieve(index i); / if (i is in the index set of the array) return the float / associated with i in the array; else s
23、ignal an error. void Store(index i, float x); / if (i is in the index set of the array) delete any pair of the / form present in the array and insert the new pair / ; else signal an error. ; / end of GeneralArray,ADT 2.1 : Abstract data type GeneralArray,10/10/2018,20,2.3 Polynomial Abstract Data Ty
24、pe,Ordered (or linear) list days of the week : (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) years Switzerland fought in WWII : () Operations on lists (a0, a1, ., an-1) : find the length, n, of the list read the list from left to right (or reverse) retrieve the ith element, 0in st
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CH2ARRAYSPPT
