《The C++ Programming Language》课程教学资源(PPT课件讲稿)Lecture 04 Object-Based Programming

The c ++ Programming Language Lecture 4 Object-Based Programming
The C++ Programming Language Lecture 4: Object-Based Programming

Basic Concepts and Syntaxes
Basic Concepts and Syntaxes

You have no class Class object c++C u Class- user defined data type More operations not only operators but also functions a Object-an instance or running entity of certain class Class vs, object Class a static concept object, a dynamic concept One class definition multiple object entities
Class & Object ◼ Class – user defined data type ◼ More operations, not only operators but also functions ◼ Object – an instance or running entity of certain class ◼ Class vs. Object ◼ Class, a static concept; Object, a dynamic concept ◼ One class definition, multiple object entities

Concepts about class u Class components From the view of Type Data members ( Built-in, user-defined), or Attributes Member functions(function, operator), or Methods, Behaviors From the view of role Public Interface Private Implementation Encapsulation Class users only access public interface, need not be aware of the internal details of implementation No matter how dramatically the internal implementation changed client codes need not be modified if interface is fixed ■ struct in c++ a Taken as a special type of class which has no member functions, and all data members are public
Concepts about class ◼ Class components ◼ From the view of Type – Data members (Built-in, user-defined), or Attributes – Member functions (function, operator), or Methods, Behaviors ◼ From the view of Role – Public Interface – Private Implementation ◼ Encapsulation ◼ Class users only access public interface, need not be aware of the internal details of implementation ◼ No matter how dramatically the internal implementation changed, client codes need not be modified if interface is fixed ◼ struct in C++ ◼ Taken as a special type of class, which has no member functions, and all data members are public

Design our own classes A simple start-a string stack class u An important LIFo data structure Required operations Push- add an element to the stack Pop-take an element out from the top of stack Full -query whether the stack is full Empty -query whether the stack is empty a Size-query the number of elements in the stack Peek -access the element on the top of the stack, but not to pop it out
Design our own classes ◼ A simple start – a string stack class ◼ An important LIFO data structure ◼ Required operations: ◼ Push – add an element to the stack ◼ Pop – take an element out from the top of stack ◼ Full – query whether the stack is full ◼ Empty – query whether the stack is empty ◼ Size – query the number of elements in the stack ◼ Peek – access the element on the top of the stack, but not to pop it out

Class Declaration Definition class STack l/class declaration lf class definition body public: laccess control bool push(const string&); //member function declaration bool pop(string &) bool peek(string &) bool empty(; bool fullO: line automatically int size(i return m_stack size(; ) //declaration definition(in Class) private: ∥/ access control vector m stack; //data member ; Class scope resolution /never to forget the semicolon bool STack; empty /member function definition (out i return m stack. empty 0; y of Class)
Class Declaration & Definition class CStack { public: bool push(const string&); bool pop(string &); bool peek(string &); bool empty(); bool full(); int size() { return m_stack.size(); } private: vector m_stack; }; bool CStack::empty() { return m_stack.empty(); } //class declaration //{} class definition body //access control //member function declaration //declaration & definition (in Class) // access control //data member //never to forget the semicolon //member function definition (out of Class) inline automatically Class scope resolution

Class Declaration Definition(cont,) u Question: If class a has a pointer data member that pointing to class B, and vice versa, how to define class a and B? Answer: Using Forward Declaration class A. class B: class A public B* m pb; } class B public A m_ pa
Class Declaration & Definition (cont.) ◼ Question: If class A has a pointer data member that pointing to class B, and vice versa, how to define class A and B? ◼ Answer: Using Forward Declaration class A; class B; class A { public: B* m_pb; }; class B { public: A* m_pa; };

Class Declaration Definition(cont,) Forward Declaration only telling the name to the compiler Try your best to minimize the compilation dependencies between files The rules is, if the class declaration could work never to indlude the class definition For pointers, references, and using objects as parameters and eturn value in function declaration, we only need the class declaration STack pstack =0 void funCA(const STack&) a STack funcB(CStack) We could define a real class object or use the class members only after we know the class definition a STack stack int s=stack, size The declaration version of --
Class Declaration & Definition (cont.) ◼ Forward Declaration only telling the name to the compiler ◼ Try your best to minimize the compilation dependencies between files ◼ The rules is, if the class declaration could work, never to include the class definition ◼ For pointers, references, and using objects as parameters and return value in function declaration, we only need the class declaration ◼ CStack* pstack = 0; ◼ void funcA(const CStack&); ◼ CStack funcB(CStack); ◼ We could define a real class object or use the class members, only after we know the class definition ◼ CStack stack; ◼ int s = stack.size(); ◼ The declaration version of --

Access control u Controlling how the member functions and data members could be accessed Public- could be accessed outside the class Protected-could only be accessed by those classes deriving from it, or by friend classes and functions Private- could only be accessed inner this class or by friend classes and functions Suggestion: Avoid exposing the data members in the public interface, Use accessing functions instead
Access control ◼ Controlling how the member functions and data members could be accessed ◼ Public – could be accessed outside the class ◼ Protected – could only be accessed by those classes deriving from it, or by friend classes and functions ◼ Private – could only be accessed inner this class, or by friend classes and functions ◼ Suggestion: Avoid exposing the data members in the public interface, Use accessing functions instead

