Question

Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the...

Long Answer 2: (Classes and Objects)

Design a class named Box. The class should have the following private members:

width - A double member variablethat holds the width of the box.

length – A double member variable for holding the length of the box.

height – A double member variable of type double that holds the height of the box.

In addition, provide the following member functions with full (inline) implementation.

a) A default constructor that sets all member variables to 0.

b) A constructor that accepts width, length and height and initializes the member variables appropriately.

c) A copy constructor

d) An appropriate set and get functions for width, length and height.

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

Answer: Hey dear student find the solution of your query if you have any doubt feel free to ask. Thanks!!

this C++ program has Box class and three private members. Default constructor parametrized constructor, copy constructor and setters, getters for all data members.

in main, call all member functions and all are working well as you can see the snapshot of the output.

If any query asks in the comment box.thanks.

C++ Code:

#include <iostream>
using namespace std;
class Box //class Box
{

private: //private data members
   double width;
   double length;
   double height;

public:
   Box()//default constructor
   {
       width = 0;
       height = 0;
       length = 0;
   }

   Box(double l,double w,double h)//parameterized constructor that initialized data members
   {
       length = l ;
       height = h;
       width = w;
   }

   Box(const Box &b2)//copy constructor
   {
       width = b2.width;
       height = b2.height;
       length = b2.length;
   }

   void setWidth(double w)//setters for all data members
   {
       width = w;//set width
      
   }
   void setHeight(double h)
   {
       height = h;//set height
   }
   void setLength(double l)
   {
       length = l;//set length
   }
   double getLength()//getters for all data members
   {
       return length;//return length
   }
   double getWidth()
   {
       return width;//return width
   }
   double getHeight()
   {
       return height;//return height
   }
};
int main()
{
Box b1(6.5,3.5,2.5);//object created with called parameterized constructor
Box b2 = b1;//call copy constructor
double l,w,h;//variables to store returning values
b2.setHeight(4.5);//call setters by object2
b2.setLength(7.5);
b2.setWidth(3.0);
l = b2.getLength();//call getters
w = b2.getWidth();
h = b2.getHeight();
cout<<" Box Created ";
cout<<endl;//print info about box
cout<<"Length: "<<l;
cout<<"\nWidth: "<<w;
cout<<"\nHeight: "<<h;
return 0;
}


  

Output:

W 62 1 b2.getLength(); 63 b2.getWidth(); 64 h b2.getHeight(); 65 cout<< Box Created 66 cout<<endl; 67 cout<<Length: <<l; 6

Add a comment
Know the answer?
Add Answer to:
Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the...
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
  • (Classes and Objects) Design a class named Box. The class should have the following private members:...

    (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables to 0. b)...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • 1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - an int variable named year - a string variable named model - a string variable named purpose 1c Add the following public functions: - Default constructor which sets numeric members to -1 and string members to the empty string - Destructor to print "Removing instance from memory" - A non-default constructor to set the year and...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • Design a class named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

  • a. construct a class named room that declares two-double precision data members named length and width....

    a. construct a class named room that declares two-double precision data members named length and width. include a constructor that initializes each datavmember to 1.0 when a room object is created. add two accessor funtions for displaying the length and width values of a room object and two mutator functions that allow changing these data member values. b. include the class written for 6a in the context of a complete program

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

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