home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
class2.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
1KB
|
48 lines
// Program demonstrates constructors and destructors
#include <iostream.h>
const unsigned MIN_SIZE = 4;
class Array
{
protected:
double *dataPtr;
unsigned size;
public:
Array(unsigned Size = MIN_SIZE);
~Array()
{ delete [] dataPtr; }
unsigned getSize() const
{ return size; }
void store(double x, unsigned index)
{ dataPtr[index] = x; }
double recall(unsigned index)
{ return dataPtr[index]; }
};
Array::Array(unsigned Size)
{
size = (Size < MIN_SIZE) ? MIN_SIZE : Size;
dataPtr = new double[size];
}
main()
{
Array Ar1(10);
double x;
// assign data to array elements
for (unsigned i = 0; i < Ar1.getSize(); i++) {
x = double(i);
x = x * x - 5 * x + 10;
Ar1.store(x, i);
}
// display data in the array element
cout << "Array Ar1 has the following values:\n\n";
for (i = 0; i < Ar1.getSize(); i++)
cout << "Ar1[" << i << "] = " << Ar1.recall(i) << "\n";
return 0;
}