Skip to main content
Hany Ammar
Professor Emeritus, Computer Science and Electrical Engineering

Programs

Program for pixel.cpp

    
// Borland C++ - (C) Copyright 1991 by Borland International

// pixel.prj consists of pixel.cpp, point.h, point2.cpp
/* PIXEL.CPP--Example from Getting Started */

// PIXEL.CPP demonstrates the Point and Location classes
// compile with POINT2.CPP and link with GRAPHICS.LIB

#include < graphics.h >   // declarations for graphics library
#include < conio.h >      // for getch() function
#include "point.h"      // declarations for Point and Location classes

int main()
{
   // initialize the graphics system
   int graphdriver = DETECT, graphmode;
   initgraph(&graphdriver, &graphmode, "..\\bgi");

   // move a point across the screen
   Point APoint(100, 50);   // Initial X, Y at 100, 50
   APoint.Show();           // APoint turns itself on
   getch();                 // Wait for keypress
   APoint.MoveTo(300, 150); // APoint moves to 300,150
   getch();                 // Wait for keypress
   APoint.Hide();           // APoint turns itself off
   getch();                 // Wait for keypress
   closegraph();            // Restore original screen
   return 0;
}

Header file point.h

// Borland C++ - (C) Copyright 1991 by Borland International

/* point.h--Example from Getting Started */

// point.h contains two classes:
// class Location describes screen locations in X and Y coordinates
// class Point describes whether a point is hidden or visible

enum Boolean {false, true};

class Location {
protected:          // allows derived class to access private data
   int X;
   int Y;

public:             // these functions can be accessed from outside
   Location(int InitX, int InitY);
   int GetX();
   int GetY();
};
class Point : public Location {      // derived from class Location
// public derivation means that X and Y are protected within Point

protected:
   Boolean Visible;  // classes derived from Point will need access    

public:
   Point(int InitX, int InitY);      // constructor
   void Show();
   void Hide();
   Boolean IsVisible();
   void MoveTo(int NewX, int NewY);
};

Program for point2.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

/* POINT2.CPP--Example from Getting Started */

// POINT2.CPP contains the definitions for the Point and Location
// classes that are declared in the file point.h

#include "point.h"
#include < graphics.h >

// member functions for the Location class
Location::Location(int InitX, int InitY) {
   X = InitX;
   Y = InitY;
};

int Location::GetX(void) {
   return X;
};

int Location::GetY(void) {
   return Y;
};

// member functions for the Point class: These assume
// the main program has initialized the graphics system

Point::Point(int InitX, int InitY) : Location(InitX,InitY) {
   Visible = false;                  // make invisible by default
};

void Point::Show(void) {
   Visible = true;
   putpixel(X, Y, getcolor());       // uses default color
};

void Point::Hide(void) {
   Visible = false;
   putpixel(X, Y, getbkcolor()); // uses background color to erase
};

Boolean Point::IsVisible(void) {
   return Visible;
};

void Point::MoveTo(int NewX, int NewY) {
   Hide();         // make current point invisible
   X = NewX;       // change X and Y coordinates to new location
   Y = NewY;
   Show();         // show point at new location
};

Program for circle.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// circle.prj consists of circle.cpp, point.h, point2.cpp
/* CIRCLE.CPP--Example from Getting Started */

// CIRCLE.CPP   A Circle class derived from Point

#include < graphics.h >    // graphics library declarations
#include "point.h"       // Location and Point class declarations
#include < conio.h >       // for getch() function

// link with point2.obj and graphics.lib

class Circle : Point {   // derived privately from class Point
                         // and ultimately from class Location
   int Radius;           // private by default

public:
   Circle(int InitX, int InitY, int InitRadius);
   void Show(void);
   void Hide(void);
   void Expand(int ExpandBy);
   void MoveTo(int NewX, int NewY);
   void Contract(int ContractBy);
};

Circle::Circle(int InitX, int InitY, int InitRadius) : Point(InitX,InitY)
{
   Radius = InitRadius;
};

void Circle::Show(void)
{
   Visible = true;
   circle(X, Y, Radius);      // draw the circle
}

void Circle::Hide(void)
{
   unsigned int TempColor;    // to save current color
   TempColor = getcolor();    // set to current color
   setcolor(getbkcolor());    // set drawing color to background
   Visible = false;
   circle(X, Y, Radius);      // draw in background color to erase
   setcolor(TempColor);       // set color back to current color
};

void Circle::Expand(int ExpandBy)
{
   Hide();                       // erase old circle
   Radius += ExpandBy;           // expand radius
   if (Radius < 0)               // avoid negative radius
      Radius = 0;
   Show();                       // draw new circle
};

void Circle::Contract(int ContractBy)
{
   Expand(-ContractBy);       // redraws with (Radius - ContractBy)
};

