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.
#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();
}
Comments
Leave a comment