Access control(cont class X class y public public int mf10 int m1. int mf20[ return mf30; 1 int get m20i return m2; 1 private: void set m2(int xim2=X;1 int mf30 private: int m2. }x; }y; void main( void maino x.mf10; llyes y. m1++; //Not suggested! Direct Xmf30;∥Eror! Private access data members member can't be accessed out yset_m2(1);/Suggested! The of the class data mem ber is accessed under control
Access control (cont.) class X { public: int mf1(); int mf2() { return mf3(); } private: int mf3(); } x; void main() { x.mf1(); //yes x.mf3(); //Error! Private member can’t be accessed out of the class } class Y { public: int _m1; int get_m2() { return _m2;} void set_m2(int x) { _m2 = x;} private: int _m2; } y; void main() { y._m1++; //Not suggested! Direct access data members y.set_m2(1); //Suggested! The data member is accessed under control }
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 北京航空航天大学:SimplyDroid - Efficient Event Sequence Simplification for Android Application.pptx
- 南京大学:《计算机图形学》课程教学资源(PPT课件讲稿)第7讲 图元填充与裁剪算法.pptx
- 香港浸会大学:Introduction to Linux and PC Cluster.ppt
- 《C语言程序设计》课程教学资源(PPT课件讲稿)第8章 结构体、共用体与枚举类型.ppt
- 《计算机系统安全》课程教学资源(PPT课件讲稿)第二章 黑客常用的系统攻击方法.ppt
- 上海交通大学:《挖掘海量数据集 Mining Massive Datasets》课程教学资源(PPT讲稿)Lecture 06 搜索引擎 Search Engines.ppt
- 《Introduction to Java Programming》课程PPT教学课件(Sixth Edition)Chapter 16 Applets and Multimedia.ppt
- 《计算机组装与维护》课程教学资源(PPT课件讲稿)第9章 BIOS设置(设置BIOS).ppt
- 香港城市大学:基序检测的随机化算法(PPT讲稿)Randomized Algorithm for Motif Detection.ppt
- 《数据结构》课程教学资源(PPT课件讲稿)第七章 图及其应用.ppt
- 3D Reconstruction from Images:Image-based Street-side City Modeling.ppt
- 大连理工大学:《计算机网络》课程教学资源(PPT课件讲稿)Chapter 2 应用层 application layer.ppt
- 四川大学:《操作系统 Operating System》课程教学资源(PPT课件讲稿)Chapter 3 Process Description and Control 3.4 Process Control 3.5 Execution of the Operating System 3.6 Unix SVR4 Process Management 3.7 Linux Process management system calls.ppt
- 《数据结构》课程教学资源(PPT课件讲稿)第七章 图 Graph.ppt
- 《数据结构》课程教学资源:实践教学大纲.doc
- 《网络算法学》课程教学资源(PPT课件讲稿)第三章 实现原则.ppt
- 《电脑组装与维护实例教程》教学资源(PPT课件讲稿)第5章 多媒体设备介绍及选购.ppt
- 广西医科大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Chapter 02 Network Classification.pptx
- 清华大学:无线网和移动网(PPT课件讲稿)Mobile and wireless network.pptx
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)第6章 Data-Level Parallelism in Vector, SIMD, and GPU Architectures.pptx
- 中国科学技术大学:Linux内核源代码导读(PPT讲稿,陈香兰).ppt
- 《网上开店实务》课程教学资源(PPT讲稿)学习情境3 网店装修.ppt
- 北京大学:《项目成本管理》课程教学资源(PPT课件讲稿)项目范围计划(主讲:周立新).ppt
- 香港中文大学:Achieving Secure and Cooperative Wireless Networks with Trust Modeling and Game Theory.ppt
- MSCIT 5210/MSCBD 5002:Knowledge Discovery and Data Mining:Chapter 4:Data Warehousing, On-line Analytical Processing and Data Cube.ppt
- 《程序设计基础》课程PPT教学课件(C++)第3讲 C++程序控制结构.ppt
- 四川大学:《数据库技术》课程教学资源(PPT课件讲稿)数据库设计.ppt
- 云计算 Cloud Computing(PPT讲稿)MapReduce进阶.ppt
- 《C语言程序设计》课程电子教案(PPT课件讲稿)第7章 用函数实现模块化程序设计.pptx
- 中国科学技术大学:云计算及安全(PPT讲稿)Cloud Computing & Cloud Security.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)数字图像处理基础 Basics of Digital Image Processing.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)图像压缩编码 Image Compression.pptx
- 《计算机组成原理》课程教学资源(PPT课件讲稿)第3章 计算机的算术运算.pptx
- Analysis of Algorithms(PPT讲稿)Data Structures and Data Management.ppt
- 《C语言程序设计》课程电子教案(PPT课件讲稿)第七章 数组.ppt
- 《计算机网络与因特网 Computer Networks and Internets》课程教学资源(PPT课件讲稿)第二讲 互联网应用软件.ppt
- 《计算机操作系统》课程教学资源(PPT课件讲稿)第四章 存储器管理.ppt
- 《单片机应用技术》课程PPT教学课件(C语言版)第10章 单片机测控接口.ppt
- 中国科技大学计算机系:《黑客反向工程》课程教学资源(PPT课件讲稿)黑客反向工程导论(陈凯明).ppt
- 香港科技大学:Record Linkage for Big Data.pptx