void Circle::MoveTo(int NewX, int NewY)
{
   Hide();                    // erase old circle
   X = NewX;                  // set new location
   Y = NewY;
   Show();                    // draw in new location
};

main()                        // test the functions
{
   // initialize the graphics system
   int graphdriver = DETECT, graphmode;
   initgraph(&graphdriver, &graphmode, "..\\bgi");

   Circle MyCircle(100, 200, 50);   // declare a circle object
   MyCircle.Show();                 // show it
   getch();                         // wait for keypress
   MyCircle.MoveTo(200, 250);       // move the circle (tests hide
                                    // and show also)
   getch();
   MyCircle.Expand(50);             // make it bigger
   getch();
   MyCircle.Contract(75);           // make it smaller
   getch();
   closegraph();
   return 0;
}

Program for mcircle.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// mcircle.prj consists of mcircle.cpp, point2.cpp, point.h
/* MCIRCLE.CPP--Example from Getting Started */

// MCIRCLE.CPP        Illustrates multiple inheritance

#include < graphics.h > // Graphics library declarations
#include "point.h"    // Location and Point class declarations
#include < string.h >   // for string functions
#include < conio.h >    // for console I/O

// link with point2.obj and graphics.lib

// The class hierarchy:
// Location->Point->Circle
// (Circle and CMessage)->MCircle

class Circle : public Point {  // Derived from class Point and 
                               // ultimately from class Location
protected:
   int Radius;
public:
   Circle(int InitX, int InitY, int InitRadius);
   void Show(void);
};


class GMessage : public Location {
// display a message on graphics screen
   char *msg;               // message to be displayed
   int Font;                // BGI font to use
   int Field;               // size of field for text scaling

public:
   // Initialize message
   GMessage(int msgX, int msgY, int MsgFont, int FieldSize,
            char *text);
   void Show(void);         // show message
};


class MCircle : Circle, GMessage {  // inherits from both classes
public:
   MCircle(int mcircX, int mcircY, int mcircRadius, int Font,
           char *msg);
   void Show(void);                 // show circle with message
};


// Member functions for Circle class

//Circle constructor
Circle::Circle(int InitX, int InitY, int InitRadius) :
	Point (InitX, InitY)        // initialize inherited members
//also invokes Location constructor
{
   Radius = InitRadius;
};

void Circle::Show(void)
{
   Visible = true;
   circle(X, Y, Radius); // draw the circle
}

// Member functions for GMessage class

//GMessage constructor
GMessage::GMessage(int msgX, int msgY, int MsgFont,
		   int FieldSize, char *text) :
		   Location(msgX, msgY)
//X and Y coordinates for centering message
{
   Font = MsgFont;    // standard fonts defined in graph.h
   Field = FieldSize; // width of area in which to fit text
   msg = text;        // point at message
};

void GMessage::Show(void)
{
   int size = Field / (8 * strlen(msg));     // 8 pixels per char.
   settextjustify(CENTER_TEXT, CENTER_TEXT); // centers in circle
   settextstyle(Font, HORIZ_DIR, size);      // magnify if size > 1 
   outtextxy(X, Y, msg);                     // display the text
}

//Member functions for MCircle class

//MCircle constructor
MCircle::MCircle(int mcircX, int mcircY, int mcircRadius, int Font,
                 char *msg) : Circle (mcircX, mcircY, mcircRadius),
                 GMessage(mcircX,mcircY,Font,2*mcircRadius,msg)
{
}

void MCircle::Show(void)
{
   Circle::Show();
   GMessage::Show();
}

main()      //draws some circles with text
{
   int graphdriver = DETECT, graphmode;
   initgraph(&graphdriver, &graphmode, "..\\bgi");
   MCircle Small(250, 100, 25, SANS_SERIF_FONT, "You");
   Small.Show();
   MCircle Medium(250, 150, 100, TRIPLEX_FONT, "World");
   Medium.Show();
   MCircle Large(250, 250, 225, GOTHIC_FONT, "Universe");
   Large.Show();
   getch();
   closegraph();
   return 0;
}

Program of intro28.cpp

// INTRO28.CPP--Example from Chapter 3, "An Introduction to C++"

#include < iostream.h >

int main()
{
	float hours[52];
	int week;

	// Initialize the array
	for (week = 0; week < 52; week++)
		hours[week] = 0;

	// Store four values in array
	hours[0] = 32.5;
	hours[1] = 44.0;
	hours[2] = 40.5;
	hours[3] = 38.0;

	// Retrieve values and show their addresses
	cout << "Element "<< "\tValue " << "\tAddress\n";
	for (week = 0; week < 4; week++)
		cout << "hours[" << week << "]" << '\t'
		     << hours[week] << '\t' << &hours[week] << '\n';

	return 0;
}

