0% found this document useful (0 votes)
2 views37 pages

Mutability

The document outlines a module on mutability and memory management in object-oriented programming, focusing on the concepts of mutable objects, external functions, and class functions. It explains how mutable objects can change their attributes, the challenges of using external functions for object manipulation, and the advantages of class functions for permanent changes. Additionally, it includes examples and challenges for implementing these concepts in programming.

Uploaded by

JB Rafael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Mutability

The document outlines a module on mutability and memory management in object-oriented programming, focusing on the concepts of mutable objects, external functions, and class functions. It explains how mutable objects can change their attributes, the challenges of using external functions for object manipulation, and the advantages of class functions for permanent changes. Additionally, it includes examples and challenges for implementing these concepts in programming.

Uploaded by

JB Rafael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Mutability

QCPP320 - Object Oriented Programming


EcE/CpE Department
College of Science, Engineering & Architecture
Ateneo de Naga University

2023
Pointer Basics and Memory Management

Module Outline
Topic Title Topic Objective

01. What is Mutability? Define the term “mutable”

02. External Functions and


Construct an external function to modify class variables
Objects
03. What is a Class
Define the term “class function”
Function?
04. Defining and Calling
Demonstrate the syntax for defining and calling class functions
Class Functions

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Section Title

01. What is Mutability?

© 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?

Objects are mutable


• means that objects
(specifically their attributes)
can change value
• A newly instatiated Player
object has a health of 100,
and a score of 0, and starts
on level 1
• This object can lose health,
increase their score, and
advance levels

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
01. What is Mutability?

Objects are mutable


• Print out the attributes of
player1. Then change each
attribute and print out the
attributes again

© 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

02. External Functions and Objects

© 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

Problem with External Functions


• Now that the PrintPlayer
can return two different
strings:
• Object has no health
• Object has health
• The problem with using
external functions, is that
the changes made to
objects in one function do
not translate or carry over
to the next function.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
#include <iostream>
using namespace std;
// add class definitions below this
line
class Player
{
public:
int health;
int score;
int level;

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()
{

// add code below this line


Player player1;
PrintPlayer(player1);
player1.health = 0;
player1.score += 25;
player1.level += 1;
PrintPlayer(player1);

ChangeHealth(player1, 20); // changes health of player1


PrintPlayer(player1); // does not register changes from
// ChangeHealth function

return 0;
}

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
02. External Functions and Objects

Problem with External Functions


• ChangeHealth(player1, 20) had no effect on player1’s health
when changed to 20
• Changes that occur within external functions are not permanent

ChangeHealth(player1, 20); // changes health of player1


PrintPlayer(player1); // does not register changes from
// ChangeHealth function

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Section Title

03. Class Functions

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
03. Class Functions

External Functions vs. Class Functions


• a class is a collection of data and the actions that can modify the
data
• the constructor can build the “collection of data”, but nothing in
the class can modify the data, so we use external functions
• using external functions is not a good practice because the
changes are not always permanent
• Class functions (also known as class or instance methods), serve
to modify the data within the objects more permanently

© 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

04. Defining and Calling Class Functions

© 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

More on Class Methods and Objects


• Changes to objects should happen exclusively through class
functions (best practice)
• Code is easier to organize and easier for others to understand
• Say you created a class that keeps track of a meal, where a meal
can be any of the ff: drinks, appetizers, courses, desserts
• Each will become a class variable (vector of strings)
• Remember: class variables/attributes are private

© 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

More on Class Methods and Objects


• Add a class function to add a drink to the Meal object.
• Use the push_back function to add an element to the vector, so
drinks.push_back(drink) adds the drink drink to the vector drinks
• Add a class function PrintDrinks() to print out all of the
elements inside the drinks vector.
• Class functions are public

© 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

More on Class Methods and Objects


• Create a Meal object in main and then test your code with the
following added commands

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions

More on Class Methods and Objects


• Create the AddAppetizer class function for the class.
• Like thew AddDrink function, AddAppetizer accepts a string as a
parameter and adds it as an element to the appetizers
attribute(which is a vector)

© 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

More on Class Methods and Objects


• Add ”bruschetta” as an appetizer to the dinner object
• Then call the class function PrintAppetizers

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
04. Defining and Calling Class Functions

Challenge – create the following


• PrintAppetizers – accepts a string which represents a main
course and adds it to the meal
• PrintMainCourses – prints all of the main courses in the meal
• AddDessert – accepts a string which represents a dessert and
adds it to the meal
• PrintDesserts – prints all of the desserts in the meal
• Test your code using “beef kaldereta” as a main course and
“minatamis na kundol” as dessert.
• Use the Print class functions you created to print out all of the
items of the meal.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Dios Mabalos

[email protected]

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

You might also like