You are on page 1of 6

Default arguments values

In C++ we can tell the compiler to use default values if the arguments are not supplied.
Indicated in the function prototype

double calc_cost(double base_cost, double tax_rate=0.06);

We can avoid supplying the same arguments all the time

107.Overloading functions(Polymorphism)
We have functions that have different parameter lists that have the same name.

No need to keep track of different function names.

Return type is obviously not considered.

Using the same name for various forms of that same functions

108. Passing Arrays to functions


int numbers [];
What is passed to the function is only the address of the first element of the array. The
function does not know how many elements are in the array, so we need to pass the size of
the array!

void print_array(int numbers[], size_t size );


Since we are passing the location of the array, the function can modify the array!

The value of the name of an array is the location of the first element of the array.

The array was never copied! instead the address of the array was passed in.
If you have access to the actual and don't want to change it, const is the way to go!

Pass an array of std::string and size_t value:


string print_guests(const string[], size_t);

109.Pass by reference
In order to be able to change the actual character parameter from within the function
body. In order to do this we need the actual address of the parameter.

Passing by value we make a complete copy of the object in the function.

110. Scope rules


Local or block scope
with every new block there is a new local scope
111. How Function Calls work?
Stack size is finite- Stack overflow
Main is a function and is on the stack already. Main stops what is doing and pushes
an activation record for function1. We need to allocate the parameters in function1.
Function1 completes its block and calls function2. Funtion1 stops and allocates
space for variables of function2. When Function 2 is done it gets popped-out the
stack and we are now in function1. When function1 is done, its stack is popped out
and we are back in Main.

112. Inline Functions


Inline code avoids the function call overhead. Inline code is generally faster.

113. Recursive functions


Recursion is a function that calls itself: either directly or indirectly through another
function.

Base case and recursive case.


The base case stops the recursion
Recursion is a form of iteration

Section 12. Pointers and references

116. Java and Python do not have pointers.


raw pointers and smart pointers.

117. What is a Pointer


Pointer is a variable! It stores the address of another
variable or function.

The heap ~ free store.

Complex data that is defined outside a function: pass the data by value or using
pointers.

118. Declaring Pointers.


Read from right to left.
int *int_ptr;

Uninitialized pointer points anywhere.


Always initialize pointers so do not point anywhere.
int *int_ptr{nullptr};

119.Accessing the pointer address


& – the address operator
int *p;
Don't confuse the size of a pointer and the size of the it
points to.
In C++

120.Dereferencing a Pointer
Process to access the data that a pointer is pointing to.
Dereferences operator *.

Dereference the pointer and it gives us what it is pointing to.


121.Dynamic memory allocation

Allocate memory dynamically from the heap.


We can use pointers to access newly allocated heap storage.

using new to allocate storage


int *int_ptr {nullptr};
int_ptr = new int; //allocate an integer on the heap.

The allocated storage doesn't have a name, the only way to get to that storage is via
the pointer.

Using delete to deallocate storage. Delete keyword followed by the name of the
pointer.

Using new[] to allocate storage for an array

int *array_ptr {nullptr};


int size{};
array_ptr = new int[size];

delete[] array_ptr; //deallocate the heap

122.Relations between arrays and pointers


The value of an array name is the address of the first element of the array and the
value of a pointer is an address
Remark: C++ does not have true arrays. The arrays are just the address of the first
element in a chunk of memory.
123. Pointer Arithmetic

124.Const and Pointers

Pointers to constants
The data pointed to by the pointers is constant and cannot be changed. However
the pointer itself can change and point somewhere else.

const int *score_ptr = &high_score;

Constant pointers
The data pointed to by the pointers can be changed. However the pointer itself
cannot be changed and point somewhere else.

int *const score_ptr = &high_score;

Constant pointers to constant


The data pointed to by the pointer is constant and cannot be changed. The pointer
itself cannot change and point somewhere else.
cons int *const score_ptr = &high_score;

125. Passing pointers to a function


Pass-by reference with pointers parameters

void double_data(int *int_ptr);

Since the parameter of the function is a pointer to an integer, that means is expects
an address of an integer.

int main(){

int value;
double_data(&value);

126. Returning a Pointer from a Function.

type *function();

int *new_storage{nullptr};
new_storage = new int[size];
We can return a pointer from a function if we want to get the address of and structure
allocated on the heap. Doing so, we don't lose the key to the elements on the heap

127.Potential Pointer Pitfalls

Uninitialized pointers contain garbage, it can point into an important area in memory
and we can whip it out.

Forgetting to release allocated memory with delete

128.What is a reference?
It is an alias for a variable.
Cannot be null

for(auto const &str:stooges)


cout<<str<<endl; // Larry, Moe, Curly

You might also like