point00
Home ] Up ]

 

//point00.cpp
////////////////////////////////////////////////////////////
//include files
////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
////////////////////////////////////////////////////////////
//constants
////////////////////////////////////////////////////////////
int TEST_COUNT = 5;
////////////////////////////////////////////////////////////
//class definition
////////////////////////////////////////////////////////////
class CPoint
  {
  private: 
    int x;
    int y;
  public:
    CPoint (int a=0, int b=0);
    CPoint (char ch);
    void display(void) const;
    void setX (int a);
    void setY (int b);
    void setXY (int a, int b);
    int getX(void) const;
    int getY(void) const;
    void getXY (int &a, int &b) const;
    void copyTo (CPoint &p) const;
    void copyFrom (const CPoint &p);
    bool isEqualTo (const CPoint &p) const;
  };
////////////////////////////////////////////////////////////
//bool CPoint::isEqualTo (const CPoint &p) const
//Compares two points
//Will return true if the two points are equal
//Will return false if the two points are not equal
//CPoint p1('r'), p2('r');
//if (p1.isEqualTo(p2)) ....
//
//Compares x with p.x and y with p.y
// or compares (*this).x with p.x and (*this).y with p.y
// or compares this->x with p.x and this->y with p.y
////////////////////////////////////////////////////////////
bool CPoint::isEqualTo (const CPoint &p) const
  {
  return ((x == p.x) && (y == p.y));
  }
