Answer to Question #258934 in C++ for zain

Question #258934

1.      You are required to create another class cuboid that inherits from rectangle. Create a header file called “cuboid.h” and implementation file called “cuboid.cpp”.

a.     This class has width and length but it also has height.

b.     Create accessor and mutator of this class

c.     Create the following functions

                                                             i.     Compute surface area.

                                                           ii.     Overload the function from c-I to compute surface area of parallel sides

                                                         iii.     Overload the function from c-I to compute total surface area.

                                                         iv.     Compute volume.

                                                           v.     Compute the length and height from center of diagonals.


 


1
Expert's answer
2021-10-30T10:37:33-0400
#include <iostream>
//#include "cuboid.h"


using namespace std;


//Base class Rectangle
class Rectangle
{
 public:
     double ZERO = 0.0;


     Rectangle(){
     setLength(ZERO);
     setWidth(ZERO);
     }
     Rectangle(double length2, double width2){
     setLength(length2);
     setWidth(width2);
     }
     //set function
     void setLength(double length2){
     //checking if the length value is negative number
     if(length2< ZERO){
        length2 = - length2;
     }
     length = length2;
     }
     void setWidth(double width2){
         if(width2 < ZERO){
            width2 = -width2;
         }
         width = width2;
     }
     //get function
     double getLength() const {
     return length;
     }
     //get function
     double getWidth() const{
     return width;
     }
     //calculate area of Rectangle
     double area() const{
     return length * width;
     }


protected: //note this is "protected" and not "private"
   double length;
   double width;


}; //end of class Rectangle
class Cuboid: public Rectangle{
public:
    //default constructor are overloaded
    Cuboid(): Rectangle(ZERO, ZERO){
    setHeight(ZERO);
    }
     Cuboid(double length2, double width2, double height2): Rectangle(length2, width2){
      setHeight(height2);
   }
   //returns name of class
   char const * name() const{
      return "Rectangular Cuboid";
   }
    //setFunction
    void setHeight(double height2){
    //checking for negative numbers
    if(height2 < ZERO){
        height2 = -height2;
    }
    height = height2;
    }
    //get function
    double getHeight() const{
    return height;
    }
    //Override Rectangle's area() function
    //to get the the surface area of the Rectangular Cuboid
    double area() const{
    return 2*(length*width+height*length+height*width);
    }
    //calculate volume of a cuboid
    double volume() const{
    return length*width*height;
    }


     /*
    Function Definition for operator<< for output with cout<<
   */
   friend ostream &operator<<(ostream &output, const Cuboid & cubo){
      output<<cubo.name()<<" with length = "<<cubo.length<<", width = "<<cubo.width<<", height = "<<cubo.height<<", surface area = "<<cubo.area()<<", volume = "<<cubo.volume();
      return output;
   }


/*
Note this is "protected" and not "private",
just in case we have another derived class.
*/
protected:
   double height; //new data member, in addition to length and width
};




int main()
{
   //Cuboid test
   Cuboid cubo1;
   cout<<cubo1<<endl;
   Cuboid cubo2(-3, -4, 5);
   cout<<cubo2<<endl;
   cubo2.setHeight(10);
   cout<<cubo2<<endl;
   double height = cubo2.getHeight();
   cout<<"height = "<<height<<endl<<endl;


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS