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.
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;
}
}
Box It!
You are viewing a single comment's thread. Return to all comments →
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; } };`