Académique Documents
Professionnel Documents
Culture Documents
This lecture prepared by the instructors at the University of Manitoba in Canada and has been modified by Dr. Ahmad Reza Hadaegh
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 1
list1, list2, and list3 are three instances of the LL class. list1, list2, and list3 are also called objects.
National University
Page
class LL { datum private: NodePtr top; void destroy(NodePtr&); This class definition public: has 5 method prototypes method LL(); and one datum. ~LL(); void buildLL(int); void printLL(); };
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 5
- Public: - Accessible by the world (scoping rules still apply) - Private: - Accessible only by member functions
National University
Page
- The only way hidden data and methods can be accessed is indirectly via public methods
- Methods and data are bound together - Encapsulation
National University
Page
National University
Page 10
constructor destructor
National University
Page 11
LL::LL() {
// Linked list constructor
top = NULL; }
National University
Page 12
LL::~LL() {
// Linked list destructor -- will call a private // member function destroy that will perform // a deep deletion of the linked list object
destroy (top) ; } This destructor will automatically be invoked when an instance of LL leaves scope
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 13
Very much like a regular procedural function -- does not operate directly on data components of the object
destroy(top); //Destroy self before building linked list. for (i=size;i>0;i--) { newNode = new (Node); newNode->next = top; newNode->number = i*2; top = newNode; } }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 15
NodePtr curr; curr = top; while (curr != NULL) { cout << curr->number << endl; curr = curr->next; } }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 16
list1.buildLL(10); list2.buildLL(20); list3.buildLL(50) list1.buildLL(40); list1.printLL(); list2.printLL(); list3.printLL(); } Destructor called 3 times here
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 19
Constructor called -- top pointer set to NULL Build a linked list of size 4 Shallow copy of list1 made to list2
Creating an alias is useful, but not likely the result you were looking for here -- really want a copy of the entire linked list
Creating an alias is useful, but not likely the result you were looking for here -- really want a copy of the entire linked list
2 4
National University
4
6
6 8
Page 23
National University
Page 25
Copy Constructor
- Implicit deep copying can be performed by creating a copy constructor - Copy constructor is a function that will automatically be called when a deep copy is likely required - As in the previous three cases
So...
National University
Page 26
Copy Constructor
class LL { private: NodePtr top; void copy(NodePtr, NodePtr&); // Deep copy of 1st to 2nd void destroy(NodePtr&);
public: LL(); ~LL(); LL(const LL&); // Copy constructor void buildLL(int); void printLL(); };
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 27
void LL::copy(NodePtr atop, NodePtr& btop) { // Performs a deep copy from linked list pointed to // by atop to linked list pointed to be btop. NodePtr acurr, bcurr; destroy (btop); // Free any storage used by target linked list if (atop != NULL) { btop = new (node); btop->number = atop->number; acurr = atop; bcurr = btop; while (acurr->next != NULL) { bcurr->next = new (node); acurr = acurr->next; bcurr = bcurr->next; bcurr->number = acurr->number; } bcurr->next = NULL; }
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 28
Copy Constructor
National University
Page 29