(Knowledge 2) classes and packages

http://www.runoob.com/cplusplus/cpp-data-encapsulation.html

1 simple example

 

 

class Box
{
   public:
      double getVolume(void)
      {
         return length * breadth * height;
      }
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};

  

Examples of encapsulated data

C ++ program, any class with public and private member can be used as examples of data abstraction and encapsulation data. Consider the following examples:

#include <the iostream> 
the using namespace STD; 
 
class the Adder { 
   public: 
      // Constructor 
      the Adder (int I = 0) 
      { 
        Total = I; 
      } 
      // external interface 
      void addNum (int Number) 
      { 
          Total + = Number; 
      } 
      / / external interface 
      int getTotal () 
      { 
          return Total; 
      }; 
   Private: 
      // hide the external data 
      int Total; 
}; 
int main () 
{ 
   the Adder a; 
   
   a.addNum (10); 
   a.addNum (20 is); 
   a .addNum (30);
 
   cout << "Total " << a.getTotal() <<endl;
   return 0;
}

  

2C ++ object-oriented programming Introduction

Data encapsulation is a mechanism to function data and operation data bundled together, data abstraction is an interface to a user and is exposed only to hide the implementation details of the mechanism.

Object-oriented programming (Object Oriented Programming), referred to OOP.

In the conventional process-oriented programming, data and related data manipulation functions are separate independent entity;

Objects, such objects are in fact all around; for programming perspective, the object comprising A) a series of attributes (data); B) a series of operations (functions).

OOP object provides design features, properties and behavior of an object comprising, include both together, constitute the object entity (i.e. an entity class);

Object entity, make the program more modular, easier to read and easy to write, improved code reuse to a higher level;

Object provides a direct method for data manipulation, defines the interaction, and the interaction between the object and how the object;

More importantly, OOP provides a more practical concepts: encapsulation, inheritance, polymorphism and abstraction.

This focuses on the package, i.e., the encapsulated data object to a class and operation function entity.

One essential difference between the structure and the class is a member of the class identifier may be used to control their accessibility;

Typically, the member variable classes, a prefix 'm_' to distinguish:

3 Constructor Ⅰ

1) Constructor: is a special member function is performed when the class is instantiated; normally to initialize member variables.

Constructor clear naming rules: A) the function names and class names must be the same; B) no return type (including void).

No argument constructor - constructor with no arguments, the default constructor for the class:

 

Typically, the class contains a default constructor, you can initialize member variables.

Containing argument constructor - Constructor a parameter, the specified value can be imparted to the member variables;

 

Two or more constructors, similar overloaded functions; constructors must have a unique front (the number of parameters and parameter types).

Class can also contain only argument constructor, no default constructor:

 

 

4 destructor

析构函数是类的另一种特殊的函数,当类的对象销毁时调用;它和构造函数是成对出现的。

普通的简单类,一般不需要析构函数;因为C++会自动回收垃圾;

如果类中执行了某些动态内存分配,则需要显式定义析构函数,并释放回收垃圾;

析构函数的明确命名规则:A)函数名和类名一样,并前缀'~';B)不能带参数(即意味着只有一个析构函数);C)没有返回类型。

 

 

注意动态分配,必须提供析构函数,来回收分配的空间。

5  静态成员函数

不用创建即可以访问

如静态成员变量一样,静态成员函数是属于类的本身,不属于任何类的对象;

如静态成员变量访问一样,可以通过:类名::静态成员来访问类的静态成员变量。

注意:静态成员函数,没有this指针;

例如:

 

6 类代码和头文件

头文件的使用可达到重用的效果;所以将类的定义放在头文件中,而成员函数放在.cpp中定义;而cpp的名字需和类的名字相同。

Date.h:

Date.cpp:

推荐分离类的定义中的成员函数到类外定义。

 

Guess you like

Origin www.cnblogs.com/kekeoutlook/p/11210849.html