01: #include <iostream> 03: using namespace std; 05: /** 06: A class that describes triangle shapes like this: 07: [] 08: [][] 09: [][][] 10: . . . 11: */
12: class Triangle { 14: public: 15: Triangle(int w); 16: int get_area() const; 17: private: 18: int width;
19: }; 21: /** 22: Constructs a triangle with a given width. 23: @param w the width of the triangle base 24: */ 25: Triangle::Triangle(int w) 26: { width = w; } 30: /** 31: Computes the area of the triangle shape. 32: @return the area 33: */ 34: int Triangle::get_area() const 35: { if (width == 1) return 1; 37: Triangle smaller_triangle(width - 1); 38: int smaller_area = smaller_triangle.get_area(); 39: return smaller_area + width; 40: } 41: 42: int main() 43: { Triangle t(4); 45: cout << "Area: " << t.get_area() << endl; 46: return 0; 47: }