Program of ex7.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// ex7.prj consists of files list.h, list.cpp, ex7.cpp
// ex7.cpp:   Using the List class
// from Hands-on C++
#include "list.h"

main()
{
   List l(5);
   int i = 0;

   // Insert the numbers 1 through 5
   while (l.put_elem(i+1,i) == 0)
      ++i;
   l.setn(i);

   l.print();
   return 0;
}

Header list.h

// Borland C++ - (C) Copyright 1991 by Borland International

// list.h:   A Integer List Class
// from Hands-on C++
const int Max_elem = 10;

class List
{
   int *list;        // An array of integers
   int nmax;         // The dimension of the array
   int nelem;        // The number of elements

public:
   List(int n = Max_elem) {list = new int[n]; nmax = n; nelem = 0;};
   ~List() {delete list;};
   int put_elem(int, int);
   int get_elem(int&, int);
   void setn(int n) {nelem = n;};
   int getn() {return nelem;};
   void incn() {if (nelem < nmax) ++nelem;};
   int getmax() {return nmax;};
   void print();
};

Program of list.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// list.cpp:   Implementation of the List Class
// from Hands-on C++
#include < iostream.h >
#include "list.h"

int List::put_elem(int elem, int pos)
{
   if (0 <= pos && pos < nmax)
   {
      list[pos] = elem;    // Put an element into the list
      return 0;
   }
   else
      return -1;           // Non-zero means error
}

int List::get_elem(int& elem, int pos)
{
   if (0 <= pos && pos < nmax)
   {
      elem = list[pos];    // Retrieve a list element
      return 0;
   }
   else
      return -1;           // non-zero means error
}

void List::print()
{
   for (int i = 0; i < nelem; ++i)
      cout << list[i] << "\n";
}

Programs of figures.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// FIGURES.CPP: This file contains the definitions for the Point
// class (declared in figures.h). Member functions for the
// Location class appear as inline functions in figures.h.

#include "figures.h"
#include < graphics.h >
#include < conio.h >

// member functions for the Point class

//constructor
Point::Point(int InitX, int InitY) : Location (InitX, InitY)
{
   Visible = false;    // make invisible by default
}

void Point::Show()
{
   Visible = true;
   putpixel(X, Y, getcolor()); // uses default color
}

void Point::Hide()
{
   Visible = false;
   putpixel(X, Y, getbkcolor()); // uses background color to erase
}

void Point::MoveTo(int NewX, int NewY)
{
   Hide();          // make current point invisible
   X = NewX;        // change X and Y coordinates to new location
   Y = NewY;
   Show();          // show point at new location
}

// a general-purpose function for getting keyboard
// cursor movement keys (not a member function)

Boolean GetDelta(int& DeltaX, int& DeltaY)
{
   char KeyChar;
   Boolean Quit;
   DeltaX = 0;
   DeltaY = 0;

   do
{
      KeyChar = getch();     // read the keystroke
      if (KeyChar == 13)     // carriage return
         return(false);
      if (KeyChar == 0)      // an extended keycode
         {
          Quit = true;       // assume it is usable
          KeyChar = getch(); // get rest of keycode
              switch (KeyChar) {
                 case 72: DeltaY = -1; break; // down arrow
                 case 80: DeltaY =  1; break; // up arrow
                 case 75: DeltaX = -1; break; // left arrow
                 case 77: DeltaX =  1; break; // right arrow
                 default: Quit = false; // bad key
                 };
         };
   } while (!Quit);
   return(true);
}

void Point::Drag(int DragBy)
{
   int DeltaX, DeltaY;
   int FigureX, FigureY;

   Show(); // display figure to be dragged
   FigureX = GetX(); // get initial position of figure
   FigureY = GetY();

   // This is the drag loop
   while (GetDelta(DeltaX, DeltaY))
{
      // Apply delta to figure at X, Y
      FigureX += (DeltaX * DragBy);
      FigureY += (DeltaY * DragBy);
      MoveTo(FigureX, FigureY); // tell figure to move
      };
}
// Member functions for the Circle class

//constructor
Circle::Circle(int InitX, int InitY, int InitRadius) : Point (InitX, InitY)
{
   Radius = InitRadius;
}

void Circle::Show()
{
   Visible = true;
   circle(X, Y, Radius);     // draw the circle
}

void Circle::Hide()
{
   unsigned int TempColor;   // to save current color
   TempColor = getcolor();   // set to current color
   setcolor(getbkcolor());   // set drawing color to background
   Visible = false;
   circle(X, Y, Radius);     // draw in background color to
   setcolor(TempColor);      // set color back to current color
}

