Question

(Demonstrate the Box class from above in a program), (Extra 5 Credits ). Re-use your code...

(Demonstrate the Box class from above in a program), (Extra 5 Credits ). Re-use your code from the previous question, add the following functions. Each function worth 1 point. You dont have to implement in order.

1) Overloaded cout’s << that displays attributes of a box. Also overload cin’s >> operator to work with this class for constructing a Box instance using values supplied as console inputs.

2) Overloaded == operator as a member function that returns true if two Boxs are equal, returns false otherwise. (Note – two Box objects are equal if the width, length and height of the two are equal to each other. i.e. length1 == length2 ... etc)

3) A member functions that calculates the outside surface area (SA) of the box. (Note - SA = 2*length*width + 2*length * height + 2 * width * height)

4) A member function that returns the volume of a box. (Volume = length*width*height)

5) A member function that returns true if a box is a cube. (Note – length == height == width if a box is also a cube)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

class Box{
   private:
       double width, length, height;
   public:
       Box();
       Box(double w, double l, double h);
       Box(const Box& b);
      
       void setWidth(double w);
       void setLength(double l);
       void setHeight(double h);
      
       double getWidth() const;
       double getLength() const;
       double getHeight() const;
      
       // PART 2 STARTS HERE
       friend ostream& operator<<(ostream& out, const Box&b);
       friend istream& operator>>(istream& in, Box &b);
       bool operator==(const Box&b1);
       double surfaceArea() const;
       double volume() const;
       bool isCube() const;
};

Box::Box(): width(0), length(0), height(0){
}
Box::Box(double w, double l, double h):
   width(w), length(l), height(h){
}
  
Box::Box(const Box& b){
   width = b.width;
   length = b.length;
   height = b.height;
}

void Box::setWidth(double w){width =w;}
void Box::setLength(double l){length =l;}
void Box::setHeight(double h){height=h;}

double Box::getWidth() const{return width;}
double Box::getLength() const{return length;}
double Box::getHeight() const{return height;}

// IMPLEMENTATION OF PART 2 STARTS HERE
ostream& operator<<(ostream& out, const Box& b){
   out<<"Width = "<<b.getWidth() <<", Length = "<<b.getLength()
       <<", Height = "<<b.getHeight() << endl;
   return out;
}

istream& operator>>(istream& in, Box &b){
   in >> b.width >> b.length >> b.height;
   return in;
}

bool Box::operator==(const Box& b){
   return width ==b.width && length ==b.length && height ==b.height;
}

double Box::surfaceArea() const{return 2*(width*length + length*height + height*width);}
double Box::volume() const{return width*length*height;}
bool Box::isCube() const{return width==length && width ==height;}



=======================================================================

Add a comment
Know the answer?
Add Answer to:
(Demonstrate the Box class from above in a program), (Extra 5 Credits ). Re-use your code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • Write a statement that declares sObj to be an object of type strange such that the...

    Write a statement that declares sObj to be an object of type strange such that the private member variables a and b are of type int. Write statement that shows the the declaration in the class strange to overload the operator == as a member function. Assume that two objects of type strange are equal if their corresponding member variables are equal. Write the definition of the function operator== for the class strange, which is overloaded as a member function....

  • I need help solving this basic C++ question. I need help fixing my code.   Question: Design...

    I need help solving this basic C++ question. I need help fixing my code.   Question: Design a class called a box that represents a box. Box classes have variables such as the length of the box, width, and height. 1. Member variables shall be dedicated members. 2. Define the creator of the Box Class. The creator may receive all of the data and may not receive any. 3. Add accessor and creator 4. Add an empty function, which indicates whether...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • ** Using Python 3 ** Write a Box class whose init method takes three parameters and...

    ** Using Python 3 ** Write a Box class whose init method takes three parameters and uses them to initialize the length, width and height of a Box. It should also have a method named volume that returns the volume of the Box. Write a function named box_sort (not part of the Box class) that uses insertion sort to sort a list of Boxes from greatest volume to least volume.

  • implement a C++ class name Vector. The Vector class contains a private double array of length...

    implement a C++ class name Vector. The Vector class contains a private double array of length 2 named elements that represents a two-dimensional vector. We will also implement an overloaded multiplication operator (*) that accepts a single Vector variable by reference and returns the Dot Product between the current Vector object and the one pointed to by the function argument in form of a double. Please recall that the dot product of two vectors a =(21,92) and 5 = (b1,b2)...

  • code in c++ Question 3 [30 Marks In architectural drawings, the distances are measured in feet...

    code in c++ Question 3 [30 Marks In architectural drawings, the distances are measured in feet and inches according to the English system of measurement. There are 12 inches in a foot. The length of a living room, for example, might be given as 15–8", meaning 15 feet plus 8 inches. The hyphen isn't a negative sign; it merely separates the feet from the inches. Figure 1 shows typical length measurements in the English system. Suppose you want to create...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT