C + + Class Encapsulation _ Engineering

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.