void Circle::Expand(int ExpandBy)
{
   Hide();                       // erase old circle
   Radius += ExpandBy;           // expand radius
   if (Radius < 0)               // avoid negative radius
      Radius = 0;
   Show();                       // draw new circle
}

void Circle::Contract(int ContractBy)
{
   Expand(-ContractBy);      // redraws with (Radius-ContractBy)
}

Program of dynpoint.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// DPOINT.CPP (dynpoint.cpp) -- exercise in Getting Started

#include < iostream.h >
#include < graphics.h >
#include < conio.h >
#include "figures.h"

int main()
{
// Assign pointer to dynamically allocated object; call constructor
Point *APoint = new Point(50, 100);

// initialize the graphics system
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "..\\bgi");

// Demonstrate the new object
APoint->Show();
cout << "Note pixel at (50,100). Now, hit any key...";
getch();
delete APoint;
closegraph();
return(0);
}

Program of figdemo.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// FIGDEMO.CPP -- Exercise in Getting Started

// demonstrates the Figures toolbox by extending it with
// a new type Arc.
// The figdemo.prj consists of figures.cpp

// Link with FIGURES.OBJ and GRAPHICS.LIB

#include "figures.h"
#include < graphics.h >
#include < conio.h >

class Arc : public Circle {
   int StartAngle;
   int EndAngle;
public:
// constructor
   Arc(int InitX, int InitY, int InitRadius, int InitStartAngle, int
       InitEndAngle) : Circle (InitX, InitY, InitRadius) {
       StartAngle = InitStartAngle; EndAngle = InitEndAngle;}
   void Show();  // these functions are virtual in Point
   void Hide();
};

// Member functions for Arc

void Arc::Show() 
{
   Visible = true;
   arc(X, Y, StartAngle, EndAngle, Radius);
}

void Arc::Hide()
{
   int TempColor;
   TempColor = getcolor();
   setcolor (getbkcolor());
   Visible = false;
   // draw arc in background color to hide it
   arc(X, Y, StartAngle, EndAngle, Radius);
   setcolor(TempColor);
}

int main()   // test the new Arc class
{
   int graphdriver = DETECT, graphmode;
   initgraph(&graphdriver, &graphmode, "..\\bgi");
   Circle ACircle(151, 82, 50);
   Arc AnArc(151, 82, 25, 0, 190);

   // you first drag an arc using arrow keys (5 pixels per key)
   // press Enter when tired of this!
   // Now drag a circle (10 pixels per arrow key)
   // Press Enter to end FIGDEMO.

   AnArc.Drag(5);   // drag increment is 5 pixels
   AnArc.Hide();
   ACircle.Drag(10); // now each drag is 10 pixels
   closegraph();
   return 0;
}

Header figures.h

// Borland C++ - (C) Copyright 1991 by Borland International

// figures.h contains three classes.
//
//  Class Location describes screen locations in X and Y
//  coordinates.
//
//  Class Point describes whether a point is hidden or visible.
//
//  Class Circle describes the radius of a circle around a point.
//
// To use this module, put #include  in your main
// source file and compile the source file FIGURES.CPP together
// with your main source file.

enum Boolean {false, true};

class Location {
protected:
   int X;
   int Y;
public:
   Location(int InitX, int InitY) {X = InitX; Y = InitY;}
   int GetX() {return X;}
   int GetY() {return Y;}
};

class Point : public Location {
protected:
   Boolean Visible;
public:
   Point(int InitX, int InitY);
   virtual void Show();       // Show and Hide are virtual
   virtual void Hide();
   virtual void Drag(int DragBy); // new virtual drag function
   Boolean IsVisible() {return Visible;}
   void MoveTo(int NewX, int NewY);
};

class Circle : public Point {  // Derived from class Point and
                               // ultimately from class Location
protected:
   int Radius;
public:
   Circle(int InitX, int InitY, int InitRadius);
   void Show();
   void Hide();
   void Expand(int ExpandBy);
   void Contract(int ContractBy);
};

// prototype of general-purpose, non-member function
// defined in FIGURES.CPP

Boolean GetDelta(int& DeltaX, int& DeltaY);

Program listdemo.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

/* LISTDEMO.CPP--Example from Getting Started */

// listdemo.prj consists of listdemo.cpp and figures.cpp

// LISTDEMO.CPP           Demonstrates dynamic objects

// Link with FIGURES.OBJ and GRAPHICS.LIB

#include < conio.h >          // for getch()
#include < alloc.h >          // for coreleft()
#include < stdlib.h >         // for itoa()
#include < string.h >         // for strcpy()
#include < graphics.h >
#include "figures.h"

