Professional Documents
Culture Documents
12 - Part II:
Pointers, Classes, and Lists
Objectives (contd.)
In this chapter, you will
Discover the peculiarities of classes with pointer member
variables
Examine the relationship between the address of operator
and classes
Become aware of abstract classes
Thus,
(*studentPtr).gpa = 3.9;
is equivalent to:
studentPtr->gpa = 3.9;
void classExample::setX(int a)
{ x = a;
}
void classExample::print()
{
cout << "x = " << x << endl;
}
int main()
{
classExample *cExpPtr;
classExample cExpObject;
cExpPtr = &cExpObject;
cExpPtr->setX(5);
cExpPtr->print(); // x = 5
return 0;
}
C++ Programming: Program Design Including Data Structures, Sixth Edition 7
Classes and Pointers: Some
Peculiarities
Example class:
private:
int x;
int lenP;
int *p;
};
if (sizeP <= 0)
{
cout << "The array size must be positive." << endl;
cout << "Creating an array of size 10." << endl;
lenP = 10;
}
else
lenP = sizeP;
p = new int[lenP];
}
pointerDataClass::~pointerDataClass()
{
delete [] p;
}
lenP = otherObject.lenP;
p = new int[lenP]; // the declared object has its own array
int main()
{
pointerDataClass one(5);
one.setData();
one.print();
pointerDataClass two(one);
two.print();
two.destroyP();
one.print();
testCopyConst(one);
one.print();
return 0;
}
class arrayListType
{
public:
bool isEmpty();
bool isFull();
int listSize();
int maxListSize();
void print();
bool isItemAtEqual(int location, int item);
void insertAt(int location, int insertItem);
void insertEnd(int insertItem);
void removeAt(int location);
void retrieveAt(int location, int& retItem);
void replaceAt(int location, int repItem);
void clearList();
int seqSearch(int item) const;
private:
int *list;
int length;
int maxSize;
};
#endif
bool arrayListType::isEmpty()
{ return (length == 0);
}
bool arrayListType::isFull()
{ return (length == maxSize);
}
int arrayListType::listSize()
{ return length;
}
int arrayListType::maxListSize()
{ return maxSize;
}
void arrayListType::print()
{ int i;
for (i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
bool arrayListType::isItemAtEqual(int location, int item)
{
return(list[location] == item);
}
void arrayListType::insertAt(int location, int insertItem) // shift then insert
{ int i;
if (location < 0 || location >= length)
cout << "The position of the item to be inserted is out of range." << endl;
else
if (length == maxSize)
cout << "Cannot insert in a full list." << endl;
else
{
for (i = length; i > location; i--) // elements are in [0, length-1]
list[i] = list[i - 1]; // move the elements down
// in the last iteration: list[location+1]=list[location];
list[location] = insertItem;
length++;
}
}
void arrayListType::insertEnd(int insertItem)
{ if (length == maxSize)
cout << "Cannot insert in a full list." << endl;
else
{ list[length] = insertItem; length++;
}
}
void arrayListType::removeAt(int location) // shift elements up by one position
{ int i;
if (location < 0 || location >= length)
cout << "The location of the item to be removed is out of range." << endl;
else
{ for (i = location; i < length - 1; i++)
list[i] = list[i+1];
length--;
}
}
void arrayListType::retrieveAt(int location, int& retItem)
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is out of range." << endl;
else
retItem = list[location];
}
void arrayListType::replaceAt(int location, int repItem)
{ if (location < 0 || location >= length)
cout << "The location of the item to be replaced is out of range." << endl;
else
list[location] = repItem;
}
void arrayListType::clearList()
{ length = 0;
}
arrayListType::arrayListType(int size)
{ if (size <= 0)
{
cout << "The array size must > 0. Creating an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int[maxSize];
}
arrayListType::~arrayListType()
{
delete [] list;
}
maxSize = otherList.maxSize;
length = otherList.length;
list = new int[maxSize];
int loc;
if (length == 0)
cout << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list." << endl;
}
int main()
{ arrayListType list, list2;
int num;
cout << "Enter numbers ending with -999" << endl;
cin >> num;
while(num != -999)
{ list.insert(num);
cin >> num;
}
cout << "The list you entered is: " << endl;
list.print(); list2 = list;
cout << "The list size is: " << list.listSize() << endl;
cout << "Enter the item to be deleted: ";
cin >> num;
return 0;
}
Printing List #2
123456789
int main()
{ string str1 = "Hello";
string str2 = "Happy";
cout << "Larger of 5 and 6 = " << larger(5, 6) << endl;
cout << "Larger of A and B = " << larger('A', 'B') << endl;
cout << "Larger of 5.6 and 3.2 = " << larger(5.6, 3.2) << endl;
cout << "Larger of " << str1 << " and "
<< str2 << " = " << larger(str1, str2) << endl;
return 0;
}
template<class elemType>
class arrayListType
{public:
arrayListType<elemType>
operator=(arrayListType<elemType>&);
bool isEmpty();
void print();
bool isItemAtEqual(int location, elemType& item);
.
arrayListType(int size = 100);
arrayListType (arrayListType< elemType >& otherList);
~arrayListType();
protected:
elemType *list;
int length;
int maxSize;
};
template <class elemType>
bool arrayListType<elemType>::isEmpty()
{
return (length == 0);
}
return 0;
}
C++ Programming: Program Design Including Data Structures, Sixth Edition 61
Processing the integer list
Enter 5 integers: 12 23 34 45 56
The list you entered is: 12 23 34 45 56
Enter the item to be deleted: 23
After removing 23 the list is:
12 34 45 56
Processing the string list
Enter 5 strings: hi hello bye for now
The list you entered is:
hi hello bye for now
Enter the string to be deleted: bye
After removing bye the list is:
Hi hello for now