"C + + Primer" objects, arrays, object-based design, generic design

Source: Internet
Author: User
Tags array sort int size
Array
C + + does not support an abstract abstraction of arrays nor does it support operations on an entire array we sometimes want to manipulate the entire array, such as assigning an array to another array for equal comparison of two arrays or to know the size of the array. For example, given two arrays, we can't copy an array to another with the assignment operator.
int array0[], array1[10];
Array0 = array1; Error
The array type itself has no self-awareness it does not know its own length we must record this information separately from the array itself.
The relationship between arrays and pointers:
int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};
Ia What it means. The array identifier represents the address of the first element in the array. Its type is a pointer to an array element type.
Ia Equivalence &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 an object car for the Cgoods class and assigns it its own block of storage to hold the data and the member functions (code) that perform the operation on the data. As with variable definitions, an object is valid only in the domain in which it is defined.
* The second is the method of dynamically creating the object of the class.
Dynamic memory allocation (Memory allocation) solves problems that static compilation, such as Large array opening, can not solve.
C + + defines 4 memory intervals: 1, code area, 2, global variable and static variable area, 3, local variable area i.e. stack area, 4), dynamic storage area, i.e. heap (heap) area or free store.
Static allocation and dynamic allocation:
Typically, a variable (or object) is defined, and the compiler can at compile time know the size of the desired memory space based on the type of the variable (or object), so that the system allocates the determined storage space for them at the appropriate time. This memory allocation is called static storage allocation.

Some action objects can be determined only when the program is running, so that the storage space cannot be scheduled for them at compile time, only when the program is running, the system allocates memory as required, a method called dynamic storage allocation.

All dynamic storage allocations are performed in the heap area. When a program runs to a variable or object that needs to be dynamically allocated, the system must be requested to obtain a storage space of the desired size in the heap for storing the variable or object. When the variable or object is no longer in use, it is the end of its life to explicitly release the storage space it occupies, so that the system can redistribute the heap space and reuse the limited resources. Dynamic allocation format:
Pointer variable name =new type name (initialization);
Delete pointer name;
eg
int *pi=new int (0);
Delete pi; Note that the target memory space that the PI points to is freed, but the pi pointer itself is not undone and the memory space that the pointer occupies is not released.
Description
The *new operator returns a pointer to the assigned type variable (object). The variable or object that you create is indirectly manipulated by the pointer, and the dynamically created object itself has no name.
* General definition of variables and objects to be named with identifiers, named objects, and dynamically called nameless objects (note the difference from the temporary objects in the stack, the two are completely different: The life cycle is different, the operation method is different, the temporary variable is transparent to the programmer).
* The heap area does not initialize automatically at the time of allocation (including clear 0) and must be explicitly initialized with an initialization (initializer).
* The action sequence for the new expression is as follows: Allocates an object from the heap area, and then initializes the object with the value in parentheses.

Object based Design

Implements array abstraction,
1. The implementation of the array class has built-in self-awareness first of all it knows its own size.
2. Array classes support the assignment between arrays and the comparison of equality and inequality between two arrays.
3. An array class should support the following query operations on the values it contains. What is the maximum value of the minimum value of a particular value in the array if there is an index of the first position it occupies.
4. Array classes support self-ordering to allow for discussion suppose there is a group of users they think the array supports sorting is important and others disagree about it. In addition to supporting array operations, the mechanisms that must support the array itself are included.
5. The value of the ability to specify the length to create an array does not need to be known at compile time.
6. Can initialize an array with a set of values.
7. Ability to access a single element of an array in order to facilitate discussion the user is strongly required to use the array subscript operator to achieve this function.
8. Ability to intercept and point out incorrect index values Suppose we think it's necessary, so we don't ask the user's idea. We think this is a well-designed array that must be implemented our discussion with potential users has caused a great deal of 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);//lookup
	int max ();//MAX element
	int min ();//min element
	int size ();//array size
	void Display ();
};

IntArray.cpp

#include <iostream>//io streaming files are introduced #include "IntArray.h" using namespace std; /************************************************************************//* Constructor 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; }/************************************************************************//* Construction method initializes another array with an 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];                                                                     }}/************************************************************************//* Compute the maximum value of an array *//************************************************************************/int int Array::max ()
{int max =-1;
		for (int i = 0; i< _size; i++) {if (P[i] > max) {max = p[i];
} return max;                                                                     /************************************************************************//* Compute the minimum value of an array * */************************************************************************/int Intar
	Ray::min () {int min = 10000;
		for (int i = 0; i<_size; i++) {if (P[i] < min) {min = p[i];
} return min;                                                                     /************************************************************************//* Finds the specified element, returns 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;                                       /************************************************************************//* Return array size                              *//************************************************************************/int intarray::

Size () {return _size;}                                                                     /************************************************************************//* Displays the values in the array element * */************************************************************************/void Intar
	Ray::d Isplay () {for (int i = 0; i<_size; i++) {cout<<p[i]<< "";
} cout<<endl;                                                                     /************************************************************************//* Array sort, ascending or descending * */************************************************************************/VO                                                                     ID Intarray::sort () {}/************************************************************************//* destructor, freeing memory space */
/******************************************************** ************/Intarray::~intarray () {delete p;}                                                                     /************************************************************************//* Keynote 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 (+) <<endl;
	Intarr.display ();
Intarr.~intarray (); }

generic Design (generic programming)
The Intarray class provides a useful alternative type for predefined integer array types if the user wants to use a double or string array
Keyword template The introduction of template parameters is surrounded 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;
};
You can use these three instances directly in your program later when we instantiate an instance of a particular type, such as int double or string array of type instantiate.
Call:
const int array_size = 4;
Elemtype into Int.
Array<int> ia (array_size);
Elemtype into Double.
Array<double> da (array_size);
Elemtype into Char.
Array<char> CA (array_size);
What about the member functions of the class template? Not all member functions can be instantiated automatically with the class template instantiation. Only the member functions that are actually used by the program will be instantiated. This generally occurs during the process of program generation in an independent phase.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.