// File: box2a.C // Second implementation of class Box. #include #include #include "box2a.h" // Local function prototypes static void swap(float &, float &) ; // Local function implementations static void swap(float &x, float &y) { float temp ; temp = x ; x = y ; y = temp ; } // The default constructor initializes // the new box with default dimensions Box::Box() { length = 1.0 ; height = 2.0 ; width = 3.0 ; top = up ; } // Alternate constructor, allows client to specify // the dimensions of the box. Box::Box(float x, float y, float z) { length = x ; height = y ; width = z ; top = up ; } // Report vital stats void Box::identify() { if (length == 0.0 || height == 0.0 || width == 0.0) { cout << "I am an invisible box\n" ; } else { cout << "I am a box with length=" << length << ", height=" << height << " and width=" << width << "\n" ; } cout << "I am pointing " ; switch(top) { case front: cout << "to the front" ; break ; case back: cout << "to the back" ; break ; case left: cout << "to the left" ; break ; case right: cout << "to the right" ; break ; case up: cout << "up" ; break ; case down: cout << "down" ; break ; default: cerr << "Oops. Bad direction\n" ; exit(1) ; } cout << "\n" ; } // Compute box volume float Box::volume() { return length * height * width ; } // Double each dimension void Box::grow() { length = length * 2 ; height = height * 2 ; width = width * 2 ; } // Halve each dimension void Box::shrink() { length = length / 2 ; height = height / 2 ; width = width / 2 ; } // turn around x axis void Box::turn() { swap(height, width) ; switch(top) { case front : top = down ; break ; case back : top = up ; break ; case left : break ; case right : break ; case up : top = front ; break ; case down : top = back ; break ; default : cerr << "Oops, internal error in turn\n" ; exit(1) ; } } // spin around y axis void Box::spin() { swap(length, width) ; switch(top) { case front : top = right ; break ; case back : top = left ; break ; case left : top = front ; break ; case right : top = back ; break ; case up : break ; case down : break ; default : cerr << "Oops, internal error in spin\n" ; exit(1) ; } } // rotate around z axis void Box::rotate() { swap(length, height) ; switch(top) { case front : break ; case back : break ; case left : top = down ; break ; case right : top = up ; break ; case up : top = left ; break ; case down : top = right ; break ; default : cerr << "Oops, internal error in rotate\n" ; exit(1) ; } }