C++. Class objects as members of the class data. Examples




Class objects as members of the class data. Examples


Contents


Search other websites:

1. An example of declaring and using a class object that is a member of the data of another class

As you know, classes can declare the members of the class data, which are objects of other classes. This example demonstrates the use of two objects that are members of the data class.

In the example, two classes are declared:

  • class CMyPoint defining a point with coordinates x, y;
  • class CMyLine, defining a line containing two objects of type CMyPoint. These objects contain the coordinates of the ends of the segment, of which the line consists.

The CMyPoint class contains:

  • internal members of the data x, y;
  • the default constructor is CMyPoint();
  • methods for reading internal data of class GetX(), GetY();
  • method SetXY(), which sets new values for x, y.

The CMyLine class contains:

  • internal class members of CMyPoint type with the names mp1, mp2. These data members are objects of the CMyPoint class. They determine the 2 points from which the line consists;
  • the default constructor is CMyLine();
  • methods for reading the value of objects mp1, mp2 which have the names GetPoint1(), GetPoint2();
  • the SetLine() method to set a new value for the coordinates of the ends of the CMyLine segment;
  • the Length() method, which calculates the length of a segment.

Declaring and implementing methods of classes CMyPoint and CMyLine

// CMyPoint class declaration
class CMyPoint
{
    int x;
    int y;

    public:
    CMyPoint(void); // default constructor

    // class methods
    int GetX(void)
    {
        return x;
    }

    int GetY(void)
    {
        return y;
    }

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

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

// ----------------------------------------------
// declaration and implementation of methods of class CMyLine
class CMyLine
{
    CMyPoint mp1;
    CMyPoint mp2;

    public:
    // default constructor
    CMyLine(void);

    // access methods
    CMyPoint GetPoint1(void)
    {
        return mp1;
    }

    CMyPoint GetPoint2(void)
    {
        return mp2;
    }

    void SetLine(CMyPoint p1, CMyPoint p2);

    // a method that returns the length of a line
    double Length(void);
};

// implementation of CMyLine class methods
CMyLine::CMyLine(void)
{
    mp1.SetXY(0,0);
    mp2.SetXY(1,1);
}

// set new values for the points
void CMyLine::SetLine(CMyPoint p1, CMyPoint p2)
{
    int tx, ty;
    tx = p1.GetX();
    ty = p1.GetY();
    mp1.SetXY(tx, ty);
    tx = p2.GetX();
    ty = p2.GetY();
    mp2.SetXY(tx, ty);
}

// method that returns the length of the line
double CMyLine::Length(void)
{
    int x1, y1, x2, y2;
    double len;

    x1 = mp1.GetX();
    y1 = mp1.GetY();
    x2 = mp2.GetX();
    y2 = mp2.GetY();
    len = System::Math::Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    return len;
}

Demonstrating the use of the CMyLine class

CMyPoint mp1, mp2;
CMyLine ml;
int d;

// call of CPoint class methods
// filling in the values of objects mp1, mp2
mp1.SetXY(5, 8);
d = mp1.GetX(); // d = 5
mp2.SetXY(9, 11);

// call of methods from CMyLine
ml.SetLine(mp1, mp2); // установить новые значения точек

// calling of method that returns the first point from the line
mp1 = ml.GetPoint2();

// test
d = mp1.GetX(); // d = 9
d = mp1.GetY(); // d = 11

// calculating the length of a segment ml
// calling the Length method
double length;
length = ml.Length(); // length = 5.0

 

2. How is the constructor call performed, if class A uses an object of class B? How is memory allocated for objects of these classes?

If an object of class B is declared in class A, then when you declare an object of class A, the constructors of both classes are called in the following sequence:

  • constructor of class B;
  • constructor of class A;

The compiler generates this sequence of actions automatically. The memory for the data members of the class that are objects of the class is allocated when allocating memory for the object of the class in which these objects are implemented. That is, if we describe an object of class A, in which there is an object of class B, then the memory for the object of class A is allocated with the use of the memory of the object of class B.

For example. If you take the class CMyPoint, which is declared in item 1 of this topic, then when you create an object of the CMyLine class, you first call the corresponding CMyPoint class constructor (memory is allocated), and then the CMyLine class constructor is called.

3. Initialization of the internal object of the class using the constructor. Example

The syntax of the C++ language allows for convenient initialization of the internal class object in the constructor using the symbol : (colon) and calling the constructor of the internal class.

For example, if in some class OuterClass an instance of obj is declared of another class InnerClass

class OuterClass
{
  ...

  // An internal instance of the InnerClass class
  InnerClass obj;

  ...
}

then the constructor of the OuterClass class can initialize this object on the next call

class OuterClass
{
  ...

  // An internal instance of the InnerClass class
  InnerClass obj;

  // Constructor of class OuterClass.

  // This constructor takes as a parameter an instance of _obj of the InnerClass class.
  // In the constructor, the internal object obj is initialized by calling obj (_obj).
  OuterClass(InnerClass _obj) : obj(_obj)
  {
    ...
  }

  ...
}

Example. The example demonstrates the features of initialization of internal objects of type Point, declared in the Line class, namely:

  • initialization of the internal object in the constructor;
  • delegation of authority to another constructor.

The demo text is as follows:

#include <iostream>
using namespace std;

// Class describing a point on a coordinate plane
class Point
{
private:
  int x, y; // Internal class fields

public:
  // Constructor without a parameters
  Point() :Point(0, 0) // delegating authority to a constructor with two parameters
  { }

  // Constructor with two parameters
  Point(int x, int y)
  {
    this->x = x;
    this->y = y;
  }

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

  void SetXY(int _x, int _y)
  {
    x = _x;
    y = _y;
  }

  // Distance from point to origin of coordinates
  double Length()
  {
    return sqrt(x * x + y * y);
  }
};

// The class that implements the line
class Line
{
private:
  // In the Line class, there are two internal objects of the Point class that define the endpoints of the line.
  Point pt1;
  Point pt2;

public:
  // Constructor without a parameters
  Line() : Line(pt1, pt2) // Delegating authority to a constructor with 2 parameters
  { }

  // Internal object initialization via constructor
  Line(Point _pt1, Point _pt2) : pt1(_pt1), pt2(_pt2)
  { }

  // Access methods for internal objects
  Point GetP1() { return pt1; }
  Point GetP2() { return pt2; }
  void Set(Point _pt1, Point _pt2)
  {
    pt1 = _pt1;
    pt2 = _pt2;
  }

  // Method that determines the length of the line
  double Length()
  {
    int x1, y1, x2, y2;
    x1 = pt1.GetX();
    y1 = pt1.GetY();
    x2 = pt2.GetX();
    y2 = pt2.GetY();
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  }
};

void main()
{
  // Initializing Objects in Classes
  Point pt1(1, 1);
  Point pt2(3, 4);

  // Calling a constructor with 2 parameters of the Line class
  Line ln1(pt1, pt2);

  // Display the distance between points pt1, pt2
  cout << "(1,1) - (3,4) => " << ln1.Length() << endl;
}

Program result

(1,1) - (3,4) => 3.60555

 


Related topics