class Box { private: double lenght, width, height; public: Box(double l = 0.0, d
ID: 3668799 • Letter: C
Question
class Box
{
private:
double lenght, width, height;
public:
Box(double l = 0.0, double w = 0.0, double h = 0.0)
{
lenght = l;
width = w;
height = h;
}
double GetLength() { return lenght; }
double GetWidth() { return width; }
double GetHeight() { return height; }
};
Overload the I/O operators for the above class to be able to achieve the calls in the main function below.
int main()
{
cout << "Enter a Box: " << endl;
Box objBox;
cin >> objBox;
cout << " You entered: " << objBox << endl;
return 0;
}
Please note that the output of your program should be as follows:
Explanation / Answer
//Header file
//Box.h
#ifndef BOX_H
#define BOX_H
#include<iostream>
using namespace std;
//Rational class defintion
class Box
{
private:
double lenght, width, height;
public:
//Default consturctor
Box();
//parameter construcotor
Box(double l, double w , double h );
double GetLength() ;
double GetWidth() ;
double GetHeight() ;
//overload << and >> operators
friend istream &operator>>( istream &input, Box &r );
friend ostream &operator<<( ostream &output, Box &r );
};
#endif BOX_H
-------------------------------------------------------------------------------------------------------------------------------------
//Implementation file of Box.h header file
//Box.cpp
#include<iostream>
#include<iomanip>
//include Box.h header file
#include "Box.h"
using namespace std;
//Defautl constructor to set default values for lenght, width and height
Box::Box()
{
lenght = 0.0;
width = 0.0;
height = 0.0;
}
//Parameter constructor that sets the lenght , widht and height values
Box::Box(double l , double w , double h )
{
lenght = l;
width = w;
height = h;
}
//Returns lenght
double Box::GetLength()
{
return lenght;
}
//Returns widht
double Box::GetWidth()
{
return width;
}
//Returns height
double Box::GetHeight()
{
return height;
}
//Overload insertion operator to read input box lenght , width and height
istream &operator>>( istream &input, Box &box )
{
//to read a operator character
//and then ignore ignore the operator
input>>box.lenght>>box.width>>box.height;
return input;
}
//Overload extraction operator to return the box object
ostream &operator<<( ostream &output, Box &box )
{
output<<"Box Length :"<<box.GetLength()<<
" Width :"<<box.GetWidth()<<
" Height :"<<box.GetHeight();
//return the output object
return output;
}
-----------------------------------------------------------------------------------------------------------------------------
/*C++ program that tests the Box class and read box object
and write to console*/
#include<iostream>
//include Box.h header file
#include "Box.h"
using namespace std;
int main()
{
cout << "Enter a Box: " << endl;
//Create an Box object
Box objBox;
//read box object values
cin >> objBox;
//print the Box object
cout << " You entered: " << objBox << endl;
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------
Sample Output:
Enter a Box:
2 3 4
You entered:
Box Length :2 Width :3 Height :4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.