/* Tank.c Class implementation for tanks Author: Date: */ /* Import some "standard" classes */ #include #include #include "Tank.h" /* Import the specification of this class - this enables the compiler to check that the specification and this implementation are consistent */ /* Definition of the attributes of the class */ struct tank { double height, width, depth; double safe_cap; double cur_vol; }; /**** Methods for operating on the class ****/ /* Constructor */ Tank ConsTank( double height, double width, double depth ) { Tank t; t = malloc( sizeof(struct tank) ); if ( t == NULL ) { printf("ConsTank: Insufficient memory\n"); } else { t->height = height; t->width = width; t->depth = depth; t->safe_cap = t->cur_vol = 0.0; } return t; } /* Destructor */ void DestroyTank( Tank t ) { free( t ); } /* Obtain the current height */ double Height( Tank t ) { return t->height; } /* Obtain the current width */ double Width( Tank t ) { return t->width; } /* Obtain the current width */ double Depth( Tank t ) { return t->depth; } /* Properties of tanks */ /* Current liquid volume */ double CurVol( Tank t ) { return t->cur_vol; } /* Remaining liquid volume */ double RemVol( Tank t ) { return t->safe_cap - t->cur_vol; } /* Set the safe capacity */ int SetSafeCap( Tank t, double cap ) { if ( cap < (t->height*t->width*t->depth) ) { t->safe_cap = cap; return TRUE; } else return FALSE; } /* Fill the tank */ int Fill( Tank t, double vol ) { if ( (t->cur_vol + vol) <= (t->safe_cap) ) { t->cur_vol = t->cur_vol + vol; return TRUE; } else return FALSE; } /* Drain the tank */ int Drain( Tank t, double vol ) { if ( t->cur_vol >= vol ) { t->cur_vol = t->cur_vol - vol; return TRUE; } else return FALSE; }