0

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.
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Quack
  • 133
  • 7
  • 2
    the copy constructor is used to copy instances. Its not clear how the last part of code is related to your quesiton, there is no copy being made – 463035818_is_not_an_ai May 20 '21 at 18:11
  • 3
    Unrelated to your problem but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude May 20 '21 at 18:12
  • 2
    dont expect coding competition sites to teach you the basics, they don't. See eg here https://en.cppreference.com/w/cpp/language/copy_constructor – 463035818_is_not_an_ai May 20 '21 at 18:13
  • The copy-constructor is typically invoked when you do something like `Box b3 = b1;` or call a function where a `Box` object is passed by value. – Some programmer dude May 20 '21 at 18:13
  • 2
    Note: You probably should read about initialization lists. – EL_9 May 20 '21 at 18:15
  • if you know how the `Box(int length,int breadth,int height)` constructor works then the copy constructor is actually not that different. Details aside, the main difference is just different parameters – 463035818_is_not_an_ai May 20 '21 at 18:16
  • And I really recommend investing in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Or perhaps even take some classes. – Some programmer dude May 20 '21 at 18:18
  • @Someprogrammerdude It's not my code, i don't normally include that i just pasted the working answer provided by hackerrank as i was trying to reverse engineer it to understand what it does but i appreciate the heads up! – Quack May 20 '21 at 18:20
  • 2
    Competition code is designed to win a competition, not be used as an educational aid. Often the code takes advantage of knowing how the competition works and the underlying judging system to exploit [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub). Approach it with caution. – user4581301 May 20 '21 at 18:36
  • @user4581301 Thanks for the heads up. I just have no idea how else to practice code. I've taken an online course but not sure where else to go from here other than these kinds of sites. My knowledge isn't good enough to understand projects on Github yet. – Quack May 20 '21 at 18:47
  • My suggestion is to start sniffing around an open source project that A) interests you, B) is interested in mentoring a new programmer, and C) has a compatible culture. You can get a feel for C by reading thorough the dev forums and their bug tracker. If the read through and come to the conclusion that, "These people are flaming s!" that's probably not the right project to learn the journeyman skills from. – user4581301 May 20 '21 at 18:53

1 Answers1

1

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?

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • 1
    @largest_prime_is_463035818 Thanks for pointing it out. I clarified that point. – D-RAJ May 20 '21 at 18:42
  • Sorry i still have no idea. Are you saying that the copy constructor makes it so that i can pass an object of the Box class back in to itself..? Where are the values declared? What is the point? Im so confused. – Quack May 20 '21 at 18:45
  • @Quack Imagine two similar cups. One is filled with water + food coloring and the other is empty. So copy constructor, basically copies the water in the full cup to the empty cup. And now you have two similar cups with the similar content in them. – D-RAJ May 20 '21 at 18:47