home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file19 / shape.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  2.8 KB  |  129 lines

  1. ////////////////////////////////////////////////////////////
  2. // shape.cpp: Methods for the shape class.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved. 
  4. ////////////////////////////////////////////////////////////
  5. #include "shape.h"
  6.  
  7. int Shape::objects_needing_destruction = 0;
  8. int Shape::verbose = 1; // ctor/dtor message control
  9.  
  10. Shape::Shape()
  11. {
  12.   x = 0; y = 0;
  13.   if (verbose) cout << "Default Shape constructor called\n";
  14.   objects_needing_destruction++;
  15. }
  16.  
  17. Shape::Shape(const Shape &p)
  18. {
  19.   x = p.x; y = p.y;
  20.   if (verbose) cout << "Shape copy constructor called\n";
  21.   objects_needing_destruction++;
  22. }
  23.  
  24. Shape::Shape(int xx, int yy)
  25. {
  26.   x = xx; y = yy;
  27.   if (verbose) cout << "General Shape constructor called\n";
  28.   objects_needing_destruction++;
  29. }
  30.  
  31. Shape::~Shape()
  32. {
  33.   if (verbose) cout << "Shape destructor called\n";
  34.   objects_needing_destruction--;
  35. }
  36.  
  37. Shape &Shape::operator=(const Shape &p)
  38. {
  39.   x = p.x; y = p.y;
  40.   if (verbose) cout << "Shape assignment called\n";
  41.   return *this;
  42. }
  43.  
  44. int operator==(const Shape &a, const Shape &b)
  45. // FRIEND function. Returns 1 if data members
  46. // are the same, else 0.
  47. {
  48.   return a.x == b.x && a.y == b.y;
  49. }
  50.  
  51. int operator<(const Shape &a, const Shape &b)
  52. // FRIEND function. Returns a 1 if distance of
  53. // a from origin is less than b's distance.
  54. {
  55.   return a.x*a.x + a.y*a.y < b.x*b.x + b.y*b.y;
  56. }
  57.  
  58. int operator>(const Shape &a, const Shape &b)
  59. // FRIEND function. Returns a 1 if distance of
  60. // a from origin is less than b's distance.
  61. {
  62.   return a.x*a.x + a.y*a.y > b.x*b.x + b.y*b.y;
  63. }
  64.  
  65. ostream &Shape::Print(ostream &os) const
  66. {
  67.   return os << *this;
  68. }
  69.  
  70. ostream &operator<<(ostream &os, const Shape &p)
  71. {
  72.   return os << '(' << p.x << ',' << p.y << ')';
  73. }
  74.  
  75.  
  76. Circle::Circle()
  77. {
  78.   r = 0;
  79.   if (verbose) cout << "Default Circle constructor called\n";
  80.   objects_needing_destruction++;
  81. }
  82.  
  83. Circle::Circle(const Circle &p)
  84. : Shape(p)
  85. {
  86.   r = p.r;
  87.   if (verbose) cout << "Circle copy constructor called\n";
  88.   objects_needing_destruction++;
  89. }
  90.  
  91. Circle::Circle(int xx, int yy, int rr)
  92. : Shape(xx, yy)
  93. {
  94.   r = rr;
  95.   if (verbose) cout << "Parameterized Circle constructor called\n";
  96.   objects_needing_destruction++;
  97. }
  98.  
  99. Circle::~Circle()
  100. {
  101.   if (verbose) cout << "Circle destructor called\n";
  102.   objects_needing_destruction--;
  103. }
  104.  
  105. Circle &Circle::operator=(const Circle &p)
  106. {
  107.   x = p.x; y = p.y; r = p.r;
  108.   if (verbose) cout << "Circle assignment called\n";
  109.   return *this;
  110. }
  111.  
  112. int operator==(const Circle &a, const Circle &b)
  113. // FRIEND function. Returns 1 if data members
  114. // are the same, else 0.
  115. {
  116.   return a.x == b.x && a.y == b.y && a.r == b.r;
  117. }
  118.  
  119.  
  120. ostream &Circle::Print(ostream &os) const
  121. {
  122.   return os << *this;
  123. }
  124.  
  125. ostream &operator<<(ostream &os, const Circle &p)
  126. {
  127.   return os << '(' << p.x << ',' << p.y << ',' << p.r << ')';
  128. }
  129.