Classes and objects (base)

Copyright notice: reproduced please indicate the source of the article https://blog.csdn.net/swo_ows/article/details/90544738

The introduction of class

Results European body only variable defined in the C language, C ++, in addition to defining the structure of the variable, the function may be defined, but the C ++ class is generally used instead of struct

Class definition

  • class {} class name;
  • Members of the class of elements known as class: attribute or a method referred to as data members of the class variable, or a function called a class member functions

Defined way

  • Declarations and definitions in the whole class, if the class member functions defined in the compiler might deal with it as an inline function
  • Statement on the .h file .cpp file definitions in general this manner, it is necessary to use the definition of the scope resolution operator (: :) Description class field belongs members

Access qualifier

  • public: you can directly access outside the class
  • private: outside the class can not directly access
  • protected: outside the class can not directly access the derived class can access

characteristic

  • Access the scope of a visit from the position of the access qualifier qualifier appears to lower
  • class default access is private, struct default access is public
  • Reflects the encapsulation, data package is to combine and methods of operation, the hidden object properties and implementation details, only publicly interface and interact with objects

Class instantiation

  • Object creation class type referred to with the class instantiation
  • There are two ways to create a class object, one is static build (class name object name;), and a dynamic establishment (name = new class name of the object class name;)
  • Class is just like a model thing, defining what members of a class and not the actual allocation of memory space to store
  • A class of a plurality of objects can be instantiated, the instance of the actual physical space occupied by the object member variable storage class

Object class size

  • The size of a class is the class member variables sum, pay attention to memory alignment, because the class object is only to save the member variables, member functions in a public code segment
  • For empty class, the compiler a byte that uniquely identifies the class

this pointer

C ++ compilers to each member functions (except the constructors) adds a hidden pointer parameter, the pointer to the current object, the operation of all member variables are a function of the weight of the pointer to access by

characteristic

  • this pointer Type: Class Type * const
  • this pointer exists on the stack
  • Internal use only member function
  • On this pointer is essentially a member function parameter, the object is a member function is called, the address of the object passed as an argument to this parameter, so the object is not stored in this pointer
  • this pointer is the first member of an implicit function pointer parameter, typically ecx register is automatically passed through by the compiler, the user does not need to pass

Guess you like

Origin blog.csdn.net/swo_ows/article/details/90544738