Box It!

Sort by

recency

|

448 Discussions

|

  • + 0 comments
    class Box{
    //for people who are learning
        private:
        int l;
        int b;
        int h;
        
        public:
    //Getter functions for Box object attributes
        int getLength() const { //const shows it does not modify object
            return l;
        }
        int getBreadth() const {
            return b;
        }
        int getHeight() const {
            return h;
        }
        //volume calculation
        long long CalculateVolume() const {
    /*
    We use the static_cast method to prevent overflow,
    just in case the volume exceeds the interger range.
    Note we dont place l * b * h inside the static_cast parameters
    as that causes the multiplication to take place first.
    */
            return static_cast<long long>(l) * b * h;
        }
    // Constructors - Topic overloading constructors with differerent parameters
        Box(int length, int breadth, int height): l(length), b(breadth), h(height){}
        Box(Box& X): l(X.l), b(X.b), h(X.h){}
        Box():l(0), b(0), h(0) {}
        
    // overloading the '<' operator
        bool operator<(const Box& other) const { //again const shows we are not modifying either obj
            if (l < other.l) return true;
            if (l == other.l && b < other.b) return true;
            if (l == other.l && b == other.b && h < other.h) return true;
            return false;
        }
    //Declare friend function for opp<< that exists outside the class.
        friend ostream& operator<<(ostream& out, const Box& B); 
    };   
    
    // The friend we refered to in the class
    ostream& operator<<(ostream& out,const Box& B){
    //its our friend so it can see our privates :p
            out << B.l <<' '<< B.b <<' '<<B.h;
            return out;
    }
    
  • + 0 comments
    class Box
    {   
        //friend bool operator<(Box& A, Box& B);
        friend ostream& operator<<(ostream&out, Box& B);
        private:
            int l;
            int b;
            int h;
        public:
            Box(): l(0), b(0), h(0){};
            Box(const Box& B)
            {
                this->l = B.l;
                this->b = B.b;
                this->h = B.h;
            }
            Box(int l, int b, int h): l(l), b(b), h(h){}
            bool operator<(Box& B)
            {
                if(this->l < B.l)
                {
                    return true;
                }
                else if(this->b < B.b && this->l == B.l)
                {
                    return true;
                }
                else if(this->h < B.h && this->b == B.b && this->l == B.l)
                {
                    return true;
                }
                else 
                    return false;
            }
            bool operator==(Box& B)
            {
                if(this->l == B.l && this->b == B.b && this->h == B.h)
                {
                    return true;
                }
                else 
                {
                    return false;
                }
            }
            Box& operator=(Box& B)
            {
                if(*this == B)
                {
                    return *this;
                }
                this->l = B.l;
                this->b = B.b;
                this->h = B.h;
                
                return *this;
            }
            int getLength()
            {
                return this->l;
            }
            
            int getBreadth()
            {
                return this->b;
            }
            
            int getHeight()
            {
                return this->h;
            }
            long long CalculateVolume()
            {
                return ((long long)(l) * b * h);
            }
    };
    ostream& operator<<(ostream& out, Box& B)
    {
        out << B.l << " " << B.b << " " << B.h << " ";
        return out;
    }
    
  • + 0 comments
    class Box {
    private:
        int length, breadth, height;
    
    public:
        // Constructors
        Box() : length(0), breadth(0), height(0) {}
        Box(int l, int b, int h) : length(l), breadth(b), height(h) {}
        Box(const Box& box) : length(box.length), breadth(box.breadth), height(box.height) {}
    
        // Getter functions
        int getLength() { return length; }
        int getBreadth() { return breadth; }
        int getHeight() { return height; }
    
        // Calculate volume
        long long CalculateVolume() { return static_cast<long long>(length) * breadth * height; }
    
        // Operator overloading <
        bool operator<(const Box& b) {
            if (length < b.length)
                return true;
            else if (length == b.length && breadth < b.breadth)
                return true;
            else if (length == b.length && breadth == b.breadth && height < b.height)
                return true;
            else
                return false;
        }
    
        // Operator overloading <<
        friend ostream& operator<<(ostream& out, const Box& B) {
            out << B.length << " " << B.breadth << " " << B.height;
            return out;
        }
    };
    
  • + 2 comments

    For those who is wondering why test case 3 is not getting passed. Just try to cast volume to long long

    long long CalculateVolume() {
        return (long long)l*b*h;
    }
    
  • + 1 comment

    use the long datatype instead of int for lenght, breadth and height if some test case fails