Lab Manual 06
Lab Manual 06
DEFAULT CONSTRUCTOR
CONSTRUCTOR OVERLOADING
When two or more functions share the same name, the function name is said to be overloaded. Any
class member function may be overloaded, including the constructor. One constructor might take an
integer argument, for example, while another constructor takes a double.
EXPLANATION: The value of the pointer variable ptr is a memory address. A data item whose
address is stored in this variable must be of the specified type.
The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the
address of the first element that it points to.
p = numbers;
When declaring pointers we can explicitly specify which variable we want them to point to.
int number;
int *tommy;
tommy = &number;
We can also initialize the content at which the pointer points with constants at the same moment the
pointer is declared:
Assuming that "fast" is stored at the memory locations that start at addresses 1702, we can represent the
previous declaration as:
Only addition and subtraction operations are allowed with pointers. Furthermore, when incrementing a
pointer, the size in bytes of the type pointed to is added to the pointer.
C++ allows the use of pointers that point to pointers, that these, in its turn, point to data (or even to other
pointers). In order to do that, we only need to add an asterisk (*) for each level of reference in their
declarations:
EXAMPLE: char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;
This, supposing the randomly chosen memory locations for each variable of 7230, 8092 and 10502, could
be represented as:
Void pointers are pointers that can point to any data type. One limitation of void pointers is that they
cannot be directly de-referenced.
EXAMPLE:
NULL POINTER
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not
pointing to any valid reference or memory address.
EXAMPLE: int * p;
p = 0; // p has a null pointer value
Do not confuse null pointers with void pointers. A null pointer is a value that any pointer may take to
represent that it is pointing to "nowhere", while a void pointer is a special type of pointer that can point to
somewhere without a specific type.
C++ allows operations with pointers to functions. The typical use of this is for passing a function as an
argument to another function, since these cannot be passed de-referenced.
EXAMPLE:
In order to request dynamic memory we use the operator new, which is followed by a data type
specifier and optional brackets [ ] (if a sequence of more than one element is required).
In this example, the system dynamically assigns space for five elements of type int and returns a pointer
to the first element of the sequence, which is assigned to bobby. Therefore, now, bobby points to a valid
block of memory with space for five elements of type int.
The first element pointed by bobby can be accessed either with the expression bobby[0] or the
expression *bobby. Both are equivalent as has been explained in the section about pointers. The second
element can be accessed either with bobby[1] or *(bobby+1) and so on.