In: Computer Science
Please write this program in C++
Write a program that
- a user-defined class Cuboid which has the following elements:
- default constructor (has no parameters)
- constructor that has 3 parameters (height, length, width)
- data members
- height, length, width
- member functions
- to set the value of each of the data members - 3 functions
- to get the value of each of the data members - 3 functions
- one that returns the volume
- one that returns the surface area
- one that increases each dimension by a specified factor
(Function definitions outside of the class!!!)
- in main()
- instantiate a Cuboid object and initialize at the time of definition
- make sure the constructor with parameters is called
- define an array of 2 Cuboid objects
- using a loop get information to populate the 2 objects in the array
-prompt for the height, length, and width
-prompt for a size by which to increase each dimension(one input)
-call a global function passing a Cuboid object and the increase factor as separate arguments; function will call the class member function to increase the dimensions; parameters of the function should be references
-display the dimensions, the volume, and the surface area for each of the three objects.
#include <iostream>
using namespace std;
//declaring class
class Cuboid {
public:
Cuboid() {}
Cuboid(float height, float
length, float width) {
this ->
height = height;
this ->
length = length;
this -> width
= width;
}
float getHeight();
void setHeight(float height);
float getLength();
void setLength(float length);
float getWidth();
void setWidth(float width);
float getVolume();
float getSurfaceArea();
void increaseDimension(float
factor);
private:
float height, length, width;
};
//defining member functions
float Cuboid::getHeight() {
return this -> height;
}
void Cuboid::setHeight(float height) {
this -> height = height;
}
float Cuboid::getLength() {
return this -> length;
}
void Cuboid::setLength(float length) {
this -> length = length;
}
float Cuboid::getWidth() {
return this -> width;
}
void Cuboid::setWidth(float width) {
this -> width = width;
}
float Cuboid::getVolume() {
return (height*length*width);
}
float Cuboid::getSurfaceArea() {
return 2*((length*height) + (height*width) +
(length*width));
}
void Cuboid::increaseDimension(float factor) {
this -> height = this -> height + factor;
this -> length = this -> length + factor;
this -> width = this -> width + factor;
}
//Global function to increase dimension by a common factor
void increaseDimensionByFactor(Cuboid &c, float &factor)
{
c.increaseDimension(factor); //calls class
member function
}
int main() {
//initiating the first cuboid object with
parameterized constructor
Cuboid c0 = Cuboid(5.1, 6.2, 7.3);
//printing details for c0
cout << "Height for c0 : " <<
c0.getHeight() << "\n";
cout << "Length for c0 : " <<
c0.getLength() << "\n";
cout << "Width for c0 : " << c0.getWidth()
<< "\n";
cout << "Volume for c0 : " <<
c0.getVolume() << "\n";
cout << "Surface Area for c0 : " <<
c0.getSurfaceArea() << "\n";
//defining an array of two Cuboid objects
Cuboid cuboids[2];
float height, length, width, increaseFactor;
//taking input and initializing
for(int i=0; i < sizeof(cuboids)/sizeof(*cuboids);
i++) {
cout << "Enter height of
Cuboid " << i + 1 << " : ";
cin >> height;
cout << "Enter length of
Cuboid " << i + 1 << " : ";
cin >> length;
cout << "Enter width of
Cuboid " << i + 1 << " : ";
cin >> width;
cout << "Enter size by
which to increase each dimension of Cuboid " << i + 1
<< " : ";
cin >> increaseFactor;
cuboids[i] = Cuboid(height, length, width);
//increasing dimension by
factor
increaseDimensionByFactor(cuboids[i], increaseFactor);
//printing details
cout << "Height for c"
<< i + 1 << " : " << cuboids[i].getHeight()
<< "\n";
cout << "Length for c"
<< i + 1 << " : " << cuboids[i].getLength()
<< "\n";
cout << "Width for c"
<< i + 1 << " : " << cuboids[i].getWidth()
<< "\n";
cout << "Volume for c"
<< i + 1 << " : " << cuboids[i].getVolume()
<< "\n";
cout << "Surface Area for c"
<< i + 1 << " : " << cuboids[i].getSurfaceArea()
<< "\n";
}
}