Project 14
Home ] Up ]

 

//Project14.cpp
//Date 10/03/2001
//Author: Us

////////////////////////////////////////////////////
//includes
////////////////////////////////////////////////////
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>



////////////////////////////////////////////////////
// constants
////////////////////////////////////////////////////
const int MAX_LEN = 20;

const char *testNames[] = 
  {
  "Chair", "Table", "Spoon", "Book", "Pen", "Eraser",
  "TV", "Stereo", "VCR", "DVD Player", "XBox", "Microwave",
  "Toaster", "Car", "Phone", "Couch", "Calculator",
  "Cookies"
  };

const int MAX_NAMES = sizeof(testNames)/sizeof(testNames[0]);



////////////////////////////////////////////////////
// test functions
////////////////////////////////////////////////////
void testInput(void);
void testSetName(void);
void testSetWeight(void);
void testConstructor(void);
void testConstructorCh(void);
void testConstructorCopy(void);
void testSet(void);
void testOperatorEqual(void);
void testGetName(void);
void testGetWeight(void);
void testOperatorLessThan(void);
void testOperatorAssign(void);
void testOperatorInsertion(void);
void testOperatorExtraction(void);


////////////////////////////////////////////////////
// CItem class
////////////////////////////////////////////////////
class CItem
  {
  private:
    char m_name[MAX_LEN+1];
    int  m_weight;
  public:
    CItem(void); 
    void input(void);
    void display(void) const;
    void set(char name[], int weight);
    void setName(char name[]);
    void setWeight(int weight);
    CItem(char name[], int weight); 
    CItem(char ch); 
    CItem(const CItem &item); 
    friend bool operator ==(const CItem &i1, const CItem &i2);
    char * getName(void);
    int getWeight(void);

    bool operator < (const CItem &i2);
    CItem & operator = (const CItem &i2);
    friend ostream & operator << (ostream &bob, const CItem &item);
    friend istream & operator >> (istream &joe, CItem &item);

  };



////////////////////////////////////////////////////
// main function
////////////////////////////////////////////////////
void main(void)
  {
  srand( (unsigned)time( NULL ) );

  //testInput();
  //testConstructor();
  //testConstructorCh();
  //testConstructorCopy();
  //testSetName();
  //testSetWeight();
  //testSet();
  //testOperatorEqual();
  //testGetName();
  //testGetWeight();
  //testOperatorLessThan();
  //testOperatorAssign();
  //testOperatorInsertion();
  //testOperatorExtraction();
  
  //Project#3

  //populate
  //display
  //sortBubble
  //main

  //main
  //do the following 5 time
  // populate
  // display
  // sortBubble
  // display


  
  //Create
  CItem items[5];
  for (int i=0; i<5; i++)
    {
    CItem item('r');
    items[i] = item;
    }

  //display
  for (i=0; i<5; i++)
    cout << items[i];

  //sort
  /*
  n = 5

  do the following
    sorted = true
    for i=0 to n-1 do the following
      if items[i] > items[i+1] then
          tItem = items[i]
          items[i] = items[i+1]
          items[i+1] = tItem
          sorted = false
          end if
      end for
    while not sorted
  */

  //display
  for (i=0; i<5; i++)
    cout << items[i];

  }


