当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PPT,文档页数:74,文件大小:978.5KB,团购合买
Basic Concepts and Syntaxes ‘Inline’s and ‘Static’s Const and Mutable Construction and Destruction Operators Overloading
点击下载完整版文档(PPT)

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 }

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共74页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有