////////////////////////////////////////////////////////////
//void testIsEqualTo(void)
////////////////////////////////////////////////////////////
void testIsEqualTo(void)
  {
  cout << "testIsEqualTo(p)\n";
  cout << "================\n";
  for (int i=0;i<TEST_COUNT;i++)
    {
    CPoint p('r'), q('r');
    cout << "p = "; p.display();
    cout << "q = "; q.display();
    if (p.isEqualTo(q))
        cout << "p is equal to q\n";
      else
        cout << "p is NOT equal to q\n";
    p = q;
    cout << "After p = q;\n";
    cout << "p = "; p.display();
    cout << "q = "; q.display();
    
    if (p.isEqualTo(q))
        cout << "p is equal to q\n";
      else
        cout << "p is NOT equal to q\n";
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//CPoint::CPoint(int a, int b)
//CPoint constructor
//will set the values of x and y to a and b
////////////////////////////////////////////////////////////
CPoint::CPoint(int a, int b)
  {  
  x=a;
  y=b;
  } 
////////////////////////////////////////////////////////////
//void CPoint::display(void) const
//display function
//will display on the screen 
//values of x and y;
////////////////////////////////////////////////////////////
void CPoint::display(void) const
  {
  cout<<"("<<x<<","<<y<<")\n";
  }  
////////////////////////////////////////////////////////////
//void testConstructorAB(void)
//Creates a point p with randomly
//generated a and b values;
////////////////////////////////////////////////////////////
void testConstructorAB(void)
  {
  cout<<"testConstructorAB\n";
  cout<<"-----------------\n";
  for (int i=0;i<TEST_COUNT;i++)
    {
    int a = rand()%100;
    int b = rand()%100;
    cout << "a= " << a << endl;
    cout << "b= " << b << endl;
            
    CPoint p(a,b);
    cout << "CPoint p(a,b); => p = "; p.display();
    CPoint q(a);
    cout << "CPoint q(a);   => q = "; q.display();
    CPoint r;
    cout << "CPoint r;      => r = "; r.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//CPoint::CPoint(char ch)
//CPoint general constructor
//either generates random point if ('r'==ch)
//or prompts for keyboard input if ('i'==ch)
//or sets x and y equal to 0
////////////////////////////////////////////////////////////
CPoint::CPoint(char ch)
  { 
  if ('r'==ch)
      {
      x=rand()%100;
      y=rand()%100;
      if (rand()%2==0)
          x=-x;
      if (rand()%2==0)
          y=-y;
      }
    else if ('i'==ch)
      {  
      cout<<"Enter x coordinate:";
      cin >> x;
      cout<<"Enter y coordinate:";    
      cin >> y;
      }
    else
      {
      x=0;
      y=0;
      } 
  }
////////////////////////////////////////////////////////////
//void testConstructorRandom(void)
////////////////////////////////////////////////////////////
void testConstructorRandom(void)
  {
  cout<<"testConstructorRandom\n";
  cout<<"---------------------\n";
  for (int i=0;i<TEST_COUNT;i++)
    {
    CPoint p('r'), q('x') /*, r('i')*/;
    cout << "CPoint p('r') => p = "; p.display();
    cout << "CPoint q('x') => q = "; q.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void CPoint::setX(int a)
//sets the value of member x to the value of a
////////////////////////////////////////////////////////////
void CPoint::setX(int a)
  {
  x=a;
  }
////////////////////////////////////////////////////////////
//void testSetX(void)
////////////////////////////////////////////////////////////
void testSetX(void)
  {
  cout<<"test setX\n";
  cout<<"---------\n";
  for (int i=0; i<TEST_COUNT; i++)
    {
    CPoint p('r');
    cout << "p = " ; 
    p.display();
    
    int x = rand()%100;
    cout << "x = " << x << endl;
    p.setX(x);
    cout << "after p.setX(x);\n";
    cout << "p = " ; 
    p.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void CPoint::setY(int b)
//sets the value of y to b
////////////////////////////////////////////////////////////
void CPoint::setY(int b)
  { 
  y = b;
  }
////////////////////////////////////////////////////////////
//void testSetY(void)
////////////////////////////////////////////////////////////
void testSetY(void)
  {
  cout<<"test setY\n";
  cout<<"---------\n";
  for (int i=0; i<TEST_COUNT; i++)
    {
    CPoint p('r');
    cout << "p = " ; 
    p.display();
    
    int y = rand()%100;
    cout << "y = " << y << endl;
    p.setY(y);
    cout << "after p.setY(y);\n";
    cout << "p = " ; 
    p.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void CPoint::setXY(int a, int b)
//sets values of x and y to a and b
////////////////////////////////////////////////////////////
void CPoint::setXY(int a, int b)
  {
  x=a;
  y=b;
  }  
////////////////////////////////////////////////////////////
//void testSetXY(void)
////////////////////////////////////////////////////////////
void testSetXY(void)
  {
  cout<<"test setXY\n";
  cout<<"----------\n";
  for (int i=0; i<TEST_COUNT; i++)
    {
    CPoint p('r');
    cout << "p = " ; p.display();
    
    int x = rand()%100;
    int y = rand()%100;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    p.setXY(x, y);
    cout << "after p.setXY(x, y);\n";
    cout << "p = " ; p.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//int CPoint::getX(void) const
//this function returns 
//the x coordinate of a point p
////////////////////////////////////////////////////////////
int CPoint::getX(void) const
  {
  return x;
  }
////////////////////////////////////////////////////////////
// testGetX(void) 
////////////////////////////////////////////////////////////
void testGetX(void)
  {
  cout<<"testGetX\n";  
  cout<<"--------\n";
  for (int i=0; i<TEST_COUNT; i++)
    { 
    CPoint p('r');
    cout << "p = "; 
    p.display();
    cout << "x = " << p.getX() << endl;
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//int CPoint::getY(void) const
//returns y coordinate of a point p
////////////////////////////////////////////////////////////
int CPoint::getY(void) const
  {
  return y;
  }
////////////////////////////////////////////////////////////
//void testGetY(void) 
////////////////////////////////////////////////////////////
void testGetY(void) 
  {
  cout<<"testGetY\n";
  cout<<"--------\n";
  
  for (int i=0; i<TEST_COUNT; i++)
    { 
    CPoint p('r');
    cout << "p = ";
    p.display();
    cout << "y = " << p.getY() << endl;
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void CPoint::getXY(int &a, int &b) const
//returns values of x and y through a and b
////////////////////////////////////////////////////////////
void CPoint::getXY(int &a, int &b) const
  { 
  a=x;
  b=y;
  }
////////////////////////////////////////////////////////////
//void testGetXY(void)
////////////////////////////////////////////////////////////
void testGetXY(void)
  {
  cout<<"testGetXY\n";
  cout<<"---------\n";
  for (int i=0; i<TEST_COUNT; i++)
    { 
    CPoint q('r');
    cout << "q = ";
    q.display();
    int xCoord, yCoord;
    q.getXY(xCoord, yCoord);
    cout << "x = " << xCoord << endl;
    cout << "y = " << yCoord << endl;
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void CPoint::copyTo(CPoint &p) const
//copies the object in operation to the point p  
//example: 
//CPoint q('r'), p('r');
//q.copyTo(p) is equivalent to p=q
////////////////////////////////////////////////////////////
void CPoint::copyTo(CPoint &p) const
  {                  
  p.x = this->x;
  p.y = (*this).y;
  }
////////////////////////////////////////////////////////////
//void testCopyTo(void)
////////////////////////////////////////////////////////////
void testCopyTo(void)
  {
  cout<<"testCopyTo\n";
  cout<<"----------\n";
  for (int i=0; i<TEST_COUNT; i++)
    {
    CPoint q('r'), p('r');
    cout << "p = "; p.display();
    cout << "q = "; q.display();
    q.copyTo(p);
    cout<<"after q.copyTo(p);\n";
    cout << "p = "; p.display();
    cout << "q = "; q.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void CPoint::copyFrom (const CPoint &p)                
//copies point p to the point in operation
//example: 
//CPoint q('r'), p('r');
//q.copyFrom(p) is equivalent to q=p  
////////////////////////////////////////////////////////////
void CPoint::copyFrom (const CPoint &p)                
  {
  x=p.x;
  y=p.y;
  }
////////////////////////////////////////////////////////////
//void testCopyFrom(void)
////////////////////////////////////////////////////////////
void testCopyFrom(void)
  {
  cout<<"testCopyFrom\n";
  cout<<"------------\n";
  for (int i=0; i<TEST_COUNT; i++)
    {
    CPoint q ('r'), p ('r');
    cout << "p = "; p.display();
    cout << "q = "; q.display();
    q.copyFrom(p);
    cout<<"after q.copyFrom(p);\n";
    cout << "p = "; p.display();
    cout << "q = "; q.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////
//void testAll(void)
////////////////////////////////////////////////////////////
void testAll(void)
  {
  testConstructorAB();
  testConstructorRandom();  
  testSetX();  
  testSetY(); 
  testSetXY();
  testGetX();
  testGetY();
  testGetXY();  
  testCopyTo();
  testCopyFrom();
  testIsEqualTo();
  }
////////////////////////////////////////////////////////////
//main function
////////////////////////////////////////////////////////////
void main (void)//main function
  {
  //testConstructorAB();
  //testConstructorRandom();  
  //testSetX();  
  //testSetY(); 
  //testSetXY();
  //testGetX();
  //testGetY();
  //testGetXY();  
  //testCopyTo();
  //testCopyFrom();
  //testIsEqualTo();
  testAll();
  }
/*
SAMPLE TEST RUN
testConstructorAB
-----------------
a= 41
b= 67
CPoint p(a,b); => p = (41,67)
CPoint q(a);   => q = (41,0)
CPoint r;      => r = (0,0)
a= 34
b= 0
CPoint p(a,b); => p = (34,0)
CPoint q(a);   => q = (34,0)
CPoint r;      => r = (0,0)
a= 69
b= 24
CPoint p(a,b); => p = (69,24)
CPoint q(a);   => q = (69,0)
CPoint r;      => r = (0,0)
a= 78
b= 58
CPoint p(a,b); => p = (78,58)
CPoint q(a);   => q = (78,0)
CPoint r;      => r = (0,0)
a= 62
b= 64
CPoint p(a,b); => p = (62,64)
CPoint q(a);   => q = (62,0)
CPoint r;      => r = (0,0)
testConstructorRandom
---------------------
CPoint p('r') => p = (5,45)
CPoint q('x') => q = (0,0)
CPoint p('r') => p = (61,-91)
CPoint q('x') => q = (0,0)
CPoint p('r') => p = (27,-36)
CPoint q('x') => q = (0,0)
CPoint p('r') => p = (-2,-53)
CPoint q('x') => q = (0,0)
CPoint p('r') => p = (-21,16)
CPoint q('x') => q = (0,0)
test setX
---------
p = (47,-26)
x = 69
after p.setX(x);
p = (69,-26)
p = (12,67)
x = 94
after p.setX(x);
p = (94,67)
p = (-3,11)
x = 73
after p.setX(x);
p = (73,11)
p = (64,41)
x = 68
after p.setX(x);
p = (68,41)
p = (-47,44)
x = 37
after p.setX(x);
p = (37,44)
test setY
---------
p = (59,23)
y = 78
after p.setY(y);
p = (59,78)
p = (-16,-35)
y = 88
after p.setY(y);
p = (-16,88)
p = (-6,-40)
y = 48
after p.setY(y);
p = (-6,48)
p = (-46,5)
y = 70
after p.setY(y);
p = (-46,70)
p = (50,6)
y = 48
after p.setY(y);
p = (50,48)
test setXY
----------
p = (-29,-23)
x = 56
y = 40
after p.setXY(x, y);
p = (56,40)
p = (66,-76)
x = 44
y = 39
after p.setXY(x, y);
p = (44,39)
p = (26,-23)
x = 18
y = 82
after p.setXY(x, y);
p = (18,82)
p = (29,41)
x = 39
y = 58
after p.setXY(x, y);
p = (39,58)
p = (4,-30)
x = 73
y = 86
after p.setXY(x, y);
p = (73,86)
testGetX
--------
p = (-21,-45)
x = -21
p = (70,29)
x = 70
p = (-97,-12)
x = -97
p = (61,36)
x = 61
p = (55,-74)
x = 55
testGetY
--------
p = (50,-50)
y = -50
p = (66,30)
y = 30
p = (7,37)
y = 37
p = (53,83)
y = 83
p = (9,-58)
y = -58
testGetXY
---------
q = (-22,-46)
x = -22
y = -46
q = (-13,68)
x = -13
y = 68
q = (-62,55)
x = -62
y = 55
q = (-24,37)
x = -24
y = 37
q = (-95,-41)
x = -95
y = -41
testCopyTo
----------
p = (-96,21)
q = (-91,-36)
after q.copyTo(p);
p = (-91,-36)
q = (-91,-36)
p = (-53,-99)
q = (68,-84)
after q.copyTo(p);
p = (68,-84)
q = (68,-84)
p = (-28,93)
q = (0,88)
after q.copyTo(p);
p = (0,88)
q = (0,88)
p = (13,-14)
q = (-7,21)
after q.copyTo(p);
p = (-7,21)
q = (-7,21)
p = (-19,56)
q = (-35,51)
after q.copyTo(p);
p = (-35,51)
q = (-35,51)
testCopyFrom
------------
p = (89,2)
q = (-24,8)
after q.copyFrom(p);
p = (89,2)
q = (89,2)
p = (-14,-3)
q = (93,43)
after q.copyFrom(p);
p = (-14,-3)
q = (-14,-3)
p = (98,-81)
q = (-58,-18)
after q.copyFrom(p);
p = (98,-81)
q = (98,-81)
p = (-38,92)
q = (-9,-57)
after q.copyFrom(p);
p = (-38,92)
q = (-38,92)
p = (-15,88)
q = (-90,57)
after q.copyFrom(p);
p = (-15,88)
q = (-15,88)
testIsEqualTo(p)
================
p = (-2,34)
q = (-28,-46)
p is NOT equal to q
After p = q;
p = (-28,-46)
q = (-28,-46)
p is equal to q
p = (75,-33)
q = (44,-16)
p is NOT equal to q
After p = q;
p = (44,-16)
q = (44,-16)
p is equal to q
p = (22,51)
q = (-57,76)
p is NOT equal to q
After p = q;
p = (-57,76)
q = (-57,76)
p is equal to q
p = (-75,-12)
q = (3,-69)
p is NOT equal to q
After p = q;
p = (3,-69)
q = (3,-69)
p is equal to q
p = (1,89)
q = (-2,85)
p is NOT equal to q
After p = q;
p = (-2,85)
q = (-2,85)
p is equal to q
Press any key to continue
*/