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

  1. /* VCIRC.CPP--Example from Chapter 5 of Getting Started */
  2.  
  3. // A Circle class derived from Point
  4.  
  5. #include <graphics.h>    // graphics library declarations
  6. #include "vpoint.h"       // Location and Point class declarations
  7. #include <conio.h>       // for getch() function
  8.  
  9. // link with vpoint.obj and graphics.lib
  10.  
  11. class Circle : public Point { // derived 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 Contract(int ContractBy);
  21. };
  22.  
  23. // Circle constructor calls base Point constructor first
  24. Circle::Circle(int InitX, int InitY, int InitRadius) : Point(InitX,InitY)
  25. {
  26.    Radius = InitRadius;
  27. };
  28.  
  29. void Circle::Show()
  30. {
  31.    Visible = true;
  32.    circle(X, Y, Radius);      // draw the circle using BGI function
  33. }
  34.  
  35. void Circle::Hide()
  36. {
  37.    if (!Visible) return;      // no need to hide
  38.    unsigned int TempColor;    // to save current color
  39.    TempColor = getcolor();    // set to current color
  40.    setcolor(getbkcolor());    // set drawing color to background
  41.    Visible = false;
  42.    circle(X, Y, Radius);      // draw in background color to erase
  43.    setcolor(TempColor);       // set color back to current color
  44. };
  45.  
  46. void Circle::Expand(int ExpandBy)
  47. {
  48.    Boolean vis = Visible;  // is current circle visible?
  49.    if (vis) Hide();        // if so, hide it
  50.    Radius += ExpandBy;     // expand radius
  51.    if (Radius < 0)         // avoid negative radius
  52.       Radius = 0;
  53.    if (vis) Show();        // draw new circle if previously visible
  54. };
  55.  
  56. inline void Circle::Contract(int ContractBy)
  57. {
  58.    Expand(-ContractBy);       // redraws with (Radius - ContractBy)
  59. };
  60.  
  61. main()                        // test the functions
  62. {
  63.    // initialize the graphics system
  64.    int graphdriver = DETECT, graphmode;
  65.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  66.  
  67.    Circle MyCircle(100, 200, 50);   // declare a circle object
  68.    MyCircle.Show();                 // show it
  69.    getch();                         // wait for keypress
  70.    MyCircle.MoveTo(200, 250);       // move the circle (tests hide
  71.                                     // and show also)
  72.    getch();
  73.    MyCircle.Expand(50);             // make it bigger
  74.    getch();
  75.    MyCircle.Contract(75);           // make it smaller
  76.    getch();
  77.    closegraph();
  78.    return 0;
  79. }
  80.