Code:
/*Purpose: This program creates a class named box. The class contains the variables
of the box and also the operations on it. This includes the function that sets the size
and prints information about the box. The values and changed and manipulated throughout the
program. */
#include<iostream>
#include<string>
using namespace std;
class box
{
//member function prototypes
public:
void set_size(float,float,float,float,float,string,string);//changes box variables
//the variables are length,width,height,thickness,weight,color,contents (yes or no)
void get_size(); // returns dimensions of the box
void re_enter();
void calc_area(); // calculates a box's outside area
void calc_volume(); // calculates a box's volume
void print_report(); // prints all known info about a box
box(float,float,float,float,float,string,string); //constructor
box(); //default constructor
//member variables
private:
float length; // length of the box (ft)
float width; // width of the box (ft)
float height; // height of the box (ft)
float thickness; // thickness of the box material (in)
float weight; // weight of box (lb)
string color; // color of the box
float volume; //volume of the box
float areaofsideone,areaofsidetwo,areaofsidethree;//area of three different sides
float surfacearea; // surface area
string contents; // whether it contains something (yes or no)
};
void box::calc_volume()
//Precondition: Length, width, and height have been assigned.
//Postcondition: Returns volume of box with variables in parameter list.
{
volume=length*width*height;
}
void box::calc_area()
//Precondition: Length, width, and height have been assigned.
//Postcondition: Returns surface area of box with variables in parameter list.
{
areaofsideone=height*length;
areaofsidetwo=width*length;
areaofsidethree=height*width;
surfacearea=2*areaofsideone+2*areaofsidetwo+2*areaofsidethree;
}
void box::get_size()
//This function returns a box's current dimensions
{
cout<<"The box's current dimensions are: length="<<length<<", width="
<<width<<", and height="<<height<<endl;
}
void box::set_size(float l,float w,float h,float t,float lb,string c,string cont)
//This function changes a box's dimensions and also its other variables
{
if(l<=0||w<=0||h<=0||t<=0||lb<=0) //checking if valid input was entered (>0)
{re_enter();}
length=l;
width=w;
height=h;
thickness=t;
weight=lb;
color=c;
contents=cont;
}
void box::re_enter()
{
float l,w,h,t,lb; //length,width,height,thickness,weight
string c,cont; //color, contents (yes or no)
cout<<"The input you entered is invalid."<<endl;
cout<<"Please re-enter your data as valid inputs: "<<endl;
cin>>l>>w>>h>>t>>lb>>c>>cont;
set_size(l,w,h,t,lb,c,cont);
}
void box::print_report()
//This function prints all known info. about the box
{
cout<<"The box's current dimensions are: length="<<length<<", width="
<<width<<", and height="<<height<<endl;
cout<<"Other box properties include:"<<endl
<<"thickness="<<thickness<<endl
<<"weight="<<weight<<endl
<<"surface area="<<surfacearea<<endl
<<"area of three different sides:"<<areaofsideone<<", "<<areaofsidetwo<<", "
<<areaofsidethree<<endl
<<"volume="<<volume<<endl
<<"color is "<<color<<endl
<<"contents? "<<contents<<endl<<endl;
}
int main()
{
float l,w,h,t,lb; //length,width,height,thickness,weight
string c,cont; //color, contents (yes or no)
//box box1; //name of the first box
box box1(1.5,3.4,0.9,0.1,2.7,"brown","no");
box1.calc_area();
box1.calc_volume();
box1.print_report();
//just testing to see if I get some output going before I write the rest of the program
return 0;
}