You are on page 1of 62

Chapter

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

C++ Programming: Program Design Including Data Structures, Sixth Edition 2


Classes, structs, and Pointer
Variables
You can declare pointers to classes:
class
public:

student is an object of type studentType


studentPtr is a pointer variable of type
studentType

C++ Programming: Program Design Including Data Structures, Sixth Edition 3


Classes, structs, and Pointer
Variables (contd.)
To store address of student in studentPtr:
studentPtr = &student;
To store 3.9 in component gpa of student:
(*studentPtr).gpa = 3.9;
Parenthesis ( ) used because dot operator has higher
precedence than dereferencing operator
Alternative: use member access operator arrow (->)

C++ Programming: Program Design Including Data Structures, Sixth Edition 4


Classes, structs, and Pointer
Variables (contd.)
Syntax to access a class member using the
operator -> :

Thus,
(*studentPtr).gpa = 3.9;
is equivalent to:
studentPtr->gpa = 3.9;

C++ Programming: Program Design Including Data Structures, Sixth Edition 5


class classExample
{ public:
void setX(int a);
void print() const;
private:
int x;
};

void classExample::setX(int a)
{ x = a;
}
void classExample::print()
{
cout << "x = " << x << endl;
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 6


#include <iostream>
#include "classExample.h"
using namespace std;

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:

Pointer p can be used to allocate a dynamic array


Example program statements:

C++ Programming: Program Design Including Data Structures, Sixth Edition 8


Destructor
If objectOne goes out of scope, its member
variables are destroyed
Memory space of dynamic array stays marked as allocated,
even though it cannot be accessed
Solution: in destructor, ensure that when
objectOne goes out of scope, its array memory is
deallocated:

C++ Programming: Program Design Including Data Structures, Sixth Edition 9


Assignment Operator
After a shallow copy: if objectTwo.p deallocates
memory space to which it points, objectOne.p
becomes invalid

Solution: extend definition of the assignment


operator to avoid shallow copying of data

C++ Programming: Program Design Including Data Structures, Sixth Edition 10


Copy Constructor
Default member-wise initialization:
Initializing a class object by using the value of an existing
object of the same type
Example:
ptrMemberVarType objectThree(objectOne);
Copy constructor: provided by the compiler
Performs this initialization
Leads to a shallow copying of the data if class has pointer
member variables

C++ Programming: Program Design Including Data Structures, Sixth Edition 11


Copy Constructor (contd.)
Similar problem occurs when passing objects by
value
Copy constructor automatically executes in three
situations:
When an object is declared and initialized by using the
value of another object
When an object is passed by value as a parameter
When the return value of a function is an object

C++ Programming: Program Design Including Data Structures, Sixth Edition 12


Copy Constructor (contd.)
Solution: override the copy constructor

For classes with pointer member variables, three


things are normally done:
Include the destructor in the class
Overload the assignment operator for the class
Include the copy constructor

C++ Programming: Program Design Including Data Structures, Sixth Edition 13


class pointerDataClass
{
public:
void print() const;
void setData();
void destroyP();
pointerDataClass(int sizeP = 10);
~pointerDataClass();
pointerDataClass (const pointerDataClass& otherObject);

private:
int x;
int lenP;
int *p;
};

C++ Programming: Program Design Including Data Structures, Sixth Edition 14


#include <iostream>
#include "ptrDataClass.h"
using namespace std;

void pointerDataClass::print() const


{ cout << "x = " << x << endl;
cout << "p = ";
for (int i = 0; i < lenP; i++)
cout << p[i] << " ";
cout << endl;
}

void pointerDataClass::setData() // set data through keyboard from user


{
cout << "Enter an integer for x: ";
cin >> x;
cout << endl;
cout << "Enter " << lenP << " numbers: ";
for (int i = 0; i < lenP; i++)
cin >> p[i];
cout << endl;
}
void pointerDataClass::destroyP()
{ lenP = 0;
delete [] p;
p = NULL;
}
pointerDataClass::pointerDataClass(int sizeP)
{
x = 0;

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;
}

pointerDataClass::pointerDataClass (const pointerDataClass&


otherObject)
{
x = otherObject.x;

lenP = otherObject.lenP;
p = new int[lenP]; // the declared object has its own array

for (int i = 0; i < lenP; i++) // deep copy


p[i] = otherObject.p[i];

C++ Programming: Program Design Including Data Structures, Sixth Edition 17


#include <iostream>
#include "ptrDataClass.h"
using namespace std;
void testCopyConst(pointerDataClass temp);

int main()
{
pointerDataClass one(5);
one.setData();
one.print();
pointerDataClass two(one);
two.print();
two.destroyP();
one.print();
testCopyConst(one);

one.print();

return 0;
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 18


void testCopyConst(pointerDataClass temp)
{
temp.print();
temp.setData();
temp.print(); Enter an integer for x: 10
} Enter 5 numbers: 23 45 67 89 76
x = 10 // printing one
p = 23 45 67 89 76
x = 10 // printing two
p = 23 45 67 89 76
x = 10 // printing one after destroying two
p = 23 45 67 89 76
x = 10 // printing one in testCopyConst
p = 23 45 67 89 76
Enter an integer for x: 15 // setting one
Enter 5 numbers: 3 4 5 6 7
x = 15 // printing one in testCopyConst
p=34567
x = 10 // printing one in main
p = 23 45 67 89 76

C++ Programming: Program Design Including Data Structures, Sixth Edition 19


Array Based Lists
List: a collection of element of the same type
Length of a list: the number of elements in a list
Array: an effective way to store a list
Three variables needed to process a list:
list: an array that holds the elements
length: number of elements currently in array
maxSize: maximum number of elements that can be
stored (array size)

C++ Programming: Program Design Including Data Structures, Sixth Edition 20


Array Based Lists
Array-Based Lists

Operations performed on a list are:


Create the list and initialize to an empty state
Determine whether the list is empty
Determine whether the list is full
Find the size of the list
Destroy, or clear the list

C++ Programming: Program Design Including Data Structures, Sixth Edition 22


Array-Based Lists (continued)
Operations performed on a list (continued):
Determine whether an item is the same as a given list
element
Insert an item in the list at the specified location
Remove an item from the list at the specified location
Replace an item at the specified location with another
item
Retrieve an item from the list at the specified location
Search the list for a given item

C++ Programming: Program Design Including Data Structures, Sixth Edition 23


#ifndef H_arrayListType
#define H_arrayListType

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;

C++ Programming: Program Design Including Data Structures, Sixth Edition 24


void insert(int insertItem);
void remove(int removeItem);

arrayListType(int size = 100); // constructor


// copy constructor
arrayListType (const arrayListType& otherList);
// overloading the assignment operator
arrayListType operator=(arrayListType & otherList);
~arrayListType(); // destructor

private:
int *list;
int length;
int maxSize;
};

#endif

C++ Programming: Program Design Including Data Structures, Sixth Edition 25


#include <iostream>
#include "arrayListType.h"
using namespace std;

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;
}

//copy constructor that ensures deep copy


arrayListType::arrayListType(const arrayListType& otherList)
{
int j;

maxSize = otherList.maxSize;
length = otherList.length;
list = new int[maxSize];

for (j = 0; j < length; j++)


list [j] = otherList.list[j];
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 30


Overloading the Assignment Operator (=)

Every object of a class maintains a (hidden) pointer


to itself called this

this is a reserved word

When an object invokes a member function


the this pointer is referenced by the member function

C++ Programming: Program Design Including Data Structures, Sixth Edition 31


Overloading the Assignment Operator (=)
arrayListType arrayListType ::operator= (arrayListType & otherList)
{
if (this != &otherList) // Avoid copying an element to itself since
{
delete [ ] list; // we destroy the list to be copied to!
maxSize = otherList.maxSize;
length = otherList.length;

list = new int[maxSize];

for (int i = 0; i < length; i++)


list[i] = otherList.list[i];
}
return *this;
}
The return type arrayListType allows: list1 = list2 = list3;

C++ Programming: Program Design Including Data Structures, Sixth Edition 32


int arrayListType::seqSearch(int item) const
{
bool found = false;

for (int loc = 0; loc < length; loc++)


if (list[loc] == item)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 33


void arrayListType::insert(int insertItem) // insert item only if NOT in list
{ int loc; // the item will be inserted at the end
if (length == 0) // in case of empty list
list[length++] = insertItem;
else
if (length == maxSize)
cout << "Cannot insert in a full list." << endl;
else
{
loc = seqSearch(insertItem);
if (loc == -1)
list[length++] = insertItem;
else
cout << "the item to be inserted is already in the list. << endl;
}
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 34


// look for item and remove it
void arrayListType::remove(int removeItem) {

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;
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 35


#include <iostream>
#include "arrayListType.h"
using namespace std;
void testCopyConstructor(arrayListType testList);

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;

C++ Programming: Program Design Including Data Structures, Sixth Edition 36


list.remove(num);
cout << "After removing " << num << " the list is: " << endl;
list.print();
cout << "The list size is: " << list.listSize() << endl;
testCopyConstructor(list);
cout << "The list after the copy constructor." << endl;
list.print();
cout << "The list size is: " << list.listSize() << endl << endl;
cout << "Printing List #2" << endl;
list2.print();

return 0;
}

void testCopyConstructor(arrayListType testList)


{
cout << "Inside the function testCopyConstructor." << endl;
testList.print();
cout << "The list size is: " << testList.listSize() << endl;
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 37


Enter numbers ending with -999
123456789
-999
The list you entered is:
123456789
The list size is: 9
Enter the item to be deleted: 7
After removing 7 the list is:
12345689
The list size is: 8
Inside the function testCopyConstructor.
12345689
The list size is: 8
The list after the copy constructor.
12345689
The list size is: 8

Printing List #2
123456789

C++ Programming: Program Design Including Data Structures, Sixth Edition 38


Inheritance, Pointers, and
Virtual Functions
You can pass an object of a derived class to a
formal parameter of the base class type
Inheritance, Pointers, and
Virtual Functions

C++ Programming: Program Design Including Data Structures, Sixth Edition 40


Inheritance, Pointers, and
Virtual Functions

The breed is missing


Inheritance, Pointers, and
Virtual Functions
For both statements (Lines 7 and 9), member
function print of petType was executed
Because the binding of print in the body
of callPrint, occurred at compile time
Compile-time binding: the necessary code to call a
specific function is generated by the compiler
Also known as static binding or early binding

C++ Programming: Program Design Including Data Structures, Sixth Edition 42


Inheritance, Pointers, and
Virtual Functions
How can we avoid this problem?
Virtual functions (reserved word virtual)
Virtual function: binding occurs at program execution
time, not at compile time
This kind of binding is called run-time binding
Run-time binding: compiler does not generate code
to call a specific function; it generates information to
enable run-time system to generate specific code for
the function call
Also known as dynamic or late binding

C++ Programming: Program Design Including Data Structures, Sixth Edition 43


Inheritance, Pointers, and
Virtual Functions

C++ Programming: Program Design Including Data Structures, Sixth Edition 44


Abstract Classes and
Pure Virtual Functions
Through inheritance we can derive new classes
without designing them from scratch
Derived classes inherit existing members of base class, can
add their own members, and also redefine or override
public and protected member functions
Base class can contain functions that you would want each
derived class to implement
o Base class may contain functions that may not have
meaningful definitions in the base class

C++ Programming: Program Design Including Data Structures, Sixth Edition 45


Abstract Classes and Pure Virtual Functions

To make them pure virtual functions:


Abstract Classes and
Pure Virtual Functions
Abstract class: contains one or more pure virtual
functions
You cannot create objects of an abstract class
Abstract Classes and
Pure Virtual Functions
If we derive rectangle from shape and want to
make it a non-abstract class:
We must provide the definitions of the pure virtual
functions of its base class
Note that an abstract class can contain instance
variables, constructors, and functions that are not
pure virtual
The class must provide the definitions of
constructor/functions that are not pure virtual

C++ Programming: Program Design Including Data Structures, Sixth Edition 48


Templates
Templates: a single code body for a set of related
functions (called function template) and related
classes (called class template)
The syntax for templates is:
template <class Type>
declaration;
where Type is the type of the data and declaration
is either a function declaration or a class
declaration

C++ Programming: Program Design Including Data Structures, Sixth Edition 49


Templates (continued)
template is a reserved word
The word class in the heading refers to any user-
defined type or built-in type
Type is called a formal parameter to the template
Just as variables are parameters to functions
Data types are parameters to templates

C++ Programming: Program Design Including Data Structures, Sixth Edition 50


Function Templates
The syntax of the function template is:
template<class Type>
function definition;
where Type is called a formal parameter of the template
Type
Specifies type of parameters to the function
Specifies return type of the function
Declares variables within the function

C++ Programming: Program Design Including Data Structures, Sixth Edition 51


#include <iostream>
#include <string>
using namespace std;

template <class Type>


Type larger(Type x, Type y); // function template

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 Type> Larger of 5 and 6 = 6


Type larger(Type x, Type y)
Larger of A and B = B
{
if (x >= y) Larger of 5.6 and 3.2 = 5.6
return x; Larger of Hello and Happy = Hello
else
return y;
}
Class Templates

Class templates: a single code segment represents


a set of related classes
Syntax:
template<class Type>
class declaration
Called parameterized types
A specific class is made based on the parameter type

C++ Programming: Program Design Including Data Structures, Sixth Edition 53


Class Templates (continued)

A template instantiation can be created with


either a built-in or user-defined type

The function members of a class template are


considered function templates

C++ Programming: Program Design Including Data Structures, Sixth Edition 54


Header File of a Class Template
Passing parameters to a function takes effect at run
time

Passing a parameter to a class template takes effect


at compile time

C++ Programming: Program Design Including Data Structures, Sixth Edition 55


Header File of a Class Template
(continued)
Cannot compile the implementation file
independently of the user code
Can put class definition and the definitions of the function
templates directly in the user code

Can put class definition and the definitions of the function


templates in the same header file

C++ Programming: Program Design Including Data Structures, Sixth Edition 56


Header File of a Class Template
(continued)
In either case, function definitions and client code
are compiled together
We will put the class definition and the function
definitions in the same header file

C++ Programming: Program Design Including Data Structures, Sixth Edition 57


#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
using namespace std;

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);
}

template <class elemType>


bool arrayListType<elemType>::isItemAtEqual
(int location, elemType& item)
{
return (list[location] == item);
}

C++ Programming: Program Design Including Data Structures, Sixth Edition 59


#include <iostream>
#include <string>
#include "arrayListType.h"
using namespace std;
int main()
{ arrayListType<int> intList(100);
arrayListType<string> stringList;
int counter, number;
cout << "Processing the integer list" << endl;
cout << "Enter 5 integers: ";
for (counter = 0; counter < 5; counter++)
{
cin >> number;
intList.insertAt(counter, number);
}
cout << "The list you entered is: ";
intList.print();
cout << endl;
cout << "Enter the item to be deleted: ";
cin >> number;
intList.remove(number);
cout << "After removing " << number << " the list is:" << endl;
intList.print();
string str;
cout << "Processing the string list" << endl;
cout << "Enter 5 strings: ";
for (counter = 0; counter < 5; counter++)
{ cin >> str;
stringList.insertAt(counter, str);
}
cout << "The list you entered is: " << endl;
stringList.print();
cout << "Enter the string to be deleted: ";
cin >> str;
stringList.remove(str);
cout << "After removing " << str << " the list is:" << endl;
stringList.print();
cout << endl;

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

C++ Programming: Program Design Including Data Structures, Sixth Edition 62