|
class ListElem
{
public:
int value; // Integer
ListElem *next; // Pointer used to make link
};
void InsertAtHead(ListElem * & h, ListElem * elem)
{
elem->next = h; // Make new list element points
// to the start of "old list"
h = elem; // Make "h" points to
// the **new** starting object
}
void DeleteAtHead(ListElem * & h)
{
ListElem *p2;
if (h == NULL ) // If list is empty, return(NULL)
return;
p2 = h; // Delete p2
h = p2->next; // Unlink AND return new head
delete p2; // Return memory !!
}
|
|
|
void InsertAtHead(ListElem * & h, ListElem * elem)
{
elem->next = h;
h = elem;
}
void DeleteAtHead(ListElem * & h)
{
ListElem *p2;
if (h == NULL )
return;
p2 = h;
h = p2->next;
delete p2;
}
|
|
| The InsertAtHead() and DeleteAtHead() functions always operate on a list |
class List
{
public:
ListElem *head;
void InsertAtHead(List *x, ListElem * elem)
{
elem->next = x->head;
x->head = elem;
}
void DeleteAtHead(List *x)
{
ListElem *p2;
if (x->head == NULL )
return;
p2 = x->head;
x->head = p2->next;
delete p2;
}
}
|
|
|
(An implicit parameter that is always passed of a function.
We will soon how such a parameter is passed)
Result:
class List
{
public:
ListElem *head;
void InsertAtHead(ListElem * elem)
{
elem->next = x->head;
x->head = elem;
}
void DeleteAtHead()
{
ListElem *p2;
if (x->head == NULL )
return;
p2 = x->head;
x->head = p2->next;
delete p2;
}
}
|
A special keyword is designated to refer to the implicit parameter
|
class List
{
public:
ListElem *head;
void InsertAtHead(ListElem * elem)
{
elem->next = this->head;
this->head = elem;
}
void DeleteAtHead()
{
ListElem *p2;
if (this->head == NULL )
return;
p2 = this->head;
this->head = p2->next;
delete p2;
}
}
|
List myList; p = new ListElem; p->value = 1500; myList.InsertAtHead(p); myList.DeleteAtHead(p); Print(myList.head); |
The implicit parameter is passed in as an variable reference before the function call.
class List
{
public:
ListElem *head;
void InsertAtHead(ListElem * elem)
{
elem->next = x->head;
x->head = elem;
}
void DeleteAtHead()
{
ListElem *p2;
if (x->head == NULL )
return;
p2 = x->head;
x->head = p2->next;
delete p2;
}
|
List myList; myList.Print(); |
Again, the implicit parameter is passed in as an variable reference before the function call.
class List
{
public:
ListElem *head;
void InsertAtHead(ListElem * elem)
{
elem->next = this->head;
this->head = elem;
}
void DeleteAtHead()
{
ListElem *p2;
if (this->head == NULL )
return;
p2 = this->head;
this->head = p2->next;
delete p2;
}
}
|
class List
{
public:
ListElem *head;
void InsertAtHead(ListElem * elem)
{
elem->next = head;
head = elem;
}
void DeleteAtHead()
{
ListElem *p2;
if (head == NULL )
return;
p2 = head;
head = p2->next;
delete p2;
}
}
|
|
int x = 99; global variable
class myClass
{
public:
double x; class variable
void f1()
{
double x; local variable
x = 1111.0;
// x refers to the local variable
}
void f2()
{
double x;
this->x = 2222.0;
// this->x refers to the class variable
}
void f3()
{
x = 3333.0;
// x refers to the class variable
}
void f4()
{
::x = 4444.0;
// ::x refers to the global variable
}
};
int main(int argc, char *argv[])
{
myClass a;
a.x = 9999;
a.f1();
cout << "After a.f1(): x = " << x << ", a.x = " << a.x << endl << endl;
a.f2();
cout << "After a.f2(): x = " << x << ", a.x = " << a.x << endl << endl;
a.f3();
cout << "After a.f3(): x = " << x << ", a.x = " << a.x << endl << endl;
a.f4();
cout << "After a.f4(): x = " << x << ", a.x = " << a.x << endl << endl;
}
|