C ++ objects, arrays, object-based design

  C ++ does not support the abstract abstraction of arrays or the operation of the entire array. We sometimes want to operate on the entire array. For example, assign an array to another array to compare the two arrays or compare the size of the array. For example, give the size of the array. We can't use the assignment operator to copy one array to another for two arrays.
  
  int array0 [10], array1 [10];
  
  array0 = array1; // error The
  
  array type itself has no self-awareness and it does not know its length. We must additionally record this information of the array itself.
  
  The relationship between array and pointer:
  
  int ia [] = {0, 1, 1, 2, 3, 5, 8, 13, 21};
  
  ia; The array identifier represents the address of the first element in the array. Its type is a pointer to the array element type.
  
  ia; Equivalent & ia [0];
  
  * (ia + 1); Equivalent ia [1];
  
  Object creation
  
  * The first is to directly define an instance of the class-object:
  
  CGoods Car;
  
  this definition creates a CGoods class The object Car, at the same time, allocates its own storage block for storing data and member functions (codes) that operate on these data. As with variable definitions, an object is only valid in the domain in which it is defined.
  
  * The second method is to dynamically create class objects.
  
  Dynamic memory allocation (Dynamic Memory Allocation) can solve the problems that static compilation can't be solved, such as the use of arrays.
  
  C / C ++ defines 4 memory ranges: 1), code area, 2), global variable and static variable area, 3), local variable area is the stack area, 4), dynamic storage area, namely the heap area or Free storage area (free store).
  
  Static allocation and dynamic allocation:
  
  usually define variables (or objects), the compiler can know the size of the required memory space according to the type of the variables (or objects) at compile time, so that the system allocates certain storage for them when appropriate space. This kind of memory allocation is called static storage allocation.
  
  Some operation objects can only be determined when the program is running, so that they cannot reserve storage space for them when compiling. Only when the program is running, the system allocates memory according to requirements. This method is called dynamic storage allocation.
  
  All dynamic storage allocations are made in the heap area. When the program runs to a dynamically allocated variable or object, you must apply to the system to obtain a block of storage space of the required size for storing the variable or object. When the variable or object is no longer used, that is, at the end of its life, the storage space it occupies should be explicitly released so that the system can reallocate the heap space and reuse limited resources.
  
  Dynamic allocation format:
  
  pointer variable name = new type name (initialization type);
  
  delete pointer name;
  
  eg:
  
  int * pi = new int (0);
  
  delete pi; // Note that the target memory space pointed to by pi is released, but pi The pointer itself is not revoked, and the memory space occupied by the pointer is not released.
  
  Explanation: The
  
  * new operator returns a pointer to a variable (object) of the allocated type. The created variables or objects are operated indirectly through the pointer, and the dynamically created object itself has no name.
  
  * Generally define variables and objects with identifiers, named objects, and dynamically named unnamed objects (please note the difference with temporary objects in the stack area, the two are completely different: different lifespans, different methods of operation, temporary Variables are transparent to programmers).
  
  * The heap area will not be automatically initialized (including cleared) during allocation, and must be initialized explicitly with an initializer.
  
  * The operation sequence of the new expression is as follows: allocate the object from the heap, and then initialize the object with the value in parentheses.
  
  Based on object design to
  
  achieve array abstraction,
  
  1. Array class implementation has built-in self-awareness. First of all, it knows its size.
  
  2. The array class supports assignment between arrays and comparison operations between two arrays for equality and inequality.
  
  3. The array class should support the following query operations on the values ​​contained in it. What is the minimum value in the array? What is the maximum value? Is the index of the first position in the array if it exists?
  
  4. The array class supports self-sorting. For ease of discussion, it is assumed that there is a group of users who think that the array support sorting function is very important, but others do not take it for granted. In addition to supporting array operations, the array itself must be supported.
  
  5. You can specify the length to create an array. This value does not need to be known at compile time.
  
  6. Ability to initialize an array with a set of values.
  
  7. A single element in the array can be accessed through an index. For ease of discussion, assume that the user strongly requires the array subscript operator to achieve this function.
  
  8. The ability to intercept and point out wrong index values ​​assumes that we think this is necessary so there is no idea of ​​asking the user. We think that this is a must for a well-designed array. Our discussions with potential users have caused great heat.
  
  IntArray.h
  
  class IntArray
  
  {
  
  private:
  
  int _size; // Array size
  
  int * p; // Array pointer
  
  public:
  
  IntArray (int array_size);
  
  IntArray (int * array, int array_size);
  
  IntArray (IntArray & arr); // Copy constructor
  
  ~ IntArray () ;
  
  void sort (); // sort
  
  int find (int value); // find
  
  int max (); // maximum element
  
  int min (); // minimum element
  
  int size (); // array size
  
  void display () ;
  
  };
  
  IntArray.cpp
  
  #include <iostream> // Introduction of io stream file
  
  #include "IntArray.h"
  
  using namespace std;
  
  / ********************* ************************************************** * /
  
  / * Constructor The
  
  default array element is initialized to 0
  
  * /
  
  / ************************************************* *********************** /
  
  IntArray :: IntArray (int array_size)
  
  {
  
  _size = array_size;
  
  p = new int [array_size];
  
  for (int i = 0; i <array_size; i ++)
  
  {
  
  p [i] = 0;
  
  }
  
  }
  
  / ******************************* ***************************************** /
  
  / * The constructor is
  
  initialized with an array Another array
  
  * /
  
  / ******************************************** **************************** /
  
  IntArray :: IntArray (int * array, int array_size)
  
  {
  
  _size = array_size;
  
  p = new int [array_size];
  
  for (int i = 0; i <array_size; i ++)
  
  {
  
  p [i] = array [i];
  
  }
  
  }
  
  / ************************************************ ************************ /
  
  / * Calculate the maximum value of the array * /
  
  / **************** ************************************************** ****** /
  
  int IntArray :: max ()
  
  {
  
  int max = -1;
  
  for (int i = 0; i <_size; i ++)
  
  {
  
  if (p [i]> max) {
  
  max = p [i ];
  
  }
  
  }
  
  return max;
  
  }
  
  / ***************************************** ******************************* /
  
  / * Calculate the minimum value of the array * /
  
  / ********* ************************************************** ************* /
  
  int IntArray :: min ()
  
  {
  
  int min = 10000;
  
  for (int i = 0; i <_size; i ++)
  
  {
  
  if (p [i] <min) {
  
  min = p [i];
  
  }
  
  }
  
  return min;
  
  }
  
  / ************************** ********************************************** /
  
  / * Find Specify the element, return the position in the array * /
  
  / ************************************** ********************************** /
  
  int IntArray :: find (int value)
  
  {
  
  int index = -1 ;
  
  for (int i = 0; i <_size; i ++)
  
  {
  
  if (p [i] == value) {
  
  index = i;
  
  }
  
  }
  
  return index;
  
  }
  
  / ************* ************************************************** ********* /
  
  / * Returns the size of the array * /
  
  / ******************************** **************************************** /
  
  int IntArray :: size ()
  
  {
  
  return _size;
  
  }
  
  / ************************************* *********************************** /
  
  / * Display the value in the array element * /
  
  / *** ************************************************** ******************* /
  
  void IntArray :: display ()
  
  {
  
  for (int i = 0; i <_size; i ++)
  
  {
  
  cout << p [i] < <"";
  
  }
  
  cout << endl;
  
  }
  
  / ****************************************** ********************************** /
  
  / * Sort the array, ascending or descending * /
  
  / *** ************************************************** ******************* /
  
  void IntArray :: sort ()
  
  {
  
  }
  
  / ************************************************* *********************** /
  
  / * Destructor, free memory space * /
  
  / ************** ************************************************** ******** /
  
  IntArray :: ~ IntArray ()
  
  {
  
  delete p;
  
  }
  
  / **************************** ************************************************ /
  
  / * Calling function * /
  
  / *********************************************** ************************* /
  
  int main (void)
  
  {
  
  int intA [] = {10,2,4,6,7,98, 32,4};
  
  IntArray intArr (intA, 8);
  
  cout << "size:" << intArr.size () << endl;
  
  cout << "max:" << intArr.max () << endl;
  
  cout << "min:" << intArr.min () << endl;
  
  cout<<"find:"<< intArr.find(98) <<endl;
  
  intArr.display ();
  
  intArr. ~ IntArray ();
  
  }
  
  Generic programming (generic programming) The
  
  IntArray class provides a useful alternative type for predefined integer array types. If the user wishes to use an array
  
  key of type double or string The word template is introduced. The template parameters are enclosed by a pair of angle brackets <>
  
  template <class elemType>
  
  class Array {
  
  public:
  
  Array (int size);
  
  Array (elemType * array, int array_size);
  
  virtual elemType min ();
  
  virtual elemType max ( );
  
  Private:
  
  int _size;
  
  elemType * ia;
  
  };
  
  Later when we instantiate an instance of a specific type, such as an Array array of type int double or string, you can use these three instances directly in the program.
  
  Call:
  
  const int array_size = 4;
  
  // elemType becomes int
  
  Array <int> ia (array_size);
  
  // elemType becomes double
  
  Array <double> da (array_size);
  
  // elemType becomes char
  
  Array <char> ca (array_size);
  
  what happens to the member functions of the class template? Not all member functions can be automatically It is instantiated with the instantiation of the class template. Only member functions that are actually used by the program are instantiated. This generally occurs at an independent stage in the program generation process.

Guess you like

Origin www.cnblogs.com/zhenhua1618/p/12729762.html