Questions and codes:
"Project-My Array class"
Read the program, complete the definition of the member function, because there are pointer-type data members, note the constructor that requires a deep copy.
#include <iostream>using namespace Std;class myarray{private: int *arrayaddr;//Save the first address of an array with Len integer elements int Len; Record the length of the dynamic array int max; The maximum value in a dynamic array (not a data member that must be in a dynamic array) public: MyArray (int *a, int n); ~myarray (); int getValue (int i); Gets the value of the element labeled I in the array int getlen (); Returns the array length int getmax (); Returns the maximum value in an array};//writes out the definition of each member function int main () { int b[10]= {5, 7, 4 MyArray R1 (b,10); cout<< "Max:" <<r1.getmax () <<endl; int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93}; MyArray R2 (c,15); int i,s=0; for (i=0; I<r2.getlen (); i++) s+=r2.getvalue (i); cout<< "and for all elements:" <<s<<endl; return 0;}
/* Copyright (c) 2016, College of Computer and Control engineering, Yantai University All rights reserced file name: Test.cpp Author: Cai finish date: May 9, 2016 version number: v1.0 problem Description: Input Description: Program output Out: */#include <iostream>using namespace Std;class myarray{private:int *arrayaddr;//Save the first address of an array with len integer elements int le N Record the length of the dynamic array int max; The maximum value in the dynamic array (not the data member that must be in the dynamic array) public:myarray (int *a, int n); ~myarray (); int getValue (int i); Gets the value of the element labeled I in the array int getlen (); Returns the array length int getmax (); Returns the maximum value in the array};//writes out the definition of each member function myarray::myarray (int *a, int n) {int i; Len=n; Arrayaddr=new Int[n]; MAX=A[0]; for (i=0;i<n;i++) {arrayaddr[i]=a[i]; if (A[i]>max) max=a[i]; }} int Myarray::getvalue (int i) {return arrayaddr[i];} int Myarray::getlen () {return len;} int Myarray::getma X () {return max;} Myarray::~myarray () {delete []arrayaddr;} int main () {int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4}; MyArray R1 (b,10); cout<< "Maximum value:" <<r1.getmax () <<endl; int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93}; MyArray R2 (c,15); int i,s=0; for (i=0; I<r2.getlen (); i++) S+=r2.getvalue (i); cout<< "and for all elements:" <<s<<endl; return 0;}
Operation Result:
Summary of Knowledge points:
Learning experience:
Nineth Week on-Machine Practice Project 2-My Array class