C++. Arrays of objects in classes. Examples. Arrays of objects in managed and unmanaged classes. An array of unmanaged (*) pointers to a class object




Arrays of objects in classes. Examples. Arrays of objects in managed and unmanaged classes. An array of unmanaged (*) pointers to a class object


Contents


Search other websites:

1. An example of using an array of objects in an unmanaged class

A CMyPoint class is given that implements the point on the coordinate plane. A class called CPolygon that implements the polyline is also defined. The CPolygon class declares an array of 10 objects of the CMyPoint type.

Implementation of class CMyPoint is as follows:

// class describes a point on the coordinate plane
class CMyPoint
{
    int x, y;

    public:
    // class constructor
    CMyPoint(void)
    {
        x = y = 0;
    }

    // access methods
    int GetX(void) { return x; }
    int GetY(void) { return y; }
    void SetXY(int nx, int ny)
    {
        x = nx;
        y = ny;
    }
};

Implementation the CPolygon class

// class that describes a polygon
class CPolygon
{
    int n; // number of points
    CMyPoint cp[10]; // array of points

    public:
    // class constructor
    CPolygon(void) { n = 0; }

    // access methods
    CMyPoint GetPoint(int index)
    {
        return cp[index];
    }

    // sets a new value of a given point
    void SetPoint(int index, CMyPoint & pt)
    {
        if (index>=n) return;
        cp[index] = pt;
    }

    // adds a point to a polyline
    void AddPoint(CMyPoint pt)
    {
        if (n<10) n++;
        else return;
        cp[n-1] = pt;
    }

    // determines the length of the polygon
    double Length(void)
    {
        double len, t;
        int x1, y1, x2, y2;

        len = 0;
        for (int i=0; i<n-1; i++)
        {
            x1 = cp[i].GetX();
            y1 = cp[i].GetY();
            x2 = cp[i+1].GetX();
            y2 = cp[i+1].GetY();
            t = System::Math::Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            len += t;
        }
        return len;
    }
};





The use of the CPolygon and CMyPoint classes in some method

// using the CPolygon class
CPolygon p1;
CMyPoint pt1;

// form a polygonal line with coordinates (1; 2), (3; 4), (5;6)
pt1.SetXY(1, 2);
p1.AddPoint(pt1);

pt1.SetXY(3, 4);
p1.AddPoint(pt1);

pt1.SetXY(5, 6);
p1.AddPoint(pt1);

// test
int x, y;
CMyPoint pt2;

pt2 = p1.GetPoint(0);
x = pt2.GetX(); // x = 1
y = pt2.GetY(); // y = 2

pt2 = p1.GetPoint(1);
x = pt2.GetX(); // x = 3
y = pt2.GetY(); // y = 4

// length of polygon
double d = p1.Length(); // d = 5.6568...

2. An example of using an array of objects in a managed class

Implementing the CMyPoint class

// class that describes a point on the coordinate plane
ref class CMyPoint
{
    int x, y;

    public:
    // class constructor
    CMyPoint(void)
    {
        x = y = 0;
    }

    // access methods
    int GetX() { return x; }
    int GetY() { return y; }

    void SetXY(int nx, int ny)
    {
        x = nx;
        y = ny;
    }
};

Implementation of CPolygon class

// class that describes a polygon
ref class CPolygon
{
    int n; // number of points in the polygon
    array <CMyPoint ^> ^mp; // array of points CMyPoint

    public:
    // constructor without parameters
    CPolygon(void);

    // constructor with 2 parameters, which gets an array of points CMyPoint
    CPolygon::CPolygon(int n, array <CMyPoint^> ^mp2);

    // access methods
    CMyPoint^ GetPoint(int index); // returns a point
    void AddPoint(int nx, int ny); // adds a point to a polygone
    double Length(void); // determines the length of the polygone
};

// class constructor
CPolygon::CPolygon(void)
{
    n = 0;

    // allocate memory - only 1 time
    // for an array as a whole
    mp = gcnew array <CMyPoint^>(10);

    // для каждого элемента массива
    for (int i=0; i<10; i++)
        mp[i] = gcnew CMyPoint;
}