////////////////////////////////////////////////////
//overload >> for CItem
////////////////////////////////////////////////////
istream & operator >> (istream &joe, CItem &item)
  {
  cout << "item m_name:   ";
  joe >> item.m_name;
  cout << "item m_weight: ";
  joe >> item.m_weight;
  return joe;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// test operator >>
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorExtraction(void)
  {
  cout << "Test operator >>\n";
  cout << "=================\n";
  for (int i=1; i<=5; i++)
    {
    CItem item1;

    cin >> item1;
    cout << item1;

    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
//overload << for CItem
////////////////////////////////////////////////////
ostream & operator << (ostream &bob, const CItem &item)
  {
  bob << '[' << item.m_name << ", ";
  bob << item.m_weight << ']' << endl;
  return bob;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// test operator <<
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorInsertion(void)
  {
  cout << "Test operator <<\n";
  cout << "=================\n";
  for (int i=1; i<=5; i++)
    {
    CItem item1('r');

    cout << "item1 = "; item1.display();
    cout << "item1 = " << item1 << endl;

    cout << item1 << item1;

    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
// operator = overloaded
////////////////////////////////////////////////////
CItem & CItem::operator = (const CItem &i2)
  {
  strcpy(m_name, i2.m_name);
  this->m_weight = i2.m_weight;
  return *this;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorAssign
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorAssign(void)
  {
  cout << "Test operator =\n";
  cout << "===============\n";
  for (int i=1; i<=5; i++)
    {
    CItem item1('r'), item2('r'), item3('r');

    cout << "item1 = "; item1.display();
    cout << "item2 = "; item2.display();
    cout << "item3 = "; item3.display();

    item1 = item2 = item3;
    //item1.operator= (item2.operator= item3);
    //item1.operator = (item2.operator = (item3));
    item2.operator = (item3);
    item1.operator = (item2);

    cout << "After item1 = item2 = item3;\n";
    cout << "item1 = "; item1.display();
    cout << "item2 = "; item2.display();
    cout << "item3 = "; item3.display();

    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
// operator < overloaded  for CItem class objects
// returns true if weight of i1 is less than that of i2
// else returns false
////////////////////////////////////////////////////
bool CItem::operator < (const CItem &i2)
  {
  if (this->m_weight < i2.m_weight) 
      return true;
    else
      return false;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorLessThan
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorLessThan(void)
  {
  cout << "Test operator <\n";
  cout << "===============\n";
  for (int i=1; i<=5; i++)
    {
    CItem item1('r'), item2('r'), item3('r');

    cout << "item1 = "; item1.display();
    cout << "item2 = "; item2.display();
    cout << "item3 = "; item3.display();

    if (item1 < item2)
        cout << "item1 is lighter than item2\n";
      else
        cout << "item1 is not lighter than item2\n";

    if (item1 < item3)
        cout << "item1 is lighter than item3\n";
      else
        cout << "item1 is not lighter than item3\n";

    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
// getWeight
// returns the weight
////////////////////////////////////////////////////
int CItem::getWeight(void)
  {
  return m_weight;
  };


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testGetWeight
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testGetWeight(void)
  {
  cout << "test getWeight\n";
  for (int i=1; i<=5; i++)
    {
    CItem i('r');

    cout << "item = "; i.display();

    cout << "weight = " << i.getWeight() << endl;
    cout << "-----------------------------\n";
    }
  }


////////////////////////////////////////////////////
// getName
// returns the name
////////////////////////////////////////////////////
char * CItem::getName(void)
  {
  return m_name;
  };


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testGetName
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testGetName(void)
  {
  cout << "test getName\n";
  for (int i=1; i<=5; i++)
    {
    CItem i('r');

    cout << "item = "; i.display();

    cout << "name = " << i.getName() << endl;
    cout << "-----------------------------\n";
    }
  }


////////////////////////////////////////////////////
// operator ==
// compares two items for equality
////////////////////////////////////////////////////
bool operator ==(const CItem &i1, const CItem &i2)
  {
  if (i1.m_weight != i2.m_weight) 
      return false;

  if (strcmp(i1.m_name, i2.m_name) != 0) 
      return false;

  return true;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorEqual
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorEqual(void)
  {
  cout << "Test operator equal\n";
  cout << "===================\n";
  for (int i=1; i<=5; i++)
    {
    CItem item1('r'), item2('r'), item3(item1);

    cout << "item1 = "; item1.display();
    cout << "item2 = "; item2.display();
    cout << "item3 = "; item3.display();

    if (item1 == item2)
        cout << "item1 is equal to item2\n";
      else
        cout << "item1 is not equal to item2\n";

    if (item1 == item3)
        cout << "item1 is equal to item3\n";
      else
        cout << "item1 is not equal to item3\n";


    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
// setWeight(int weight)
// sets the m_weight data member to weight
////////////////////////////////////////////////////
void CItem::setWeight(int weight)
  {
  m_weight = weight;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSetWeight
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSetWeight(void)
  {
  cout << "Test setWeight(weight)\n";
  for (int i=1; i<=5; i++)
    {
    CItem item('r');
    item.display();

    int tWeight = rand()%100;

    cout << "tWeight = " << tWeight  << endl;

    item.setWeight(tWeight);

    cout << "After item1.setWeight(tWeight);\n";
    item.display();
    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
// setName(char name)
// sets the m_name data member to name
////////////////////////////////////////////////////
void CItem::setName(char name[])
  {
  strcpy(m_name, name);
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSetName
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSetName(void)
  {
  cout << "Test setName(name)\n";
  for (int i=1; i<=5; i++)
    {
    CItem item('r');
    item.display();

    int i = rand()%MAX_NAMES;
    char tName[MAX_LEN+1];
    strcpy(tName, testNames[i]);

    cout << "tName = \"" << tName << "\"" << endl;

    item.setName(tName);

    cout << "After item1.setName(tName);\n";

    item.display();
    cout << "---------------------------\n";
    }
  }


////////////////////////////////////////////////////
//CItem (item) copy constructor
// constructs an object which is a copy given object
////////////////////////////////////////////////////
CItem::CItem(const CItem &item)
  {
  strcpy(this->m_name, item.m_name);
  this->m_weight = item.m_weight;
  (*this).m_weight = item.m_weight;
  cout << "copy constructor called\n";
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructorCopy
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructorCopy(void)
  {
  cout << "Test copy Constructor\n";
  for (int i=1; i<=5; i++)
    {
    CItem item1('r');
    CItem item2(item1);
    item1.display();
    item2.display();
    }
  }


////////////////////////////////////////////////////
//CItem (ch) constructor
// constructs an object with random name and weight
////////////////////////////////////////////////////
CItem::CItem(char ch)
  {
  if ('r' == ch )
      {
      strcpy(m_name, testNames[rand()%MAX_NAMES]);
      m_weight = rand()%100;
      }
    else
      {
      strcpy(m_name, "");
      m_weight = -99;
      }
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructorCh
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructorCh(void)
  {
  cout << "Test ConstructorCh\n";
  CItem item1('r');
  CItem item2('m');
  item1.display();
  item2.display();
  }

    
////////////////////////////////////////////////////
//CItem (name, weight) constructor
// constructs an object with given name and weight
////////////////////////////////////////////////////
CItem::CItem(char name[], int weight)
  {
  strcpy(m_name, name);
  m_weight = weight;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructor
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructor(void)
  {
  cout << "Test Constructor(name, weight)\n";
  CItem item("Chair", 150);
  item.display();
  }


////////////////////////////////////////////////////
// display 
////////////////////////////////////////////////////
void CItem::display(void) const
  {
  cout << '[' << m_name << ", ";
  cout << m_weight << ']' << endl;
  }


////////////////////////////////////////////////////
//constructor default
////////////////////////////////////////////////////
CItem::CItem(void)
  {
  cout << "Default Constructor called\n";
  strcpy(m_name, "");
  m_weight = -99;
  }


////////////////////////////////////////////////////
//input function
// This is a member function for CItem class
// it gets the name and weight from the keyboard
// and sets the values of name and weight data members
////////////////////////////////////////////////////
void CItem::input(void)
  {
  cout << "item m_name:   ";
  cin >> m_name;
  cout << "item m_weight: ";
  cin >> m_weight;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testInput
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testInput(void)
  {
  cout << "test input function\n";
  CItem item1;
  item1.display();
  item1.input();
  item1.display();
  }


////////////////////////////////////////////////////
// set(name, weight)
////////////////////////////////////////////////////
void CItem::set(char name[], int weight)
  {
  strcpy(m_name, name);
  m_weight = weight;
  }


//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSet
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSet(void)
  {
  cout << "Test set(name, weight)\n";
  for (int i=1; i<=5; i++)
    {
    CItem item('r');
    item.display();
    int i = rand()%MAX_NAMES;
    char tName[MAX_LEN+1];
    strcpy(tName, testNames[i]);
    cout << "tName = \"" << tName << "\"" << endl;

    int tWeight = rand()%100;
    cout << "tWeight = " << tWeight  << endl;

    item.set(tName, tWeight);
    cout << "After item.set(tName, tWeight);\n";
    item.display();
    cout << "------------------------------------\n";
    }
  }