A simple array class written when learning C ++

zhaozj2021-02-16  101

Array.H interface

// Class Automatically Generated BY DEV-C New class wizard / *? * Array abstract data type, can be implemented:? * 1, under the standard range check? * 2, subscript operation? * 3, array assignment? * 4 , Array comparison? * 5, an array input / output? * 6, array knows itself? * /? # I idef array_h # define array_h # includelude

Using namespace std;

Class Array {? Friend Ostream & Operator << (Ostream &, Const Array &); // Out? Friend Istream & Operator >> (ISTREAM &, Array &);? // Input? public: ???? int Check () ; ?? Array (int arch, "; ?? ~ array (); ?? int getSize () const;? // Return Array size ?? int & operator [] (int) ;? // subscript? ?? const Array & Operator = (const array "; // assignment? ?? const Int operator! = (Const array &) const;? //? ?? const Int operator == (const array "const;? //? ? private: ???? int size;? // Array size ???? int * PTR;? // Pointer to the first element of the array};

#ENDIF // Array_H

?

?

?

// Class implementation // Class Automatically Generated by DEV-C New Class Wizard

#include "array.h" // Class's header file # include

Using namespace std;

// Class Constructorarray :: array (int Arraysize) {? size = arraysize;? ptr = new int [size] ;? Assert (PTR! = 0) ;? MEMSET (PTR, 0, SIZE * SIZEOF (Int); }

// Copy constructor array :: array (const array&it) {? Size = init.size;? Ptr = new int [size] ;? Assert (PTR! = 0) ;? MEMSET (Ptr, 0, Size * Sizeof (int));

// Class DestructorRay :: ~ array () {? delete [] PTR;}

// array range Check int Array :: Check () {? Return 1; ???}

// Acquired Array Size Int Array :: getsize () const {??? Return Size;}

iStream & Operator >> (ISTREAM & INPUT, ARRAY & A) {? for (int i = 0; i

?? Input >> A.PTR [I];

Return INPUT;

}

// Output ostream & operator << (Ostream & Output, Const Array & A) {? For (int i = 0; i

?? Output <

<< '';

? Return Output;

}

// Script INT & Array :: Operator [] (int index) {??? if (INDEX <0 | INDEX> SIZE) index = 0; Return Ptr [INDEX];

// Re-serve =? Const array & array :: Operator = (const array "{??? if (& right! = This) ??? {??????? f (size! = Right.size)? // If the space is different, it is redistributed ??????? {???????? Delete [] Ptr; ???????? size = right.size; ?????? ? PTR = new int [size]; ?? ???????? assert (PTR! = 0); ?????} ???? Memcpy (Ptr, Right.ptr, Size * Sizeof INT); ???} ??? Return * this;}

Const int Array :: Operator! = (const array "const {??? if (size! = right.size) Return 1; ??? for (INT i = 0; i

???? IF (PTR [i]! = Right.PTR [i])

??? Return 1;

??? RETURN 0;}

Const int Array :: Operator == (const array) const {??? if (size == right.size) Return 1; ??? for (int i = 0; i

???? IF (PTR [i]! = Right.PTR [i])

??? Return 0;

??? Return 1;}

?

转载请注明原文地址:https://www.9cbs.com/read-10393.html

New Post(0)