/* * First example: shapes * OOP and C++ * Note how the main program calls to draw and area become cleaner * * RunStuff:: g++ THISFILE */ #include /* * Definition of class Rect */ class Rect { public: int x, y, wid, ht; void draw(); float area(); }; /* * Definition of class Circle */ class Circle { public: int x, y, rad; void draw(); float area(); }; int main () { /* * Data definitions */ Rect *r1, *r2; Circle *c1; r1 = new Rect(); r2 = new Rect(); c1 = new Circle; /* * Set the locations of the 3 objects */ r1->x = 10; r1->y = 10; r1->wid = 50; r1->ht = 50; r2->x = 80; r2->y = 10; r2->wid = 10; r2->ht = 100; c1->x = 35; c1->y = 90; c1->rad = 10; /* * "Draw" them */ r1->draw(); r2->draw(); c1->draw(); /* * Compute and print total area */ printf ("Total area = %f\n", r1->area() + r2->area() + c1->area()); return 0; } /* * Methods for class Rect */ void Rect::draw () { printf ("Drawing rectangle at %d, %d, %d, %d\n", x, y, wid, ht); } float Rect::area () { return (float) (wid * ht); } /* * Methods for class Circle */ void Circle::draw () { printf ("Drawing circle at %d, %d, radius = %d\n", x, y, rad); } float Circle::area () { return (float) (rad * rad * 3.14159); }