I\'m having some trouble with this C++ problem. Write a Cube class that has the
ID: 3755361 • Letter: I
Question
I'm having some trouble with this C++ problem.
Write a Cube class that has the following fields:
edgeLength: a double representing the edge length of the cube.
The class should have the following methods:
Constructor. Accepts the edge length of the cube as an argument.
Constructor. A no-arg constructor that sets the edgeLength field to 0.0.
setEdgeLength. A mutator method for the edgeLength field.
getEdgeLength. A accessor method for the edgeLength field.
getSurfaceArea. Returns the surface area of the cube, which is calculated as surface area = 6 * edgeLength * edgeLength
getVolume. Returns the volume of the cube, which is calculated as volume = edgeLength * edgeLength * edgeLength
getFaceDiagonal. Returns the face diagonal of the cube, which is calculated as face diagonal = 2 * edgeLength
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
main.cpp
--------
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Cube{
private:
double edgeLength;
public:
Cube(double len){
edgeLength = len;
}
Cube(){
edgeLength = 0.0;
}
void setEdgeLength(double len){
edgeLength = len;
}
double getEdgeLength(){
return edgeLength;
}
double getSurfaceArea(){
return 6 * edgeLength * edgeLength;
}
double getVolume(){
return edgeLength * edgeLength * edgeLength;
}
double getFaceDiagonal(){
return sqrt(2) * edgeLength;
}
};
int main(){
Cube c(4);
cout << "created a cube of edge length = " << c.getEdgeLength() << endl;
cout << "surface area = " << c.getSurfaceArea() << endl;
cout << "volume = " << c.getVolume() << endl;
cout << "face diagonal = " << c.getFaceDiagonal() << endl;
}
output
------
created a cube of edge length = 4
surface area = 96
volume = 64
face diagonal = 5.65685
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.