Answer to Question #329772 in C++ for zaira

Question #329772

     Write a class Box with three variables length, width and height. Design suitable constructors, set and get functions. Write the main function and create the different objects of Box class. We want to count the objects of Box class what more you can add in your class to achieve the counting facility. Write the code and display the number of object created so far.



1
Expert's answer
2022-04-17T11:03:39-0400
#include<iostream>

using namespace std;

class Box
{
	float length;
	float width;
	float height;
	static int count;
public:
	Box(float _length=0, float _width=0, float _height=0)
	:length(_length), width(_width), height(_height)
	{
		AddCount();
	}
	void SetLength(float l) { length = l;}
	void SetWidth(float w) { width = w; }
	void SetHeigth(float h) { height = h; }
	float GetLength() { return length; }
	float GetWidth() { return width; }
	float GetHeight() { return height; }
	static void AddCount() { count++; }
	void ShowCount() {cout<<count; }
};

int Box::count = 0;

int main()
{
	Box a(2.3, 5.6, 7.8);
	cout<<"Length of a: "<<a.GetLength()<<endl;
	cout << "Width of a: " << a.GetWidth() << endl;
	cout << "Height of a: " << a.GetHeight() << endl;
	cout << endl;
	Box b;
	b.SetLength(5);
	b.SetWidth(9);
	b.SetHeigth(11);
	cout << "Length of b: " << b.GetLength() << endl;
	cout << "Width of b: " << b.GetWidth() << endl;
	cout << "Height of b: " << b.GetHeight() << endl;
	cout << "Count of boxes is ";
	b.ShowCount();
}







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