Documente Academic
Documente Profesional
Documente Cultură
Virtual Destructor:
A problem can occur when using polymorphism to process
dynamically allocated objects of a class hierarchy.
If we try to destroy a derived class object with a non-virtual
destructor by applying the delete operator to a base class
pointer to the object, only the base class destructor is called,
the destructor of the derived class is not called.
A simple solution to this is to create a virtual destructor
in the base class.
Now if an object in the hierarchy is destroyed explicitly by
applying the delete operator to a base-class pointer, the
destructor of the appropriate class is called.
class Base
// virtual destructor
{ public:
virtual ~Base()
{cout << Base class destroyed << endl;}
};
Run:
Derived class destroyed
Base class destroyed
class Base
// ambiguous reference to base class
{ protected:
int basedata;
public: Base(): basedata(0)
{
}
};
class Derv1: public Base
{ };
class Derv2: public Base
{ };
class Dervnew: public Derv1, public Derv2
{ public:
void getdata()
{cout << "basedata = " << basedata << endl;}
//Error: reference to basedata is ambiguous
};
int main()
{ Dervnew* nptr;
nptr = new Dervnew;
nptr -> getdata();
}
class Base
// virtual base classes
{ protected:
int basedata;
public:
Base(): basedata(0)
{
}
};
class Derv1: virtual public Base
{ };
class Derv2: virtual public Base
{ };
class Dervnew: public Derv1, public Derv2
{ public:
void getdata()
{cout << "basedata = " << basedata << endl;}
};
int main()
{ Dervnew* nptr;
nptr = new Dervnew;
nptr -> getdata();
}
Run:
basedata = 0
Linked Lists
A data structure is a particular way of storing and
organizing data in a computer so that it can be used
efficiently.
A data structure in which data items are arranged in a
linear order is called a linear data structure or a linear
list (or a list).
Examples of linear data structures are:
1. Arrays: statically allocated or dynamically allocated.
2. Linked Lists: dynamically allocated.
Linked lists represent dynamic data-structures that grow
and shrink during execution.
Self-Referential Classes:
15
10
Data
Members
pointer
A
Head
Cat
Fat
Hat
Bat
Cat
Fat
Hat
Gat
Bat
Cat
Fat
Gat
Hat
Defining a Node:
A node can be defined as follows:
class Node
{private:
int data;
// data item
Node *nextPtr; // link to next node
public:
...
};
class SLL;
// forward declaration
class Node
{private:
int data;
// data item
Node *nextPtr; // link to next node
public:
friend class SLL;
...
};
class SLL
{private:
Node *head;
//pointer to the first node
public:
//operations for manipulating the linked list
...
};
newNode3 -> next = NULL; //three nodes with next pointers NULL
head=newNode1;
newNode1->next=newNode2;
newNode2->next=newNode2;
}
void SLL :: displayList ()
{ Node *nodePtr;
nodePtr = head;
while(nodePtr != NULL)
{cout<<nodePtr -> value<<endl;
nodePtr = nodePtr -> next;}
}
int main()
{
SLL list;
list.appendNode ();
cout<<"List is being displayed."<<endl;
list.displayList();
}
Run:
List is being displayed.
22
33
44