home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 6.ddi / EXAMPLES.ZIP / LISTDEMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  5.0 KB  |  186 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 () {
  97.    Node *N;
  98.    N = new Node;
  99.    N->Item = NULL;
  100.    N->Next = NULL;
  101.    Nodes = NULL;             // sets node pointer to "empty"
  102.                              // because nothing in list yet
  103. }
  104.  
  105. List::~List()                // destructor
  106. {
  107.    while (Nodes != NULL) {   // until end of list
  108.       Node *N = Nodes;       // get node pointed to
  109.       delete(N->Item);       // delete item's memory
  110.       Nodes = N->Next;       // point to next node
  111.         delete N;            // delete pointer's memory
  112.    };
  113. }
  114.  
  115. void List::Add(Point *NewItem)
  116. {
  117.    Node *N;              // N is pointer to a node
  118.    N = new Node;         // create a new node
  119.    N->Item = NewItem;    // store pointer to object in node
  120.    N->Next = Nodes;      // next item points to curent list pos
  121.    Nodes = N;            // last item in list now points
  122.                          // to this node
  123. }
  124.  
  125. void List::Report()
  126. {
  127.    char TempString[12];
  128.    Node *Current = Nodes;
  129.    while (Current != NULL)
  130.    {
  131.       // get X value of item in current node and convert to string
  132.       itoa(Current->Item->GetX(), TempString, 10);
  133.       outtext("X = ");
  134.       OutTextLn(TempString);
  135.       // do the same thing for the Y value
  136.       itoa(Current->Item->GetY(), TempString, 10);
  137.       outtext("Y = ");
  138.       OutTextLn(TempString);
  139.       // point to the next node
  140.       Current = Current->Next;
  141.    };
  142. }
  143.  
  144. void setlist(void);
  145.  
  146. // Main program
  147. main()
  148. {
  149.    int graphdriver = DETECT, graphmode;
  150.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  151.  
  152.    MemStatus("Free memory before list is allocated: ");
  153.    setlist();
  154.    MemStatus("Free memory after List destructor: ");
  155.    getch();
  156.    closegraph();
  157. }
  158.  
  159. void setlist() {
  160.  
  161.    // declare a list (calls List constructor)
  162.    List AList;
  163.  
  164.    // create and add several figures to the list
  165.    Arc *Arc1 = new Arc(151, 82, 25, 200, 330);
  166.    AList.Add(Arc1);
  167.    MemStatus("Free memory after adding arc1: ");
  168.    Circle *Circle1 = new Circle(200, 80, 40);
  169.    AList.Add(Circle1);
  170.    MemStatus("Free memory after adding circle1: ");
  171.    Circle *Circle2 = new Circle(305, 136, 35);
  172.    AList.Add(Circle2);
  173.    MemStatus("Free memory after adding circle2: ");
  174.    // traverse list and display X, Y of the list's figures
  175.    AList.Report();
  176.    // The 3 Alist nodes and the Arc and Circle objects will be
  177.    // deallocated automatically by their destructors when they
  178.    // go out of scope in main(). Arc and Circle use implicit
  179.    // destructors in contrast to the explicit ~List destructor.
  180.    // However, you could delete explicitly here if you wish:
  181.    // delete Arc1; delete Circle1; delete Circle2;
  182.    getch();   // wait for a keypress
  183.    return;
  184. }
  185. 
  186.