// constructor with 2 parameters - initialized with an array of values
CPolygon::CPolygon(int n, array <CMyPoint^> ^mp2)
{
    if (n>10) return;
    this->n = n;

    // assign an array mp = mp2
    for (int i=0; i<n; i++)
    {
        mp[i] = mp2[i];
    }
}

// access methods
// take a point with a given index
CMyPoint^ CPolygon::GetPoint(int index)
{
    return mp[index];
}

// add a point to the polyline
void CPolygon::AddPoint(int nx, int ny)
{
    n++;
    mp[n-1]->SetXY(nx, ny);
}

// determine the length of the polyline
double CPolygon::Length(void)
{
    int x1, y1, x2, y2;
    double len, t;
    CMyPoint^ pt;

    len = 0; // length of polyline

    for (int i=0; i<n-1; i++)
    {
        x1 = mp[i]->GetX();
        y1 = mp[i]->GetY();
        x2 = mp[i+1]->GetX();
        y2 = mp[i+1]->GetY();
        t = System::Math::Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        len += t;
    }
    return len;
}

Demonstrating the use of classes in another program code

// the use of CPolygon class
CPolygon^ cp;
cp = gcnew CPolygon;

// to form a polyline with coordinates (1;2), (3;4), (5;6)
cp->AddPoint(1, 2);
cp->AddPoint(3, 4);
cp->AddPoint(5, 6);

// test
int x, y;
CMyPoint^ pt = gcnew CMyPoint;

pt = cp->GetPoint(0);
x = pt->GetX(); // x = 1
y = pt->GetY(); // y = 2

pt = cp->GetPoint(1);
x = pt->GetX(); // x = 3
y = pt->GetY(); // y = 4

// determine the length of polyline
double d;
d = cp->Length(); // d = 5.6568...

3. An example of using an array of unmanaged pointers to a class object

Implementation of CMyPoint class

class CMyPoint
{
    int x, y;

    public:
    // constructor
    CMyPoint::CMyPoint(void)
    {
        x = y = 0;
    }

    // access methods
    int GetX(void) { return x; }
    int GetY(void) { return y; }

    void SetXY(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
};

Implementation of CPolygon class

// class that describes a polygon
class CPolygon
{
    int n;
    CMyPoint * mp[10]; // array of pointers to CMyPoint

    public:
    // constructor
    CPolygon(void)
    {
        n = 0;

        // memory allocation - only one time
        for (int i=0; i<10; i++)
        {
            mp[i] = new CMyPoint;
        }
    }

    // access methods
    CMyPoint GetPoint(int index)
    {
        return *mp[index];
    }

    void SetPoint(int index, CMyPoint& p)
    {
        int x, y;
        x = p.GetX();
        y = p.GetY();
        mp[index]->SetXY(x, y);
    }

    // add a new point
    void AddPoint(int x, int y)
    {
        mp[n++]->SetXY(x, y);
    }

    // calculation the length of polyline
    double Length(void)
    {
        double len, t;
        int x1, y1, x2, y2;

        len = 0;
        for (int i=0; i<n-1; i++)
        {
            x1 = mp[i]->GetX();
            y1 = mp[i]->GetY();
            x2 = mp[i+1]->GetX();
            y2 = mp[i+1]->GetY();
            t = System::Math::Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            len = len + t;
        }
        return len;
    }
};

A fragment of the method that demonstrates the use of the CPolygon class

// the use of an array of unmanaged pointers to a class object
CPolygon cp; // class object

// to form a polyline of three points
cp.AddPoint(1, 2);
cp.AddPoint(3, 4);
cp.AddPoint(5, 6);

// test
int x, y;
CMyPoint mp;
mp = cp.GetPoint(0);
x = mp.GetX(); // x = 1
y = mp.GetY(); // y = 2

// length of a polyline
double d;
d = cp.Length(); // d = 5.6568...


Related topics