[Cpp]
/*
* Copyright and version Declaration of the program
* Copyright (c) 2013, a student from the computer College of Yantai University
* All rightsreserved.
* File name: object. cpp
* Author: Yang shaoning
* Completion date: January 1, April 5, 2013
* Version: v1.0
* Input Description: None
* Problem description :.
* Program output :.
*/
# Include <iostream>
Using namespace std;
Class
{Private:
Int *;
Int n;
Int MaxLen;
Public:
A (int * a, int n = 0, int MM = 0 );
~ A ();
Int GetValue (int I) {return a [I];}
Int GetMaxLen () {return MaxLen ;}
};
A: A (int * aa, int nn, int MM)
{N = nn;
MaxLen = MM;
If (n> MaxLen) exit (1 );
A = new int [MaxLen];
Int I; // transmits each element value in the aa array to the corresponding element in array a using the I as the cyclic variable.
For (I = 0; I <MaxLen; I ++)
{A [I] = aa [I];} // here is a pointer, not an array
/*
* Copyright and version Declaration of the program
* Copyright (c) 2013, a student from the computer College of Yantai University
* All rightsreserved.
* File name: object. cpp
* Author: Yang shaoning
* Completion date: January 1, April 5, 2013
* Version: v1.0
* Input Description: None
* Problem description :.
* Program output :.
*/
# Include <iostream>
Using namespace std;
Class
{Private:
Int *;
Int n;
Int MaxLen;
Public:
A (int * a, int n = 0, int MM = 0 );
~ A ();
Int GetValue (int I) {return a [I];}
Int GetMaxLen () {return MaxLen ;}
};
A: A (int * aa, int nn, int MM)
{N = nn;
MaxLen = MM;
If (n> MaxLen) exit (1 );
A = new int [MaxLen];
Int I; // transmits each element value in the aa array to the corresponding element in array a using the I as the cyclic variable.
For (I = 0; I <MaxLen; I ++)
{A [I] = aa [I];} // here is a pointer, not an array [cpp] view plaincopyprint ?}
A ::~ A () {delete [] a;} // class definition of the destructor, releasing the space pointed to by pointer data
Int main ()
{
Int B [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
A r (B, 10, 10 );
Int I, s = 0;
For (I = 0; I <r. getMaxLen (); I ++) // uses I as the cyclic variable to accumulate the values of each element in the data member of the r object to s.
{
S = s + r. GetValue (I );
}
Cout <"s =" <s <endl;
Return 0;
}
}
A ::~ A () {delete [] a;} // class definition of the destructor, releasing the space pointed to by pointer data
Int main ()
{
Int B [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
A r (B, 10, 10 );
Int I, s = 0;
For (I = 0; I <r. getMaxLen (); I ++) // uses I as the cyclic variable to accumulate the values of each element in the data member of the r object to s.
{
S = s + r. GetValue (I );
}
Cout <"s =" <s <endl;
Return 0;
}
Result:
Feel: the pointer type is used as a parameter of the member function. In the destructor, the array cannot be released, and the pointer can