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

  1. /* MCIRCLE.CPP--Example for Chapter 5 of Getting Started */
  2.  
  3. // MCIRCLE.CPP        Illustrates multiple inheritance
  4.  
  5. #include <graphics.h> // Graphics library declarations
  6. #include "point.h"    // Location and Point class declarations
  7. #include <string.h>   // for string functions
  8. #include <conio.h>    // for console I/O
  9.  
  10. // link with point2.obj and graphics.lib
  11.  
  12. // The class hierarchy:
  13. // Location->Point->Circle
  14. // (Circle and CMessage)->MCircle
  15.  
  16. class Circle : public Point {  // Derived from class Point and ultimately
  17.                    // from class Location
  18. protected:
  19.    int Radius;
  20. public:
  21.    Circle(int InitX, int InitY, int InitRadius);
  22.    void Show(void);
  23. };
  24.  
  25.  
  26. class GMessage : public Location {
  27. // display a message on graphics screen
  28.    char *msg;               // message to be displayed
  29.    int Font;                // BGI font to use
  30.    int Field;               // size of field for text scaling
  31.  
  32. public:
  33.    // Initialize message
  34.    GMessage(int msgX, int msgY, int MsgFont, int FieldSize,
  35.             char *text);
  36.    void Show(void);         // show message
  37. };
  38.  
  39.  
  40. class MCircle : Circle, GMessage {  // inherits from both classes
  41. public:
  42.    MCircle(int mcircX, int mcircY, int mcircRadius, int Font,
  43.            char *msg);
  44.    void Show(void);                 // show circle with message
  45. };
  46.  
  47.  
  48. // Member functions for Circle class
  49.  
  50. //Circle constructor
  51. Circle::Circle(int InitX, int InitY, int InitRadius) :
  52.     Point (InitX, InitY)        // initialize inherited members
  53. //also invokes Location constructor
  54. {
  55.    Radius = InitRadius;
  56. };
  57.  
  58. void Circle::Show(void)
  59. {
  60.    Visible = true;
  61.    circle(X, Y, Radius); // draw the circle
  62. }
  63.  
  64. // Member functions for GMessage class
  65.  
  66. //GMessage constructor
  67. GMessage::GMessage(int msgX, int msgY, int MsgFont,
  68.            int FieldSize, char *text) :
  69.            Location(msgX, msgY)
  70. //X and Y coordinates for centering message
  71. {
  72.    Font = MsgFont;    // standard fonts defined in graph.h
  73.    Field = FieldSize; // width of area in which to fit text
  74.    msg = text;        // point at message
  75. };
  76.  
  77. void GMessage::Show(void)
  78. {
  79.    int size = Field / (8 * strlen(msg));     // 8 pixels per char.
  80.    settextjustify(CENTER_TEXT, CENTER_TEXT); // centers in circle
  81.    settextstyle(Font, HORIZ_DIR, size);      // if size > 1, magnifies
  82.    outtextxy(X, Y, msg);                     // display the text
  83. }
  84.  
  85. //Member functions for MCircle class
  86.  
  87. //MCircle constructor
  88. MCircle::MCircle(int mcircX, int mcircY, int mcircRadius, int Font,
  89.          char *msg) : Circle (mcircX, mcircY, mcircRadius),
  90.          GMessage(mcircX,mcircY,Font,2*mcircRadius,msg)
  91. {
  92. }
  93.  
  94. void MCircle::Show(void)
  95. {
  96.    Circle::Show();
  97.    GMessage::Show();
  98. }
  99.  
  100. main()      //draws some circles with text
  101. {
  102.    int graphdriver = DETECT, graphmode;
  103.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  104.    MCircle Small(250, 100, 25, SANS_SERIF_FONT, "You");
  105.    Small.Show();
  106.    MCircle Medium(250, 150, 100, TRIPLEX_FONT, "World");
  107.    Medium.Show();
  108.    MCircle Large(250, 250, 225, GOTHIC_FONT, "Universe");
  109.    Large.Show();
  110.    getch();
  111.    closegraph();
  112.    return 0;
  113. }
  114.