//Creating and using boxes
#include <iostream> using std::cout; using std::endl;
class CBox //Class definition
{
Public: double m_Length; //Length of box in inches double m_Width; //Width of box in inches double m_Height; //Height of box in inches
//Function to calculate volume of boxes double Volume(); { Return m_Length*m_Width*m_Height; }
};
int main()
{
CBox box1; //Declare box 1 of type CBox CBox box2; //Declare box 2 of type CBox CBox box3; //Declare box 3 of type CBox
double boxVolume=0.0; //Stores box volume
box1.m_Height=18.0; //Define values box1.m_Length=78.0; //of of the members of box1.m_Width=24.0; //the object box1
box2.m_Height= box1.m_Height-10; //Define …show more content…
box2.m_Length= box1.m_Length/2.0; //members box2.m_Width= 0.25*box1.m_Length;
box3.m_Height= box1.m_Height-15; //Define box3.m_Length= box1.m_Length/2.5 //members box3.m_Width= 0.35*box1.m_Length;
//Calculate volume of box1 boxVolume= box1.Volume();
cout<<endl <<”Volume of box1 =”<<boxVolume;
//Calculate volume of box2 cout<<endl <<”Volume of box2=” <<box2.Volume();
//Calculate volume of box3 cout<<endl <<”Volume of box3=” <<box3.Volume();
cout<<endl <<”box3 has sides which total” <<box3.m_Height+box3.m_Length+box3.m_Width <<”inches.”;
cout<< endl <<”A CBox object occupies” <<sizeof box1<<”bytes”;
cout <<endl return 0;
}
.
Under what circumstances must you use classes?
Classes are useful for length programs which require large amounts of similar code. Classes can accelerate program development by reducing redundant code, testing, and debugging. Using classes also makes the code easier to read and follow for other programmers.
Why do you use a class instead of a structure? Are they interchangeable? Explain your answer.
The main difference between a class and a structure in C++ is that structs have default pblic members and basses and classes have default private members and bases. Classes and structs can contain a mixture of public and private members, use inheritance, and can have member functions. For the most part classes and structs are interchangeable, however, there are some instances where they are not which could lead to a compiling error.
Are classes more useful than structures? Why?
Although classes and structures are very similar classes are more useful because with a structure each variable is permanently bound to an individual instance where as classes are reference types which can refer to various class instances.
RUN
IMAGE: