Question

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 box is empty or not. It also adds the getVolume () member function to compute the volume. Include a print function that outputs the current state of the Box Object to the console.
5. Create multiple BOX objects from the main () and invoke the accessor and set up.

and this is the code I made so far... (It's not running yet)

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

#include <iostream>

using namespace std;

class Box

{

Box() : length(0), width(0), height(0) {}

Box(int l, int w, int h): length(l), width(w), height(h) {}

int getLength()

{

return length;

}

int getWidth()

{

return width;

}

int getHeight()

{

return height;

}

int setter(int l, int w, int h)

{

length = l;

width = w;

height = h;

}

bool empty()

{

if (length == 0 || width == 0 || height == 0)

return true;

return false;

}

int getVolume(int length, int width, int height)

{

int volume = length * width * height;

return volume;

}

void print(int length, int width, int height)

{

cout << "Length " << length << endl;

cout << "Width: " << width << endl;

cout << "Length: " << length << endl;

}

private:

int length;

int width;

int height;

};

int main()

{

Box a, b;

a.setter(0, 0, 0);

cout << "Box #1"<<endl;

a.print(a.getLength(), a.getWidth(), a.getHeight());

cout << "Volume: " <<

b.setter(3, 2, 4);

cout << "Box #2" << endl;

b.print(b.getLength(), b.getWidth(), b.getHeight());

cout <<"Volume: " <<

}

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

Thanks

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

There were some errors, which I have fixed.

Output screenshot:

M1706611:CompCoding aa048326 gcc test.c M1706611:CompCoding aa0483265 ./a.out Enter the number of elements of array:5 Enter t

code:

#include <iostream>

using namespace std;

class Box{

private:

int length;

int width;

int height;

public:

Box() : length(0), width(0), height(0) {}

Box(int l, int w, int h): length(l), width(w), height(h) {}

int getLength(){

return length;

}

int getWidth(){

return width;

}

int getHeight(){

return height;

}

void setter(int l, int w, int h){

length = l;

width = w;

height = h;

}

bool empty(){

if (length == 0 || width == 0 || height == 0)

return true;

return false;

}

int getVolume(int length, int width, int height){

int volume = length * width * height;

return volume;

}

void print(int length, int width, int height){

cout << "Length " << length << endl;

cout << "Width: " << width << endl;

cout << "Length: " << length << endl;

}

};

int main(){

Box a, b;

a.setter(0, 0, 0);

cout << "Box #1"<<endl;

a.print(a.getLength(), a.getWidth(), a.getHeight());

cout << "Volume: "<<a.getVolume(a.getLength(), a.getWidth(), a.getHeight())<<endl;

b.setter(3, 2, 4);

cout << "Box #2" << endl;

b.print(b.getLength(), b.getWidth(), b.getHeight());

cout <<"Volume: "<<a.getVolume(a.getLength(), a.getWidth(), a.getHeight())<<endl;

}

Add a comment
Know the answer?
Add Answer to:
I need help solving this basic C++ question. I need help fixing my code.   Question: Design...
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
  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • 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: •...

  • In the code (C++) below, if you input 2 as length and 4 as width, the...

    In the code (C++) below, if you input 2 as length and 4 as width, the output is: Enter the rectangle's length: 2 Enter the rectangle's width: 4 Length: 2 | Width: 4 | Area: 8 | Perimeter:12 We worked on this code during class, but there were some things that I did not understand. 1) how does compiler read"l" as length, "w" as width, etc.? I thought you had to update it first, like "w=width." 2) i thought you...

  • 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...

  • This is in C++. It is a program demonstrating how a class can be used to...

    This is in C++. It is a program demonstrating how a class can be used to modify a box in different ways/throw invalid arguments. I am having trouble understanding the code as the teacher did not include comments, can someone please add some detailed ones to help? #include<iostream> #include<stdexcept> using namespace std; class Box { double height, width, depth; public:     Box() { height = width = depth = 1.0; } Box(double h, double w, double d) { height =...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

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