HackerRank C++ Box It!




Design a class named Box whose dimensions are integers and private to the class. The dimensions are labelled: length l , breadth b, and height h .

The default constructor of the class should initialize l,b ,hand to 0 .

The parameterized constructor Box(int length, int breadth, int height) should initialize Box's l,b and h to length, breadth and height.

The copy constructor BoxBox B ) should set l,b and h to B's l,b and h, respectively.

Apart from the above, the class should have 4 functions:

int getLength() - Return box's length
int getBreadth() - Return box's breadth
int getHeight() - Return box's height
long long CalculateVolume() - Return the volume of the box



  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Box {
  4. private:
  5. int length, breadth, height;
  6. public:
  7. Box() {
  8. length = 0;
  9. breadth = 0;
  10. height = 0;
  11. }
  12. Box(int l, int b, int h) {
  13. length = l;
  14. breadth = b;
  15. height = h;
  16. }
  17. Box(const Box &B) {
  18. length = B.length;
  19. breadth = B.breadth;
  20. height = B.height;
  21. }
  22. int getLength() {
  23. return length;
  24. }
  25. int getBreadth() {
  26. return breadth;
  27. }
  28. int getHeight() {
  29. return height;
  30. }
  31. long long CalculateVolume() {
  32. return (long long)(breadth) * length * height;
  33. }
  34. bool operator<(Box &B) {
  35. if (length < B.length) {
  36. return true;
  37. } else if (length == B.length) {
  38. if (breadth < B.breadth) {
  39. return true;
  40. } else if (breadth == B.breadth) {
  41. if (height < B.height) {
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. };
  49. ostream &operator<<(ostream &out, Box &B) {
  50. out << B.getLength() << " " << B.getBreadth() << " " << B.getHeight();
  51. return out;
  52. };
  53. void check2()
  54. {
  55. int n;
  56. cin>>n;
  57. Box temp;
  58. for(int i=0;i<n;i++)
  59. {
  60. int type;
  61. cin>>type;
  62. if(type ==1)
  63. {
  64. cout<<temp<<endl;
  65. }
  66. if(type == 2)
  67. {
  68. int l,b,h;
  69. cin>>l>>b>>h;
  70. Box NewBox(l,b,h);
  71. temp=NewBox;
  72. cout<<temp<<endl;
  73. }
  74. if(type==3)
  75. {
  76. int l,b,h;
  77. cin>>l>>b>>h;
  78. Box NewBox(l,b,h);
  79. if(NewBox<temp)
  80. {
  81. cout<<"Lesser\n";
  82. }
  83. else
  84. {
  85. cout<<"Greater\n";
  86. }
  87. }
  88. if(type==4)
  89. {
  90. cout<<temp.CalculateVolume()<<endl;
  91. }
  92. if(type==5)
  93. {
  94. Box NewBox(temp);
  95. cout<<NewBox<<endl;
  96. }
  97. }
  98. }
  99. int main()
  100. {
  101. check2();
  102. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube