C ++ class summary

1, the concept of class

A class is a data type defined by the programmer himself, in solving practical problems species, often need to define data types according to their needs, such data including data types and functions. The basic idea is the class of data abstraction and encapsulation, the class includes a user interface comprising a user can perform operations, including implementation class is class data members, responsible for various functions required for the private body and define the function of the class interface.

2, the definition of the class


class 类名 {
成员列表
};

1, class members are generally composed of two pieces of data members and member functions. Each class can not have members, you can also have more than one member. You can set different access rights (which is the difference between class and structure) indicated by visiting qualifier: public (public); private (private); protected (protection).
2, the interface is accessible to the outside world can help window, usually outside the class member function interface, a class can access, access to certain data in order to perform certain functions. Data members are generally protected the privacy of the object, not random access. 3, member functions defined outside the class, and to raise :: scope declaration symbol indicates belonging to a scope. The benefits of inline function is to call faster and disadvantages take up memory (the equivalent of once every call are defined once) function externally defined, will not be treated as an inline function.
2, access control and encapsulation

Control access to different members of the class, to achieve the client programmer to hide information, only the client programmer interface programming for class.
In short: there is limited access to public limited and private limited (protect similar to private, mainly used for inheritance), within the class outside the class members also can access (will explain in detail later) by way of a friend, but not recommended, because encapsulation will destroy class.
Access qualifier Description:

  1. public members can directly access outside the class, that are accessible, anyone can see. ;
  2. private and protected members can not be accessed outside the class, private defined private defined, is of the type to be protected. This may reflect the thinking of the package, the data seal it up, not announced. ;
  3. Until the next visit qualifier appear until the qualifiers start from the position that the scope of access qualifier appears
  4. Class body defined class qualifiers If not defined, the default access rights are private; struct type is public by default

3 used outside the class, the class

Declare class variables, reuse, using three ways:
reference by the operator and the object name of the object member (.)
The Test AA;
AA function name;

Operator reference pointer to the object through the pointer member and (->)
the Test * P;
P = new new the Test;
p-> function name;

(.) By reference to an object member variables and objects referenced operator
the Test AA, AA & = R & lt;
. R & lt function name;

4, a friend of
a friend class may be:
1, the overall function
2, a member function of another class
3, another class (class A is a friend class B represents all the member functions of the A's are the friend B)
generally destroy friend class encapsulation, therefore, should be avoided friend.

5, the constructor

Why use a constructor without direct assignment.
The constructor to initialize the data members. Direct memory request corresponding to the assignment, the data type is a class, do not have the memory space in the absence of actual use. Only assigned by the constructor. If a data member is public, it can direct it outside the class initialization, but if it is private, you can not access it directly, which use the constructor.

The constructor is used to initialize a problem processing object constructor is a special member function, does not require human calling, but is automatically executed when the object is created. When the constructor write at least two, with a reference, without a reference, not to solve the problem of the description of the arguments object.
Specification writing constructor:
1, the constructor does not return type, the same function names and class names.
2, the same sequential order and object declaration initialized.

Default constructor: when not explicitly defined constructor system generated default constructor. Only if the class constructor argument constructor without free parameters, the system error (it is generally also define a constructor with no arguments). If a class constructor is not, then the system defaults generated default constructor.
The copy constructor: initialize another object generated by the object

If you need to initialize are members of a class type, you must use the constructor initialization list. Assignment does not work.

Delegate constructors: a member initialization list and a function body. Member initialization list there is only one, namely the class name itself. Behind the class name followed by parenthesized parameter list, the parameter list must match another class constructor. When a delegate to another constructor constructor initialization list received delegate body of the constructor and sequentially performed, and then return control to the body of the function of the delegate.

6, destructor

The release of the statement written in the memory destructor, the system automatically performs the destructor at the end of the program is running, destruction and release objects of memory. When the class name of the wave front of the name lines Singapore destructor "~." Destructor no return type, there is no argument. Destructor can not be overloaded (constructors can override), only one can define a destructor for the class.

Guess you like

Origin blog.csdn.net/qq_43515378/article/details/89303313