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.
I've learned about pass-by-reference as well as the convention of how to write the parameter (int* p and int *p)
Also learned to return the value in a specific data type, need to declare that expictly like return (long long) l * b * h;
I heard in C++ you can define how operators, functions behave depend upon the parameters given, called "overloading"
Realized the code doesn't work without declaring "friend" with operator "<<" overloading. But i don't understand why is that
`
class Box{
private:
//l,b,h are integers representing the dimensions of the box
int l, b, h;
// The class should have the following functions :
// Constructors:
public:
// Box();
Box(){
l = 0;
b = 0;
h = 0;
}
// Box(int,int,int);
Box(int length, int breadth, int height){
l = length;
b = breadth;
h = height;
}
// Box(Box);
// Copy Constructor
Box(const Box& Box2){
l = Box2.l;
b = Box2.b;
h = Box2.h;
}
// int getLength(); // Return box's length
int getLength(){
return l;
}
// int getBreadth (); // Return box's breadth
int getBreadth(){
return b;
}
// int getHeight (); //Return box's height
int getHeight(){
return h;
}
// long long CalculateVolume(); // Return the volume of the box
long long CalculateVolume(){
return (long long)l * b * h;
}
//Overload operator < as specified
//bool operator<(Box& b)
// When objs of this class are compared by "<"
bool operator<(Box& Box2){
if(l < Box2.l || (b < Box2.b && l == Box2.l) || (h < Box2.h && b == Box2.b && l == Box2.l)){
return true;
}else{
return false;
}
}
include
include
include
include
include
include
using namespace std; //Implement the class Box class Box{ private: int l,b,h;
};
void check2() { int n; cin>>n; Box temp; for(int i=0;i>type; if(type ==1) { cout<>l>>b>>h; Box NewBox(l,b,h); temp=NewBox; cout<>l>>b>>h; Box NewBox(l,b,h); if(NewBox
}
int main() { check2(); }
All test case clear
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; }
I've learned about pass-by-reference as well as the convention of how to write the parameter (int* p and int *p)
Also learned to return the value in a specific data type, need to declare that expictly like
return (long long) l * b * h;
I heard in C++ you can define how operators, functions behave depend upon the parameters given, called "overloading"
Realized the code doesn't work without declaring "friend" with operator "<<" overloading. But i don't understand why is that
` class Box{ private: //l,b,h are integers representing the dimensions of the box int l, b, h; // The class should have the following functions : // Constructors: public: // Box(); Box(){ l = 0; b = 0; h = 0; }
//Overload operator << as specified //ostream& operator<<(ostream& out, Box& B) friend ostream& operator<<(ostream& out, Box& Box2){ out << Box2.l << " " << Box2.b << " " << Box2.h; return out; } };`
getLength() and other has to be const: int getLength() const { ...} copy constructor has to accept const reference: Box(const Box& box) {...}