Quack
Quack

Reputation: 133

Assistance with understanding Copy Constructors

I'm attempting to tackle some Hackerrank challenges, and i've just learned about copy constructors. However i can't get my head around its use. Here is the completed working code.

#include<bits/stdc++.h>

using namespace std;
class Box
{
    private:
        int l,b,h;
    
    public:
        Box()
        {
            l = b = h = 0;
        }
        Box(int length,int breadth,int height)
        {
            l = length;
            b = breadth;
            h = height;
        }
        Box(const Box& B)
        {
            l = B.l;
            b = B.b;
            h = B.h;
        }

        int getLength()
        {
            return(l);
        }
        int getBreadth()
        {
            return(b);
        }
        int getHeight()
        {
            return(h);
        }

        long long CalculateVolume()
        {
            return ((long long)l*b*h);
        }

        friend bool operator < (Box& b1, Box& b2)
        {
            if((b1.l < b2.l) || (b1.l == b2.l && b1.b < b2.b) ||
               (b1.l == b2.l && b1.b == b2.b && b1.h <b2.h))
                return(true);
            else
                return(false);
        }

        friend ostream& operator << (ostream& s,Box& b1)
        {
            s << b1.l << " " << b1.b << " " << b1.h;
            return s;
        }
};

It's the

Box(const Box& B)
{
l = B.l;
b = B.b;
h = B.h;
}

That i can't get my head around. What is this doing? Have we just created a new version of the object inside the class? Or is it a class within a class or something? Why is it a reference? I really have no clue what its purpose is.

I figured once the class is set up, i would create objects such as Box b1 and Box b2, and use the functions to determine its volume and compare those two boxes.

The problem states that the outcomes should be the following:

Box b1; // Should set b1.l = b1.b = b1.h = 0;

Box b2(2, 3, 4); // Should set b1.l = 2, b1.b = 3, b1.h = 4;

b2.getLength(); // Should return 2

b2.getBreadth(); // Should return 3

b2.getheight(); // Should return 4

b2.CalculateVolume(); // Should return 24

bool x = (b1 < b2); // Should return true based on the conditions given

cout<<b2; // Should print 2 3 4 in order.

Upvotes: 0

Views: 78

Answers (1)

D-RAJ
D-RAJ

Reputation: 3372

The copy constructor is pretty straight forward. Construct the object by copying data from another class of the same type. Usually copy construction is done like this,

Box b1;
Box b2 = b1;  // Here the copy constructor is called for the b2 object.
Box b3 = Box(b1);  // Here the copy constructor is called explicitly for the b3 object.

Have we just created a new version of the object inside the class?

No, we didn't create a new version of the object inside the class, we just used data stored in another object (of the same type) to initialize the data of the class. That's why in the copy constructor, we assign the values of the other instance (which is B in the definition) to the object's member variables (l, b and h).

Why is it a reference?

In order to make a copy constructor, the object constructor should accept another object of the same type as a lvalue variable. To define a variable as lvalue, it should look like this,

const int value& = 10;

Note
By default, the C++ compiler defines copy and move constructors for objects if you haven't defined them explicitly. Also as the comment section suggests, make sure to look into Why should I not #include <bits/stdc++.h>? and Why is "using namespace std;" considered bad practice?

Upvotes: 1

Related Questions