Print? //
// Main. cpp
// 6-2.cpp
//
// Created by JI Zilong on 13-4-10.
// Copyright (c) 2013 Ji Zilong. All rights reserved.
//
Class {
Private:
Int;
Public:
A () {a = 0 ;}
A (int aa) {a = aa;} // defines the constructor and uses the aa parameter to initialize data member.
};
Int main (){
A * p; // defines the pointer object p of Class.
P = new A (3); // use p to point to A dynamic object and initialize it as an integer.
}
2:
//
// Main. cpp
// 6_2_2.cpp
//
// Created by JI Zilong on 13-4-10.
// Copyright (c) 2013 Ji Zilong. All rights reserved.
//
# Include <iostream. h>
# Include <stdlib. h>
Class {
Private:
Int *;
Int n;
Int MaxLen;
Public:
A (): a (0), n (0), MaxLen (0 ){}
A (int * aa, int nn, int MM );
~ A ();
Int GetValue (int I) {return a [I];} // returns the value of a [I] In the function body.
};
A: A (int * aa, int nn, int MM ){
N = nn;
MaxLen = MM;
If (n> MaxLen) exit (1 );
A = new int [MaxLen];
For (int I = 0; I <n; I ++ ){
A [I] = aa [I];
}; // Transmits each element value in the aa array to the corresponding element of array a using I as the cyclic variable
}
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 <10; I ++ ){
S = s + r. GetValue (I );
}; // Use I as the cyclic variable to accumulate the values of each element in the data member of the r object to s.
Cout <"s =" <s <endl;
}
Running result:
//
// Main. cpp
// 6-2.cpp
//
// Created by JI Zilong on 13-4-10.
// Copyright (c) 2013 Ji Zilong. All rights reserved.
//
Class {
Private:
Int;
Public:
A () {a = 0 ;}
A (int aa) {a = aa;} // defines the constructor and uses the aa parameter to initialize data member.
};
Int main (){
A * p; // defines the pointer object p of Class.
P = new A (3); // use p to point to A dynamic object and initialize it as an integer.
}
2:
//
// Main. cpp
// 6_2_2.cpp
//
// Created by JI Zilong on 13-4-10.
// Copyright (c) 2013 Ji Zilong. All rights reserved.
//
# Include <iostream. h>
# Include <stdlib. h>
Class {
Private:
Int *;
Int n;
Int MaxLen;
Public:
A (): a (0), n (0), MaxLen (0 ){}
A (int * aa, int nn, int MM );
~ A ();
Int GetValue (int I) {return a [I];} // returns the value of a [I] In the function body.
};
A: A (int * aa, int nn, int MM ){
N = nn;
MaxLen = MM;
If (n> MaxLen) exit (1 );
A = new int [MaxLen];
For (int I = 0; I <n; I ++ ){
A [I] = aa [I];
}; // Transmits each element value in the aa array to the corresponding element of array a using I as the cyclic variable
}
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 <10; I ++ ){
S = s + r. GetValue (I );
}; // Use I as the cyclic variable to accumulate the values of each element in the data member of the r object to s.
Cout <"s =" <s <endl;
}
Running result: