Explore BrainMass

Explore BrainMass

    Constructor, Object and Accessor Functions in C++

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Given below is a c++ class called Sbox used in shipping. We can declare an object of type Sbox to hold the dimensions of a shipping box, a height, width and depth. We define a height (h), width (w) and depth (d) for the three sides of a box. For shipping, it is required the height to be no greater than 45 inches. The width cannot be anymore than 75% of the box's height. The depth must be less than the height.

    Class Sbox
    {
    Private:
    Int h, w, d; // height width depth

    10. Write a constructor function that
    A) defines an object with default value of 0s
    {
    public:
    Sbox ()
    .............
    };

    Sbox::Sbox()
    { h=0; w=0; d=0;}

    b) define an object with the initial values for h, w, and d. check for the constraints listed earlier
    Sbox::Sbox (int h, int w, int d)
    {
    if (h<45 && (w<h* 0.75) && d<h)
    {
    this->h=h;
    this->w=w;
    this->d=d;
    }
    else {
    this->h=0;
    this->w=0;
    this->d=0;
    }
    }

    11. Write accessor funtions for height, width, and depth.
    Also, what exactly is an accessor function?

    © BrainMass Inc. brainmass.com March 5, 2021, 1:01 am ad1c9bdddf
    https://brainmass.com/computer-science/cpp/constructor-object-accessor-functions-556428

    Solution Preview

    assess function is as follows:

    public:
    Sbox(int h=0, int w=0, int d=0)
    {
    height = h;
    width = w;
    depth = d;
    }
    ...

    Solution Summary

    The solution gives complete C++ code of accessor funtions for height, width, and depth.

    $2.49

    ADVERTISEMENT