类和对象Chapter5ClassesandObjects.ppt
《类和对象Chapter5ClassesandObjects.ppt》由会员分享,可在线阅读,更多相关《类和对象Chapter5ClassesandObjects.ppt(27页珍藏版)》请在三一文库上搜索。
1、Chapter 5 Classes and Objects,5.1 Specifying a class 5.2 Defining Member Functions 5.3 Memory Allocation and Static Members 5.4 Passing Objects to/from Functions 5.5 Friendship,2, 5.1 Specifying a Class,Object(对象) Abstraction of entities State + Behavior Class (类) A user-defined data type that decla
2、re objects Variables + Functions,radius, getArea(),Declaring a Class,3,class class_name private: variable declarations; function declarations; public: variable declarations; function declarations; ;,class item int number; float cost; public: void getdata(int a, float b); /function declaration void p
3、utdata(void); ;,No initial value in declaration!,The following members are accessible outside the class,The following members are NOT accessible outside the class,private:,4,Creating Objects,The syntax to create an object ClassName variableName; For example, item x; item x, y, z;,Memory for x is cre
4、ated,Data type vs. Variable Class vs. Object,5,Accessing Objects,To access the members of a class/object Data fields and functions objectName.dataField -references a data field in the object. objectName.function(arguments) -invokes a function on the object.,Data hiding in class,6,class xyz int x; in
5、t y; public: int z; ; int main() xyz p; p.x = 0; /error, x is private p.z =10; /OK, z is public ,Private Member Functions,Normally: data in private; functions in public Some functions in private sections Can only be called by member function of its class To protect data from modifications by outside
6、rs,7,class sample int m; void read(void); public: void update(void); void write(void); ;,8,Getter and Setter,For users/clients of a class to retrieve and modify a data field encapsulated by “private” Getter/Accessor A function usually named getXxx: Setter/Mutator A function usually named setXxx, e.g
7、.:,returnType getPropertyName(),bool isPropertyName(),void setPropertyName(dataType propertyValue),9,A Simple Circle Class,To demonstrate creating objects, accessing data, and using functions,TestCircle,Run,Pointers to Members,A class member pointer can be declared using the operator :* with the cla
8、ss name. For example:,10,class A private: int m; public: void show(); ; int A:* ip = , 5.2 Defining Member Functions,11,return-type class-name : function-name (parameters) Function body ,Identity label,Scope resolution operation,12,Inline Declaration,A member function can be implemented inside a cla
9、ss declaration automatically an inline function,class A public: A() / do something; double f1() / return a number double f2(); ;,Inline function,Inline function,Regular function,double A:f2() / return a number ,inline double A:f2(),Inline function,13,Separating Declaration from Implementation,Simila
10、r to the definition of function Declaration(声明): In classname.h file, conventionally Simply lists all the data fields, constructor prototypes, and the function prototypes. Implementation(实现): In classname .cpp file, conventionally Implements the constructors and functions.,Circle.h,Run,Circle.cpp,#i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 对象 Chapter5ClassesandObjects
链接地址:https://www.31doc.com/p-2635745.html