A C + + project
Main.cpp
#include <stdio.h>
#include "Array.h"
int main ()
{
Array A1 (10);
for (int i=0;i<a1.length (); i++)
{
A1.setdata (I,i);
}
for (int j=0; j<a1.length (); j + +)
{
printf ("Element%d:%d \ n", J,a1.getdata (j));
}
A1.destroy ();
printf ("Press any key to continue ...");
GetChar ();
return 0;
}
Array.h
#ifndef _array_h_
#define _array_h_
Class Array
{
Private
int mlength;
int* Mspace;
Public
Array (int length); constructor function
Array (const array& obj); Constructors (implementing Replication functions)
int length ();
void SetData (int index, int value);
int GetData (int index);
~array ();
};
#endif
Array.cpp
#include "Array.h"
Array::array (int length)
{
if (length<0)
{
length=0;
}
Mlength=length;
Mspace=new Int[mlength];
}
Array::array (const array& obj)
{
Mlength=obj.mlength;
mspace=new Int (mlength);
for (int i=0; i<mlength;i++)
{
Mspace[i]=obj.mspace[i];
}
}
int Array::length ()
{
return mlength;
}
void Array::setdata (int index, int value)
{
Mspace[index]=value;
}
int array::getdata (int index)
{
return Mspace[index];
}
Array::~array ()/// destructor free memory automatic call before destroying object
{
Mlength=-1;
Delete[] Mspace;
}
C + + Class Encapsulation _ Engineering