class Arc : public Circle {
   int StartAngle, EndAngle;
public:
   // constructor
   Arc(int InitX, int InitY, int InitRadius, int InitStartAngle,
       int InitEndAngle);
   // virtual functions
   void Show();
   void Hide();
};

struct Node {     // the list item
   Point *Item;   // can be Point or any class derived from Point
   Node  *Next;   // point to next Node object
};

class List {      // the list of objects pointed to by nodes
   Node *Nodes;   // points to a node
public:
   // constructor
   List();
   // destructor
   ~List();
   // add an item to list
   void Add(Point *NewItem);
   // list the items
   void Report();
};

// definitions for standalone functions

void OutTextLn(char *TheText)
{
   outtext(TheText);
   moveto(0, gety() + 12);   // move to equivalent of next line
}

void MemStatus(char *StatusMessage)
{
   unsigned long MemLeft;  // to match type returned by
			   // coreleft()
   char CharString[12];    // temp string to send to outtext()
   outtext(StatusMessage);
   MemLeft = long (coreleft());

   // convert result to string with ltoa then copy into
   // temporary string
   ltoa(MemLeft, CharString, 10);
   OutTextLn(CharString);
}

// member functions for Arc class

Arc::Arc(int InitX, int InitY, int InitRadius, int InitStartAngle,
         int InitEndAngle) : Circle (InitX, InitY,InitRadius)
                              // calls Circle
                              // constructor
{
   StartAngle = InitStartAngle;
   EndAngle = InitEndAngle;
}

void Arc::Show()
{
   Visible = true;
   arc(X, Y, StartAngle, EndAngle, Radius);
}

void Arc::Hide()
{
   unsigned TempColor;
   TempColor = getcolor();
   setcolor(getbkcolor());
   Visible = false;
   arc(X, Y, StartAngle, EndAngle, Radius);
   setcolor(TempColor);
}

// member functions for List class

List::List ()                // constructor
{
   Nodes = NULL;             // initialize Nodes data
}

List::~List()                // destructor
{
   while (Nodes != NULL) {   // until end of list
      Node *N = Nodes;       // get node pointed to
      delete(N->Item);       // delete item's memory
      Nodes = N->Next;       // point to next node
        delete N;            // delete pointer's memory
   };
}

void List::Add(Point *NewItem)
{
   Node *N;              // N is pointer to a node
   N = new Node;         // create a new node
   N->Item = NewItem;    // store pointer to object in node
   N->Next = Nodes;      // next item points to curent list pos
   Nodes = N;            // last item in list now points
                         // to this node
}

void List::Report()
{
   char TempString[12];
   Node *Current = Nodes;
   while (Current != NULL)
   {
      // get X value of item in current node and convert to string
      itoa(Current->Item->GetX(), TempString, 10);
      outtext("X = ");
      OutTextLn(TempString);
      // do the same thing for the Y value
      itoa(Current->Item->GetY(), TempString, 10);
      outtext("Y = ");
      OutTextLn(TempString);
      // point to the next node
      Current = Current->Next;
   };
}

void setlist(void);

// Main program
main()
{
   int graphdriver = DETECT, graphmode;
   initgraph(&graphdriver, &graphmode, "..\\bgi");

   MemStatus("Free memory before list is allocated: ");
   setlist();
   MemStatus("Free memory after List destructor: ");
   getch();
   closegraph();
}

void setlist() {

   // declare a list (calls List constructor)
   List AList;

   // create and add several figures to the list
   Arc *Arc1 = new Arc(151, 82, 25, 200, 330);
   AList.Add(Arc1);
   MemStatus("Free memory after adding arc1: ");
   Circle *Circle1 = new Circle(200, 80, 40);
   AList.Add(Circle1);
   MemStatus("Free memory after adding circle1: ");
   Circle *Circle2 = new Circle(305, 136, 35);
   AList.Add(Circle2);
   MemStatus("Free memory after adding circle2: ");
   // traverse list and display X, Y of the list's figures
   AList.Report();
   // The 3 Alist nodes and the Arc and Circle objects will be
   // deallocated automatically by their destructors when they
   // go out of scope in main(). Arc and Circle use implicit
   // destructors in contrast to the explicit ~List destructor.
   // However, you could delete explicitly here if you wish:
   // delete Arc1; delete Circle1; delete Circle2;
   getch();   // wait for a keypress
   return;
}


Program solar.cpp

//SOLAR.CPP--Example from Chapter 3, "An Introduction to C++"

#include < graphics.h >
#include < iostream.h >
#include < string.h >

struct planet {
	char name[10];
	float distance;
	float radius;
	int color;
	int fill_type;
};

planet solar_system[9];
planet *planet_ptr;
int    planet_num;

