1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
ofstream outAnswer;
enum color{blue, red , green,black}; //color data type for box
enum material{metal, wood, plastic};
class BOX //class definition
{
public:
BOX (double, double, double, double, double, int); //contructor function
~BOX(); //destructr function
void set_size( double, double, double, double, double,int); // changes l,w,h,th, wt,color
double calc_area(); //for area
double calc_volume(); //for volume
void print_report(); // prints report of box
protected: // Member variables
double length;
double width;
double height;
double thickness;
double weight;
int color;
};
BOX::BOX (double l, double w, double h, double th, double wt, int c)
{ //constuctor
length=l;
width=w;
height=h;
thickness=th;
weight=wt;
color=c;
}
BOX::~BOX() //destuctor
{
outAnswer << " Destroying contents of BOX object" << endl;
}
void BOX::set_size (double l, double w, double h, double th, double wt, int c) //Changes values for variables
{
length=l;
width=w;
height=h;
thickness=th;
weight=wt;
color=c;
string colorname;
switch (color)
{ // switch statement used to print out color of box
case 0:
colorname="blue";
break;
case 1:
colorname="red";
break;
case 2:
colorname="green";
break;
case 3:
colorname="black";
}
outAnswer.open("text.txt");
outAnswer << " Size has been reset to " << length << " x " << width << " x " << height
<< endl << " Thickness = " << thickness << endl << " Weight = " << weight << endl << " Color: "
<< colorname << endl;
outAnswer.close();
}
double BOX::calc_area() //Calculates the area
{
return (2*(length*width)+2*(height*length)+2*(height*width));
}
double BOX::calc_volume() // Calculates the volume
{
return (length*width*height);
}
void BOX::print_report() // Prints out report of the box
{
string colorname;
switch (color){ // switch statement used to printout color of box
case 0:
colorname="blue";
break;
case 1:
colorname="red";
break;
case 2:
colorname="green";
break;
case 3:
colorname="black";
}
outAnswer.open("text.txt", ofstream::in | ofstream::out | ofstream::app);
outAnswer << " L x W x H = " << length << " x " << width << " x " << height << " , V = " << calc_volume() << endl << " Area = " <<
calc_area() << endl << " Thickness = " << thickness << endl << " Weight = " << weight << endl << " Color: "
<< colorname << endl;
outAnswer.close();
}
class BETTERBOX:BOX
{
public:
BETTERBOX( int);
void exceed();
double sumdim();
void printreport();
void setsize( int);
char ex;
int material;
};
double BETTERBOX::sumdim()
{
return (length +width+ height);
}
void BETTERBOX::exceed()
{
if ((length + width + height) > 65 )
ex= 'y';
else
ex= 'n';
}
void BETTERBOX::printreport()
{
string matname;
switch (material)
{
case 0:
matname="metal";
break;
case 1:
matname="wood";
break;
case 2:
matname="plastic";
}
outAnswer.open("text.txt", ofstream::in | ofstream::out | ofstream::app);
outAnswer << "Sum of the dimensions: " << sumdim() << endl << " Dimensions exeeed 65: "<< ex << endl << " Material: " << matname << endl;
outAnswer.close();
}
void BETTERBOX::setsize (int m )
{
string matname;
material = m;
switch (m)
{
case 0:
matname="metal";
break;
case 1:
matname="wood";
break;
case 2:
matname="plastic";
}
outAnswer.open("text.txt", ofstream::in | ofstream::out | ofstream::app);
outAnswer << " Material: " << matname << endl;
outAnswer .close();
}
int main()
{
outAnswer.open("text.txt", ofstream::in | ofstream::out | ofstream::app);
outAnswer.setf(ios::fixed, ios:: floatfield); // set up floating point capability
outAnswer.setf(ios::showpoint); // set up floating point capability
outAnswer << setprecision(2); // decimal up to two places
outAnswer.close();
BOX Bob (7.5, 2.75, 1.8, 0.1, 33.5, blue ); //First box changing height
outAnswer << " For Box named Bob: " << endl;
Bob.print_report();
Bob.set_size(7.5,2.75, 0.75,0.1,33.5, blue);
Bob.print_report();
BOX John (45.0, 32.0, 6.33, 0.15, 530.0, red); //Second box changing length and thickness
outAnswer << " For Box named John: " << endl;
John.print_report();
John.set_size(11.0, 32.0, 6.33, 0.23, 530.0, green);
John.print_report();
BOX Dan (60.0, 30.0, 20.0, 0.80,400.0, black );
BETTERBOX Dan1( wood); // Third box
outAnswer << " For Better Box named Dan: " << endl;
Dan.print_report();
Dan1.printreport();
Dan.set_size( 30.0, 15.5, 10.0, 0.40, 250.0, blue);
Dan1.setsize(wood);
Dan.print_report();
Dan1.printreport();
outAnswer << "Thanks and goodbye" << endl;
return 0;
}
|