//Project07.cpp
//Date 09/14/2001
//Auothor: Us
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int const 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]);
void testInput(void);
void testSetName(void);
void testSetWeight(void);
void testConstructor(void);
void testConstructorCh(void);
void testConstructorCopy(void);
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); //09/17/2001
CItem(char ch); // CItem item('r');
CItem(const CItem &item); //copy constructor
};
void main(void)
{
srand( (unsigned)time( NULL ) );
//testInput();
//testConstructor();
//testConstructorCh();
testConstructorCopy();
}
//CItem (item) copy constructor
/* constructs an object which is a copy given object
*/
CItem::CItem(const CItem &item)
{
strcpy(m_name, item.m_name);
m_weight = item.m_weight;
}
void testConstructorCopy(void)
{
cout << "Test copy Constructor\n";
CItem item('r');
CItem item2(item);
item.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;
}
}
void testConstructorCh(void)
{
cout << "Test Constructor\n";
CItem item('m');
item.display();
item.display();
item.display();
item.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;
}
void testConstructor(void)
{
CItem item("Chair", 150);
item.display();
}
void CItem::display(void) const
{
cout << "item info: ";
cout << m_name << ", ";
cout << m_weight << endl;
}
CItem::CItem(void)
{
cout << "Constructor called\n";
strcpy(m_name, "");
m_weight = 0;
}
///////////////////////////////////////////
//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;
}
void testInput(void)
{
cout << "test input function member\n";
CItem item1;
item1.display();
item1.input();
item1.display();
}
void CItem::set(char name[], int weight)
{
strcpy(m_name, name);
m_weight = weight;
}
/* SAMPLE RUN
test input function member
Constructor called
item info: , 0
item m_name:
*/
void leftOver(void)
{
/*
CItem item;
item.display();
CItem *p;
p = &item;
(*p).display();
p->display();
*/
/*
CItem items[500];
for (int i=0; i<=499; i++)
items[i].display();
*/
/*
CItem item;
item.set("Hammer", 200);
item.display();
char tname[15];
int tweight;
cin >> tname;
cin >> tweight;
item.set(tname, tweight);
item.display();
*/
/*
if (item1 == item2)
cout << "== works\n";
*/
/*
cout << item1;
*/
/*
if (item1 != item2)
cout << "!= works\n";
*/
/*
cin >> item1;
*/
}
|