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.
classBox{//for people who are learningprivate:intl;intb;inth;public://Getter functions for Box object attributesintgetLength()const{//const shows it does not modify objectreturnl;}intgetBreadth()const{returnb;}intgetHeight()const{returnh;}//volume calculationlonglongCalculateVolume()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 parametersas that causes the multiplication to take place first.*/returnstatic_cast<longlong>(l)*b*h;}// Constructors - Topic overloading constructors with differerent parametersBox(intlength,intbreadth,intheight):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 '<' operatorbooloperator<(constBox&other)const{//again const shows we are not modifying either objif(l<other.l)returntrue;if(l==other.l&&b<other.b)returntrue;if(l==other.l&&b==other.b&&h<other.h)returntrue;returnfalse;}//Declare friend function for opp<< that exists outside the class.friendostream&operator<<(ostream&out,constBox&B);};// The friend we refered to in the classostream&operator<<(ostream&out,constBox&B){//its our friend so it can see our privates :pout<<B.l<<' '<<B.b<<' '<<B.h;returnout;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Box It!
You are viewing a single comment's thread. Return to all comments →