int main()
{
	strcpy(solar_system[0].name,"Mercury");
	solar_system[0].distance = 0.4;
	solar_system[0].radius = 0.4;
	solar_system[0].color = EGA_YELLOW;
	solar_system[0].fill_type = EMPTY_FILL;

	planet_ptr = solar_system;
	planet_ptr++;             // Point to second planet structure
	 strcpy (planet_ptr->name,"Venus");
	planet_ptr->distance = 0.7;
	planet_ptr->radius = 1.0;
	planet_ptr->color = EGA_BROWN;
	planet_ptr->fill_type = SOLID_FILL;

	planet_ptr = solar_system;         // Reset to first element
	for (planet_num = 0; planet_num < 2; planet_num++, planet_ptr++) 
        {
	   cout << " \nPlanetary statistics: ";
	   cout << " \nName: "<< planet_ptr->name;
	   cout << " \nDistance from Sun in AU: "<< planet_ptr->distance;
	   cout << " \nRadius in Earth radii: "<<  planet_ptr->radius;
	   cout << " \nColor constant value "<< planet_ptr->color;
	   cout << " \nFill pattern constant value "<< planet_ptr->fill_type;
        }

   return 0;
}

Program of string.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

//STRING.CPP--Example from Getting Started */

#include < iostream.h >
#include < string.h >


class String {
   char *char_ptr;   // pointer to string contents
   int length;       // length of string in characters
public:
   // three different constructors
   String(char *text);           // constructor using existing string
   String(int size = 80);        // creates default empty string
   String(String& Other_String); // for assignment from another
                                 // object of this class
   ~String() {delete char_ptr;};
   int Get_len (void);
   void Show (void);
};

String::String (char *text)
{
   length = strlen(text);  // get length of text
   char_ptr = new char[length + 1];
   strcpy(char_ptr, text);
};

String::String (int size)
{
   length = size;
   char_ptr = new char[length+1];
   *char_ptr = '\0';
};

String::String (String& Other_String)
{
   length = Other_String.length;       // length of other string
   char_ptr = new char [length + 1];   // allocate the memory
   strcpy (char_ptr, Other_String.char_ptr); // copy the text
};

int String::Get_len(void)
{
   return (length);
};

void String::Show(void)
{
   cout << char_ptr << "\n";
};

main ()                                // test the functions
{
   String AString ("Allocated from a constant string.");
   AString.Show();

   String BString;             // uses default length
   cout << "\n" << BString.Get_len() << "\n" ; //display length
   BString = "This is BString";

   String CString(BString);    // invokes the third constructor
   CString.Show();             // note its contents
}

Program of ex5.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

//Project ex5.prj consists of programs ex5.cpp, def.cpp and def.h
// ex5.cpp:   Using the Definition class
// from Hands-on C++
#include < iostream.h >
#include "def.h"

main()
{
   Definition d;          // Declare a Definition object
   char s[81];

   // Assign the meanings
   d.put_word("class");
   d.add_meaning("a body of students meeting together to \
study the same subject");
   d.add_meaning("a group sharing the same economic status");
   d.add_meaning("a group, set or kind sharing the same attributes");

   // Print them
   cout << d.get_word(s) << ":\n\n";
   for (int i = 0; d.get_meaning(i,s) != 0; ++i)
      cout << (i+1) << ": " << s << "\n";
   return 0;
}

Program of def.h

// Borland C++ - (C) Copyright 1991 by Borland International

// def.h:   A word definition class
// from Hands-on C++
#include < string.h >

const int Maxmeans = 5;

class Definition
{
   char *word;                    // Word being defined
   char *meanings[Maxmeans];      // Various meanings of this word
   int nmeanings;

public:
   void put_word(char *);
   char *get_word(char *s) {return strcpy(s,word);};  // line 15
   void add_meaning(char *);
   char *get_meaning(int, char *);

};

Program of figures.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// FIGURES.CPP: This file contains the definitions for the Point
// class (declared in figures.h). Member functions for the
// Location class appear as inline functions in figures.h.

#include "figures.h"
#include < graphics.h >
#include < conio.h >

// member functions for the Point class

//constructor
Point::Point(int InitX, int InitY) : Location (InitX, InitY)
{
   Visible = false;    // make invisible by default
}

void Point::Show()
{
   Visible = true;
   putpixel(X, Y, getcolor()); // uses default color
}

void Point::Hide()
{
   Visible = false;
   putpixel(X, Y, getbkcolor()); // uses background color to erase
}

void Point::MoveTo(int NewX, int NewY)
{
   Hide();          // make current point invisible
   X = NewX;        // change X and Y coordinates to new location
   Y = NewY;
   Show();          // show point at new location
}

// a general-purpose function for getting keyboard
// cursor movement keys (not a member function)

