3. (28 points) Class Design (1) (17 points) Define a class called Box. Your clas
ID: 3723406 • Letter: 3
Question
3. (28 points) Class Design (1) (17 points) Define a class called Box. Your class will have member variables of type int to represent the width, length, and height of a box. The class design should meet the encapsulation 00P principles and also include the following member functions. You need to write function definition for the declared functions; and NONE of these functions should be inline! (a) a constructor to set the width, length, and height; a default constructor; ) to save time, only write the accessor and mutator functions for the length of the ox; and then you may assume all other accessor and mutator functions are defined may call them later when needed d) a member function that calculates the volume of the box, and this function ld be accessible from anywhere the object is visible.Explanation / Answer
1)
class Box {
private:
int width;
int length;
int height;
public:
Box();
Box(int w, int l, int h);
int getLength();
void setLength(int l);
int volume();
};
Box::Box() {
width = 0;
length = 0;
height = 0;
}
Box::Box(int w, int l, int h) {
width = w;
length = l;
height = h;
}
int Box::getLength() {
return length;
}
void Box::setLength(int l) {
length = l;
}
int Box::volume() {
return length*width*height;
}
2)
Box box1(1, 2, 3); will create a object with width of 1, length of 2 and height of 3
Box box2; will give compilation since there is no default constrcutor in Box class
3)
class ShippingOrder {
private:
Box box;
double postage;
public:
ShippingOrder(Box b, double p) {
box = b;
postage = p;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.