home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / CIRCLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  2.5 KB  |  86 lines

  1. /* CIRCLE.CPP--Example from Chapter 5 of Getting Started */
  2.  
  3. // CIRCLE.CPP   A Circle class derived from Point
  4.  
  5. #include <graphics.h>    // graphics library declarations
  6. #include "point.h"       // Location and Point class declarations
  7. #include <conio.h>       // for getch() function
  8.  
  9. // link with point2.obj and graphics.lib
  10.  
  11. class Circle : Point {   // derived privately from class Point
  12.              // and ultimately from class Location
  13.    int Radius;           // private by default
  14.  
  15. public:
  16.    Circle(int InitX, int InitY, int InitRadius);
  17.    void Show(void);
  18.    void Hide(void);
  19.    void Expand(int ExpandBy);
  20.    void MoveTo(int NewX, int NewY);
  21.    void Contract(int ContractBy);
  22. };
  23.  
  24. Circle::Circle(int InitX, int InitY, int InitRadius) : Point(InitX,InitY)
  25. {
  26.    Radius = InitRadius;
  27. };
  28.  
  29. void Circle::Show(void)
  30. {
  31.    Visible = true;
  32.    circle(X, Y, Radius);      // draw the circle
  33. }
  34.  
  35. void Circle::Hide(void)
  36. {
  37.    unsigned int TempColor;    // to save current color
  38.    TempColor = getcolor();    // set to current color
  39.    setcolor(getbkcolor());    // set drawing color to background
  40.    Visible = false;
  41.    circle(X, Y, Radius);      // draw in background color to erase
  42.    setcolor(TempColor);       // set color back to current color
  43. };
  44.  
  45. void Circle::Expand(int ExpandBy)
  46. {
  47.    Hide();                       // erase old circle
  48.    Radius += ExpandBy;           // expand radius
  49.    if (Radius < 0)               // avoid negative radius
  50.       Radius = 0;
  51.    Show();                       // draw new circle
  52. };
  53.  
  54. void Circle::Contract(int ContractBy)
  55. {
  56.    Expand(-ContractBy);       // redraws with (Radius - ContractBy)
  57. };
  58.  
  59. void Circle::MoveTo(int NewX, int NewY)
  60. {
  61.    Hide();                    // erase old circle
  62.    X = NewX;                  // set new location
  63.    Y = NewY;
  64.    Show();                    // draw in new location
  65. };
  66.  
  67. main()                        // test the functions
  68. {
  69.    // initialize the graphics system
  70.    int graphdriver = DETECT, graphmode;
  71.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  72.  
  73.    Circle MyCircle(100, 200, 50);   // declare a circle object
  74.    MyCircle.Show();                 // show it
  75.    getch();                         // wait for keypress
  76.    MyCircle.MoveTo(200, 250);       // move the circle (tests hide
  77.                                     // and show also)
  78.    getch();
  79.    MyCircle.Expand(50);             // make it bigger
  80.    getch();
  81.    MyCircle.Contract(75);           // make it smaller
  82.    getch();
  83.    closegraph();
  84.    return 0;
  85. }
  86.