home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / LISTDEMO.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  5KB  |  182 lines

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