Boolean GetDelta(int& DeltaX, int& DeltaY)
{
   char KeyChar;
   Boolean Quit;
   DeltaX = 0;
   DeltaY = 0;

   do
{
      KeyChar = getch();     // read the keystroke
      if (KeyChar == 13)     // carriage return
         return(false);
      if (KeyChar == 0)      // an extended keycode
         {
          Quit = true;       // assume it is usable
          KeyChar = getch(); // get rest of keycode
              switch (KeyChar) {
                 case 72: DeltaY = -1; break; // down arrow
                 case 80: DeltaY =  1; break; // up arrow
                 case 75: DeltaX = -1; break; // left arrow
                 case 77: DeltaX =  1; break; // right arrow
                 default: Quit = false; // bad key
                 };
         };
   } while (!Quit);
   return(true);
}

void Point::Drag(int DragBy)
{
   int DeltaX, DeltaY;
   int FigureX, FigureY;

   Show(); // display figure to be dragged
   FigureX = GetX(); // get initial position of figure
   FigureY = GetY();

   // This is the drag loop
   while (GetDelta(DeltaX, DeltaY))
{
      // Apply delta to figure at X, Y
      FigureX += (DeltaX * DragBy);
      FigureY += (DeltaY * DragBy);
      MoveTo(FigureX, FigureY); // tell figure to move
      };
}
// Member functions for the Circle class

//constructor
Circle::Circle(int InitX, int InitY, int InitRadius) : Point (InitX, InitY)
{
   Radius = InitRadius;
}

void Circle::Show()
{
   Visible = true;
   circle(X, Y, Radius);     // draw the circle
}

void Circle::Hide()
{
   unsigned int TempColor;   // to save current color
   TempColor = getcolor();   // set to current color
   setcolor(getbkcolor());   // set drawing color to background
   Visible = false;
   circle(X, Y, Radius);     // draw in background color to
   setcolor(TempColor);      // set color back to current color
}

void Circle::Expand(int ExpandBy)
{
   Hide();                       // erase old circle
   Radius += ExpandBy;           // expand radius
   if (Radius < 0)               // avoid negative radius
      Radius = 0;
   Show();                       // draw new circle
}

void Circle::Contract(int ContractBy)
{
   Expand(-ContractBy);      // redraws with (Radius-ContractBy)
}

Program of ex8.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// ex8.prj consists of ex8.cpp,stack.cpp,list.cpp
// ex8.cpp:   Using the Stack Class
// from Hands-on C++
#include "stack.h"

main()
{
   Stack s(5);
   int i = 0;

   // Insert the numbers 1 through 5
   while (s.push(i+1) == 0)
      ++i;

   s.print();
   return 0;
}

Program of ex9.cpp

// Borland C++ - (C) Copyright 1991 by Borland International

// ex9.prj consists of ex9.cpp, list2.cpp, stack2.cpp
// ex9.cpp:   Using the print() virtual function
// from Hands-on C++
#include < iostream.h >
#include "stack2.h"

main()
{
   Stack s(5);
   List l, *lp;
   int i = 0;

   // Insert the numbers 1 through 5 into the stack
   while (s.push(i+1) == 0)
      ++i;

   // Put a couple of numbers into the list
   l.put_elem(1,0);
   l.put_elem(2,1);
   l.setn(2);

   cout << "Stack:\n";
   lp = &s;           // line 22
   lp->print();       // Invoke the Stack print() method; line 23

   cout << "\nList:\n";
   lp = &l;
   lp->print();       // Invoke the List print() method; line 27
   return 0;
}

Program of slot.cpp

// slot.cpp
// models a slot machine
#include          // for graphics functions
#include            // for rand(), randomize()
#include              // for randomize()
#include             // for getche()
#include               // for delay(), sound(), nosound()

const int W = 15;             // 1/2 width of images
const int MAR = 10;           // margin around images

class shape                   // base class
   {
   protected:
      int xCo, yCo;           // coordinates of center
      int linecolor;          // color of outline
      int fillcolor;          // color of interior
   public:
      shape()                 // no-arg constructor
	 { xCo=0; yCo=0; linecolor=WHITE; fillcolor=WHITE; }
      void set(int x, int y, int lc, int fc)  
	 { xCo=x; yCo=y; linecolor=lc; fillcolor=fc; }
      void draw()
	 {
	 setcolor(linecolor);                  // set line color
	 setfillstyle(SOLID_FILL, fillcolor);  // set fill color
	 }
   };

class ball : public shape
   {
   public:
      ball() : shape()        // no-arg constructor
	 { }                  
      void set(int x, int y, int lc, int fc)    // set data
	 { shape::set(x, y, lc, fc); }
      void draw()             // draw the ball
	 {
	 shape::draw();                         // set colors
	 circle(xCo, yCo, W);                   // draw circle
	 floodfill(xCo, yCo, linecolor);        // fill circle
	 }
   };

