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

  1. /* LISTDEMO.CPP--Example from Chapter 5 of Getting Started */
  2.  
  3. // LISTDEMO.CPP           Demonstrates dynamic objects
  4.  
  5. // Link with FIGURES.OBJ and GRAPHICS.LIB
  6.  
  7. #include <conio.h>          // for getch()
  8. #include <alloc.h>          // for coreleft()
  9. #include <stdlib.h>         // for itoa()
  10. #include <string.h>         // for strcpy()
  11. #include <graphics.h>
  12. #include "figures.h"
  13.  
  14. class Arc : public Circle {
  15.    int StartAngle, EndAngle;
  16. public:
  17.    // constructor
  18.    Arc(int InitX, int InitY, int InitRadius, int InitStartAngle,
  19.        int InitEndAngle);
  20.    // virtual functions
  21.    void Show();
  22.    void Hide();
  23. };
  24.  
  25. struct Node {     // the list item
  26.    Point *Item;   // can be Point or any class derived from Point
  27.    Node  *Next;   // point to next Node object
  28. };
  29.  
  30. class List {      // the list of objects pointed to by nodes
  31.    Node *Nodes;   // points to a node
  32. public:
  33.    // constructor
  34.    List();
  35.    // destructor
  36.    ~List();
  37.    // add an item to list
  38.    void Add(Point *NewItem);
  39.    // list the items
  40.    void Report();
  41. };
  42.  
  43. // definitions for standalone functions
  44.  
  45. void OutTextLn(char *TheText)
  46. {
  47.    outtext(TheText);
  48.    moveto(0, gety() + 12);   // move to equivalent of next line
  49. }
  50.  
  51. void MemStatus(char *StatusMessage)
  52. {
  53.    unsigned long MemLeft;  // to match type returned by
  54.                // coreleft()
  55.    char CharString[12];    // temp string to send to outtext()
  56.    outtext(StatusMessage);
  57.    MemLeft = long (coreleft());
  58.  
  59.    // convert result to string with ltoa then copy into
  60.    // temporary string
  61.    ltoa(MemLeft, CharString, 10);
  62.    OutTextLn(CharString);
  63. }
  64.  
  65. // member functions for Arc class
  66.  
  67. Arc::Arc(int InitX, int InitY, int InitRadius, int InitStartAngle,
  68.          int InitEndAngle) : Circle (InitX, InitY,InitRadius)
  69.                               // calls Circle
  70.                               // constructor
  71. {
  72.    StartAngle = InitStartAngle;
  73.    EndAngle = InitEndAngle;
  74. }
  75.  
  76. void Arc::Show()
  77. {
  78.    Visible = true;
  79.    arc(X, Y, StartAngle, EndAngle, Radius);
  80. }
  81.  
  82. void Arc::Hide()
  83. {
  84.    unsigned TempColor;
  85.    TempColor = getcolor();
  86.    setcolor(getbkcolor());
  87.    Visible = false;
  88.    arc(X, Y, StartAngle, EndAngle, Radius);
  89.    setcolor(TempColor);
  90. }
  91.  
  92. // member functions for List class
  93.  
  94. List::List () {
  95.    Node *N;
  96.    N = new Node;
  97.    N->Item = NULL;
  98.    N->Next = NULL;
  99.    Nodes = NULL;             // sets node pointer to "empty"
  100.                              // because nothing in list yet
  101. }
  102.  
  103. List::~List()                // destructor
  104. {
  105.    while (Nodes != NULL) {   // until end of list
  106.       Node *N = Nodes;       // get node pointed to
  107.       delete(N->Item);       // delete item's memory
  108.       Nodes = N->Next;       // point to next node
  109.         delete N;            // delete pointer's memory
  110.    };
  111. }
  112.  
  113. void List::Add(Point *NewItem)
  114. {
  115.    Node *N;              // N is pointer to a node
  116.    N = new Node;         // create a new node
  117.    N->Item = NewItem;    // store pointer to object in node
  118.    N->Next = Nodes;      // next item points to curent list pos
  119.    Nodes = N;            // last item in list now points
  120.                          // to this node
  121. }
  122.  
  123. void List::Report()
  124. {
  125.    char TempString[12];
  126.    Node *Current = Nodes;
  127.    while (Current != NULL)
  128.    {
  129.       // get X value of item in current node and convert to string
  130.       itoa(Current->Item->GetX(), TempString, 10);
  131.       outtext("X = ");
  132.       OutTextLn(TempString);
  133.       // do the same thing for the Y value
  134.       itoa(Current->Item->GetY(), TempString, 10);
  135.       outtext("Y = ");
  136.       OutTextLn(TempString);
  137.       // point to the next node
  138.       Current = Current->Next;
  139.    };
  140. }
  141.  
  142. void setlist(void);
  143.  
  144. // Main program
  145. main()
  146. {
  147.    int graphdriver = DETECT, graphmode;
  148.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  149.  
  150.    MemStatus("Free memory before list is allocated: ");
  151.    setlist();
  152.    MemStatus("Free memory after List destructor: ");
  153.    getch();
  154.    closegraph();
  155. }
  156.  
  157. void setlist() {
  158.  
  159.    // declare a list (calls List constructor)
  160.    List AList;
  161.  
  162.    // create and add several figures to the list
  163.    Arc *Arc1 = new Arc(151, 82, 25, 200, 330);
  164.    AList.Add(Arc1);
  165.    MemStatus("Free memory after adding arc1: ");
  166.    Circle *Circle1 = new Circle(200, 80, 40);
  167.    AList.Add(Circle1);
  168.    MemStatus("Free memory after adding circle1: ");
  169.    Circle *Circle2 = new Circle(305, 136, 35);
  170.    AList.Add(Circle2);
  171.    MemStatus("Free memory after adding circle2: ");
  172.    // traverse list and display X, Y of the list's figures
  173.    AList.Report();
  174.    // The 3 Alist nodes and the Arc and Circle objects will be
  175.    // deallocated automatically by their destructors when they
  176.    // go out of scope in main(). Arc and Circle use implicit
  177.    // destructors in contrast to the explicit ~List destructor.
  178.    // However, you could delete explicitly here if you wish:
  179.    // delete Arc1; delete Circle1; delete Circle2;
  180.    getch();   // wait for a keypress
  181.    return;
  182. }
  183.