Lecture 2 - COMP315 – Advanced Programming

Lecture 3
•Classes,
Structs, Enums
•Passing by reference and
value
•Arrays
Class



Scope is by default private
Requires a header file (.h)
Constructor
 Called
when instantiating the class
 A default constructor is always provided

Destructor
 Called
when the removing allocated memory for a
class (Only when instantiating dynamic objects)
Constructor and Destructor
Header (.h)
Source file (.cpp)
Instantiation
Assignment
What happens?
 Is c the same as c1?
 What is being called?

Copy constructor


There’s a default copy constructor!
: after a class constructor is an initialization list
Copy constructor
Called by passing in an instantiated object
 Called by the operator =

//Calls copy constructor
//Calls copy constructor
Struct
C data type
 Public scope
 Otherwise it’s the same as a class

Enums
Useful for switch statements
 Default values -> 0, 1, 2, 3…

Gears are set to 0


Whenever we call a function e.g. y = sqrt(x);
 A stack frame is created that holds
 The return memory address (where to go when the
function ends)
 The parameters (if any)
 Local variables (if any)
 Return data type and value (if any)
 The stack frame is then placed on the program stack
 The function code is executed
 When the function ends the stack frame is removed
from the program stack
 Any return value is retrieved
 The program counter is loaded with the return
memory address
 The stack frame is destroyed
Everything in red can be ‘removed’ using inline functions
Function call versus Inline functions
When the compiler finds a function
call it replaces it with a compiled
version of the function
implementation code.
Our compiled code
Standard
Functions
Inline
Functions
Function implementation code
(compiled)
Function overhead code (compiled)
Function call
Value

If I want the value of a memory address
*
operator
Is a dereferencing operator
 E.g. int x = 10
 If I cout << *&x

It will print 10
 **& will error as there is no value of the value
 &*& will return the memory address of the variable
 *&*& will return 10

Reference

& – Address of operator
 Gets
the memory address of a variable
 E.g. int x = 10;
 10 is the value
 If I cout << &x

Something like: 0020F9D0
Pick ‘n pay (PnP) is a
value
 59 is it’s address
 &PnP returns 59

Passing by value
Passing by reference

Pass using const when the value doesn’t
need to be changed

Advantages
 No
copy is made
 Function can modify value

Disadvantages
 Difficult
to tell if passing to a function is giving
you input or output
 Dereferencing a Pointer to a variable is slow
Pass by value
Inside function
2
1
Exit function
SPAR
Pass by reference
Now I can change pick n pay
 Without recreating PnP

Function
SPAR
Passing by address

Function takes a pointer
What happens here?
Arrays
Arrays can be defined, which allows a
number of consecutive variables / objects
to be created.
 Arrays are particularly useful with forloops.
 Static arrays must be declared with a
maximum size.
 Run-time errors can occur if attempting to
access outside of an array’s index.

Example
Accessing an array of objects