C++. Classes. Part 2. Copy constructor. Examples of using. Passing a class object to a function. Returning a class from a function

C++. Classes. Part 2. Copy constructor. Examples of using. Passing a class object to a function. Returning a class from a function

In this topic, the work of the copy constructor is considered using the example of unmanaged (native) classes. When considering this topic, it is recommended to read the topic:


Contents


 1. What is the purpose of a copy constructor? Example

This is a special constructor which allows you to get the identical object. That is, using the copy constructor, you can get a copy of an existing object. The copy constructor is also called a copy initializer. The copy constructor should receive the reference (&) to the same class. This reference is as input parameter.

An example. Let the class CMyPoint, describing a point on the coordinate plane, be given. The class declares several constructors, including the copy constructor.

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

    public:
    CMyPoint(void); // default constructor
    CMyPoint(int nx, int ny); // constructor with two parameters
    CMyPoint(CMyPoint & ref_Point); // copy constructor

    // Access methods - implemented in the class
    int GetX(void) { return x; }
    int GetY(void) { return y; }
};

// implementation of class constructors (methods)
// CMyPoint class default constructor
CMyPoint::CMyPoint(void)
{
    x = y = 0;
}

// a CMyPoint class constructor with two parameters
CMyPoint::CMyPoint(int nx, int ny)
{
    x = nx;
    y = ny;
}

// copy constructor of CMyPoint class
// a reference to CMyPoint is passed
CMyPoint::CMyPoint(CMyPoint & ref_Point)
{
    // copying data from one object into another
    x = ref_Point.x;
    y = ref_Point.y;
}

Demonstration of the use of the copy constructor in some program code (method)

// demonstrating the use of the copy constructor
CMyPoint p1(5, 8); // creating object p1
CMyPoint p2; // creating object p2 - the default constructor is called

// testing
int d;
d = p2.GetX(); // d = 0
p2 = p1; // the copy constructor is called

// testing
d = p2.Get(); // d = 5 - Data were copied

 

2. When is it useful to use the copy constructor?

The copy constructors are very important. In the class, the copy constructor must be declared in cases, if necessary:

  • create a copy of the class object (see the previous paragraph);
  • pass the object of the class to some function as a parameter-value (see the example below);
  • if you need to return a class object by value.

 

3. Example of passing a class object to a function as a parameter-value

In the example, an object of the CMyPoint class (see paragraph 1) is passed to the GetLength() function, which calculates the distance from the CMyPoint point to the origin. The function text is as follows:

// function that calculates the distance from the point to the origin
// point is an incoming parameter
double GetLength(CMyPoint mp)
{
    double length;
    int tx, ty;

    tx = mp.GetX();
    ty = mp.GetY();
    length = Math::Sqrt(tx*tx + ty*ty);

    return length;
}

Using a function in another program code

CMyPoint p1(5,5); // declare an instance of the CMyPoint class
double len;
len = GetLength(p1); // pass the point p1 to the function, len = 7,07...

 

4. Example of returning a class from a function by value using the copy constructor

Implement the GetCenterPoint() function, which returns a point that is the midpoint of the segment drawn between the CMyPoint point and the origin.

The class declaration is exactly the same as in paragraph 1. Implementation of functions

// function that returns the middle of a line segment
CMyPoint GetCenterPoint(CMyPoint mp)
{
    int tx, ty;
    tx = mp.GetX() / 2;
    ty = mp.GetY() / 2;
    return CMyPoint(tx, ty); // return from function - the temporary object is created
}

// function that returns the midpoint of a segment specified by points
CMyPoint GetCenterPoint2(int x, int y)
{
    return CMyPoint(x/2,y/2); // return from function - the temporary object is created
}

Demonstration of the use of functions

CMyPoint mp(18,-8);
CMyPoint mpC;
mpC = GetCenterPoint(mp); // the copy constructor is called

int cx, cy;
cx = mpC.GetX(); // cx = 9
cy = mpC.GetY(); // cy = -4

mpC = GetCenterPoint2(-9, 13); // the copy constructor is called
cx = mpC.GetX(); // cx = -4
cy = mpC.GetY(); // cy = 6

In the functions GetCenterPoint() and GetCenterPoint2() during the call of the operator

return CMyPoint(...);

a temporary object is created (using the copy constructor). This object is then copied from the function when the assignment is made

mp = GetCenterPoint(...);

again with the use of copy constructor.

 


Related topics