C/C++: Perimeter and area of rects. Volume of cuboids

area, c++, syntax, volume

Solution

I had to change the '/' operator to be '+', Since there is no prefix '/' operator. + works great though. Oh and it uses ints. I only tested it once with the case you provided but as far as I could tell it should work.

class cuboid
{
        int w,h,l;
public:
        cuboid () : w(2), h(3), l(6) {}
        cuboid& operator - () { w += 1; return *this; }
        cuboid& operator - (cuboid&) { w += 1; return *this; }
        cuboid& operator -- (int) { w += 2; return *this; }
        cuboid& operator -- () { w += 2; return *this; }
        cuboid& operator ! () { h += 1; return *this; }
        cuboid& operator + () { l += 1; return *this; }
        cuboid& operator + (cuboid&) { l += 1; return *this; }
        cuboid& operator ++ () { l += 2; return *this; }
        cuboid& operator ++ (int) { l += 2; return *this; }

        void clear () { w = 2; h = 3; l = 6; }
        int width () const { return w / 3; }
        int height () const { return h / 3; }
        int length () const { return l / 3; }
        int volume () const { return width() * height () * length (); }
        int surface_area() const { return width() * height () * 2 +
                                          width() * length () * 2 +
                                          length() * height () * 2; }
};

See it in action. http://ideone.com/vDqEm

Edit: You don't need the ++ operators since there shouldn't be two +'s next to each other. Woops.

Problem

I want to calculate area and perimeter of rects using the following code: ``` rect a; a = ( ----- ! ! -----a ); std::cout << a.area() << std::endl; std::cout << a.perimeter() << std::endl; ``` For this purpose I crafted the following class: ``` class rect { public: rect():w(0), h(2) {} rect& operator - () { w += 0.5f; return *this; } rect& operator - (rect&) { w += 0.5f; return *this; } rect& operator -- (int a) { w += a; return *this; } rect& operator -- () { w += 1; return *this; } rect& operator ! () { h += 0.5f; return *this; } void clear() { w = 0; h = 2; } int area() { return w * h; } int perimeter() { return 2 * w + 2 * h; } int width() { return w; } int height() { return h; } private: float w; float h; }; ``` Here are some usage examples: ``` #include <iostream> int main() { rect a; a = ( ----- ! ! -----a ); std::cout << a.area() << std::endl; std::cout << a.perimeter() << std::endl; std::cout << a.width() << std::endl; std::cout << a.height() << std::endl; std::cout << std::endl; a.clear(); a = ( ---------- ! ! ! ! ! ! ! ! ---------a ); std::cout << a.area() << std::endl; std::cout << a.perimeter() << std::endl; std::cout << a.width() << std::endl; std::cout << a.height() << std::endl; return 0; } ``` Here are my questions: - Can it be done without involving any floating point arithmetic? (indeed, it is an integer grid) Can it be generalized on a 3D case? I.e: ``` cuboid b; b = ( --------- / /! ! -------! ! ! ! ! ! ! ! ! !/ ---------b ); std::cout << b.volume() << std::endl; ```

Original source

Related problems