Download presentation
Presentation is loading. Please wait.
Published byGeraldine Bryan Modified over 9 years ago
1
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays
2
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)
3
Constructor and Destructor Header (.h) Source file (.cpp)
4
Instantiation
6
Assignment What happens? Is c the same as c1? What is being called?
7
Copy constructor There’s a default copy constructor! : after a class constructor is an initialization list
8
Copy constructor Called by passing in an instantiated object Called by the operator = //Calls copy constructor
9
Struct C data type Public scope Otherwise it’s the same as a class
11
Enums Useful for switch statements Default values -> 0, 1, 2, 3…
12
Gears are set to 0
13
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
14
Function call versus Inline functions Our compiled code Function implementation code (compiled) Function overhead code (compiled) Function call Standard Functions Inline Functions When the compiler finds a function call it replaces it with a compiled version of the function implementation code.
15
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
16
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
17
Pick ‘n pay (PnP) is a value 59 is it’s address &PnP returns 59
18
Passing by value
19
Passing by reference
20
Pass using const when the value doesn’t need to be changed
21
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
22
Pass by value 1 2 Inside function SPAR Exit function
23
Pass by reference Now I can change pick n pay Without recreating PnP Function SPAR
24
Passing by address Function takes a pointer
25
What happens here?
26
Arrays Arrays can be defined, which allows a number of consecutive variables / objects to be created. Arrays are particularly useful with for- loops. Static arrays must be declared with a maximum size. Run-time errors can occur if attempting to access outside of an array’s index.
27
Example
28
Accessing an array of objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.