class rect : public shape
   {
   public:
      rect() : shape()        // no-arg constructor
	 { }
      void set(int x, int y, int lc, int fc)    // set data
	 { shape::set(x, y, lc, fc); }
      void draw()             // draw the rectangle
	 {
	 shape::draw();                         // set colors
	 rectangle(xCo-W, yCo-W, xCo+W, yCo+W); // draw rectangle
	 floodfill(xCo, yCo, linecolor);        // fill rectangle
         line(xCo-W, yCo+W, xCo+W, yCo-W);      // draw diagonal
	 }
   };

class tria : public shape
   {
   public:
      tria() : shape()        // no-arg constructor
	 { }
      void set(int x, int y, int lc, int fc)    // set data
	 { shape::set(x, y, lc, fc); }
      void draw();            // draw the triangle
   };

void tria::draw()             // draw the triangle
   {
   int triarray[] = { xCo,   yCo-W,      // top
		      xCo+W, yCo+W,      // bottom right
		      xCo-W, yCo+W };    // bottom left
   shape::draw();                        // set colors
   fillpoly(3, triarray);                // draw triangle
   }

class noshape : public shape
   {
   public:
      void erase();           // erase old shape
   };

void noshape::erase()         // erase old shape
   {
   int border[] =                        // rectangle to erase
	 { xCo-W-MAR, yCo-W-MAR,         // upper-left
	   xCo+W+MAR, yCo-W-MAR,         // upper-right
	   xCo+W+MAR, yCo+W+MAR,         // bottom-right
	   xCo-W-MAR, yCo+W+MAR };       // bottom-left
   setfillstyle(SOLID_FILL, DARKGRAY);   // background color
   fillpoly(4, border);                  // fill it
   }

class Cherry : public ball, public noshape
   {
   public:
      Cherry() : ball()                  // no-arg constructor
	 { }
      void set(int x, int y)             // set data
	 {
	 ball::set(x, y, WHITE, RED);
	 noshape::set(x, y, WHITE, RED);
	 }
      void draw()                        // draw a cherry
	 { erase(); ball::draw(); }      
   };

class Grape : public ball, public noshape
   {
   public:
      Grape() : ball()                   // no-arg constructor
	 { }
      void set(int x, int y)             // set data
	 {
	 ball::set(x, y, WHITE, BLUE);
	 noshape::set(x, y, WHITE, BLUE);
	 }
      void draw()                        // draw a grape
	 { erase(); ball::draw(); }      
   };

class Square : public rect, public noshape
   {
   public:
      Square() : rect()                  // no-arg constructor
	 { }
      void set(int x, int y)             // set data
	 {
	 rect::set(x, y, WHITE, CYAN);
	 noshape::set(x, y, WHITE, CYAN);
	 }
      void draw()                        // draw a square
	 { erase(); rect::draw(); }      
   };

class Pyramid : public tria, public noshape
   {
   public:
      Pyramid() : tria()                 // no-arg constructor
	 { }
      void set(int x, int y)             // set data
	 {
	 tria::set(x, y, WHITE, GREEN);
	 noshape::set(x, y, WHITE, GREEN);
	 }
      void draw()                        // draw a pyramid
	 { erase(); tria::draw(); }
   };

class Wheel        // contains four pips
   {
   private:
      Cherry ch;   // make one pip of each kind
      Grape gr;
      Square sq;
      Pyramid py;
      int xCo, yCo;             // wheel position   
   public:
      Wheel()                   // no-arg constructor
	 { xCo=0; yCo=0; }
      void set(int x, int y)
	 {
	 xCo=x; yCo=y;          // set our position
	 ch.set(xCo, yCo);      // set four pips to
	 gr.set(xCo, yCo);      //    our position
	 sq.set(xCo, yCo);
	 py.set(xCo, yCo);
	 }
      void draw();              // draw a random pip
   };

void Wheel::draw()              // draw a random pip
   {
   switch( random(4) )          // random number from 0 to 3
      {
      case 0 : ch.draw(); break;  // draw one of the pips
      case 1 : gr.draw(); break;  // selected randomly
      case 2 : sq.draw(); break;
      case 3 : py.draw(); break;
      }
   }

void main()
   {
   const int NUMBER = 60;      // number of times to cycle
   int driver, mode;
   driver = DETECT;            // set to best graphics mode
   initgraph(&driver, &mode, "\\bc45\\bgi");
   randomize();                // seed random number generator

   Wheel w1;                   // make three wheels
   Wheel w2;               
   Wheel w3;

   w1.set(100, 100);           // position in horizontal row
   w2.set(160, 100);
   w3.set(220, 100);
      
   for(int j=0; j