Mutability
Mutability
2023
Pointer Basics and Memory Management
Module Outline
Topic Title Topic Objective
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Section Title
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
01. What is Mutability?
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
01. What is Mutability?
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
01. What is Mutability?
Challenge
• Change the health of
player1 to 0
• Print the status of player1
specifying that their health
is 0 and the message “Game
over..”
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Section Title
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
02. External Functions and Objects
External Functions
• One of the benefits of functions is code reusability
• The previous code has repetitive cout statements which can be
improved by using a function
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
02. External Functions and Objects
External Functions
• In the main function, replace
the strings inside the print
statements with a call to the
PrintPlayer function.
• Pass the player1 object to
the PrintPlayer function.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
02. External Functions and Objects
External Functions
• Using an external function
to print the status of
player1 may not seem like
it was worth the effort to
change the code.
• The benefit of this function
becomes clear when the
code increases its
complexity.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
02. External Functions and Objects
Player()
{
health = 100;
score = 0;
level = 1;
}
};
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
void PrintPlayer(Player p)
{
if (p.health <= 0)
{
cout << "This player is dead. They died on level " << p.level;
cout << "with a score of " << p.score << "." << endl;
}
else
{
cout << "This player has " << p.health << " health, a score of " <<
p.score;
cout << ", and is on level " << p.level << "." << endl;
}
}
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
void ChangeHealth(Player p, int amount)
{
p.health += amount;
cout << "New health = " << p.health << endl;
}
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
int main()
{
return 0;
}
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
02. External Functions and Objects
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Section Title
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
03. Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
03. Class Functions
Class Functions
• a function that is attached to an object
• most common type of function when creating classes
• declared inside of the class
• have access to the class variables
• invoked using dot-notation
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
External Function Example Class Function Example
class Player { class Player {
public: class variables public:
int health; Player() {
int score; within a class health = 100;
int level; must be public score = 0;
level = 1;
Player() { }
health = 100; void ChangeLevel() {
score = 0; level += 1;
level = 1; cout << "Class change: " << level << endl;
} }
}; private: make class variables
int health;
void ChangeLevel(Player p) { int score;
private to prevent
p.level += 1; int level; them from being altered
cout << "External change: " << p.level << endl; };
}
accidentally
Syntax: Player mario;
Player mario; mario.ChangeLevel(); Syntax:
ChangeLevel(mario); Function(Object) Object.Function()
cout << "Object change: " << endl;
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
03. Class Functions
Challenge
• Add cout << “Object change: “ << mario.level << endl;
to line below mario.ChangeLevel(); in main
• Change private: to // private: to comment out the private
access modifier
• Change the code in main to:
Player mario;
ChangeLevel(mario);
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Section Title
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
What are your findings?
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
IMPORTANT
• ChangeLevel as an external function only affected the object
temporarily
• ChangeLevel as a class function was able to affect the object
permanently
• This is why the level of mario was shown as 2 and then 1 in the
external function example
• Yet, level of mario was shown as 2 and 2 in the class function
example
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
Convert to
Class
Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
Convert to
Class
Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential