We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Box() : l(0), b(0), h(0) {}
Box(int l, int b, int h) : l(l), b(b), h(h) {}
Box(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 static_cast<long long>(l) * b * h; }
bool operator < (Box &B)
{
if(l < B.l || (b < B.b && l == B.l) || (h < B.h && b == B.b && l == B.l))
{ return true;}
else { return false; }
}
friend ostream& operator<<(ostream& out, const Box& B);
};
Things I learned:
1. writing concise, readable code with functions
2. having to return an STDOUT/IN function by reference
3.I learned that returning a referenced stringstream cannot insert into STDOUT/IN functions like cin and cout, but only to sstream instances. So istream and ostream are generally better, unless you plan to only use sstream.
4. having to use a non-member friend function (prototyped inside the class), when overloading bit shifting operators (<<&>>).
This is because Inside of classes (as member functions), binary operator overloads take only one operand (parameter) and assume that it's the right operand. implicitly, the object itself (*this) is the left operand.
Outside of classes (as non-member or friend functions), operator overloads take as many operands as needed for the operation (one for unary operators, two for binary operators, etc.). and can specify order of the operands.
Basically, by default, operator overloads inside of the class assume that the object called, (*this) is on the left operand. By making a friend function outside the class, you can specify that the object in question is the right operand, and that the left is now the stream object.
Box It!
You are viewing a single comment's thread. Return to all comments →
include
using namespace std;
include
include
class Box { private: int l,b,h;
public:
};
Things I learned: 1. writing concise, readable code with functions
ostream& operator<<(ostream& out, const Box& B) { out << B.l << " " << B.b << " " << B.h; return out; }