软件设计(II)教学课件:Chapter5 Classes and Objects.pptVIP

  • 0
  • 0
  • 约6.49千字
  • 约 26页
  • 2021-11-30 发布于安徽
  • 举报

软件设计(II)教学课件:Chapter5 Classes and Objects.ppt

* 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 class name. For example: * class A { private: int m; public: void show(); }; int A::* ip = A::m; § 5.2 Defining Member Functions * return-type class-name :: function-name (parameters) { Function body } Identity label Scope resolution operation * Inline Declaration A member function can be implemented inside a class 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 * Separating Declaration from Implementation Similar 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 #include iostream using namespace std; class set{ int m, n; public: void display(void); int largest(void); }; int set::largest(void){ if(m=n) return m; else return n; } void set::display(){ coutlargest()\n; } Nesting of Member Functions * A member function can be called by another member function of the same class Via function name directly (no object name) 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 outsiders * class sample { int m; void read(void); public: void update(void); void write(void); }; § 5.3 Memory Allocation and Static Members * Memory of member functions -- Common for al

文档评论(0)

1亿VIP精品文档

相关文档