Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New entry mutable variables #5555

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

braanj
Copy link
Contributor

@braanj braanj commented Oct 25, 2024

Description

Add new concept entry term for cpp mutable variables.

Issue Solved

Closes #5473

Type of Change

  • Adding a new entry

Checklist

  • All writings are my own.
  • My entry follows the Codecademy Docs style guide.
  • My changes generate no new warnings.
  • I have performed a self-review of my own writing and code.
  • I have checked my entry and corrected any misspellings.
  • I have made corresponding changes to the documentation if needed.
  • I have confirmed my changes are not being pushed from my forked main branch.
  • I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • I have linked any issues that are relevant to this PR in the Issues Solved section.

@braanj
Copy link
Contributor Author

braanj commented Oct 25, 2024

Hey @codecademy-docs please review

@Sriparno08 Sriparno08 self-assigned this Oct 26, 2024
@Sriparno08 Sriparno08 added c++ C++ entries new entry New entry or entries labels Oct 26, 2024
@braanj
Copy link
Contributor Author

braanj commented Oct 28, 2024

Hey @Sriparno08 please review

@Sriparno08
Copy link
Collaborator

Hey @braanj, I'll review this by the end of the week.

Copy link
Collaborator

@Sriparno08 Sriparno08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @braanj, thank you for contributing to Codecademy Docs!

I've listed all the modifications that you need to make in your entry below:

Comment on lines +1 to +12
---
Title: 'Mutable variables'
Description: 'In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Variables'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifications:

  • Let's write the title in title case
  • Let's change the description a bit
  • Let's add some more tags
Suggested change
---
Title: 'Mutable variables'
Description: 'In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Variables'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---
---
Title: 'Mutable Variables'
Description: 'Mutable variables are variables that can be modified even within constant functions.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Functions'
- 'Values'
- 'Variable Types'
- 'Variables'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

- 'paths/computer-science'
---

In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make some minor changes:

Suggested change
In C++, the `mutable` keyword in C++ allows certain class variables to be modified even within const functions, useful for managing internal state flexibly.
In C++, mutable variables are variables that can be modified even within constant functions, useful for managing internal state flexibly.


## Syntax

To declare a variable as `mutable`, simply place the `mutable` keyword before the variable type:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make some minor changes:

Suggested change
To declare a variable as `mutable`, simply place the `mutable` keyword before the variable type:
To declare a variable as mutable, the `mutable` keyword needs to be placed before the variable type:

Comment on lines +24 to +25
- **type** - The type of the variable (e.g., `int`, `char`).
- **name** - The name of the variable.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make some minor changes:

Suggested change
- **type** - The type of the variable (e.g., `int`, `char`).
- **name** - The name of the variable.
- `type`: The type of the variable (e.g., `int`, `char`).
- `name`: The name of the variable.


## Example

In the example below, `accessCount` is marked `mutable`, allowing it to be modified within the const `displayData` function:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make some minor changes:

Suggested change
In the example below, `accessCount` is marked `mutable`, allowing it to be modified within the const `displayData` function:
In the example below, `accessCount` is marked `mutable`, allowing it to be modified within the constant `displayData()` function:

Comment on lines +31 to +55
```cpp
#include <iostream>
#include <string>

class Data {
public:
Data(std::string value) : data(value), accessCount(0) {}

void displayData() const {
++accessCount; // Allowed modification due to `mutable`
std::cout << "Data: " << data << ", Access count: " << accessCount << std::endl;
}

private:
std::string data;
mutable int accessCount; // Can be modified in const methods
};

int main() {
Data d("Sample");
d.displayData();
d.displayData();
return 0;
}
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the indentation to 2 spaces and make some minor changes:

Suggested change
```cpp
#include <iostream>
#include <string>
class Data {
public:
Data(std::string value) : data(value), accessCount(0) {}
void displayData() const {
++accessCount; // Allowed modification due to `mutable`
std::cout << "Data: " << data << ", Access count: " << accessCount << std::endl;
}
private:
std::string data;
mutable int accessCount; // Can be modified in const methods
};
int main() {
Data d("Sample");
d.displayData();
d.displayData();
return 0;
}
```
```cpp
#include <iostream>
#include <string>
class Data {
public:
Data(std::string value) : data(value), accessCount(0) {}
void displayData() const {
++accessCount; // Modification allowed due to 'mutable'
std::cout << "Data: " << data << ", Access count: " << accessCount << std::endl;
}
private:
std::string data;
mutable int accessCount; // Can be modified in constant methods
};
int main() {
Data d("Sample");
d.displayData();
d.displayData();
return 0;
}
```

Comment on lines +57 to +60
```shell
Data: Sample, Access count: 1
Data: Sample, Access count: 2
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a sentence to introduce the output:

Suggested change
```shell
Data: Sample, Access count: 1
Data: Sample, Access count: 2
```
The above code produces the following output:
```shell
Data: Sample, Access count: 1
Data: Sample, Access count: 2
```

Data: Sample, Access count: 2
```

Here, even though `displayData` is a const member function, `accessCount` can be incremented due to its `mutable` declaration.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make some minor changes:

Suggested change
Here, even though `displayData` is a const member function, `accessCount` can be incremented due to its `mutable` declaration.
Here, even though `displayData()` is a constant member function, `accessCount` can be incremented due to its `mutable` declaration.


## Codebyte Example

Use this example to experiment with mutable variables in C++. Enjoy coding!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the sentence:

Suggested change
Use this example to experiment with mutable variables in C++. Enjoy coding!
The following codebyte example demonstrates the usage of mutable variables:

Comment on lines +68 to +94
```codebyte/cpp
#include <iostream>

class Counter {
public:
Counter() : count(0) {}

void increment() const {
++count; // count is mutable, so this modification is allowed in const method
}

int getCount() const {
return count;
}

private:
mutable int count;
};

int main() {
Counter counter;
counter.increment();
counter.increment();
std::cout << "Count: " << counter.getCount() << std::endl;
return 0;
}
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the indentation to 2 spaces and make some minor changes:

Suggested change
```codebyte/cpp
#include <iostream>
class Counter {
public:
Counter() : count(0) {}
void increment() const {
++count; // count is mutable, so this modification is allowed in const method
}
int getCount() const {
return count;
}
private:
mutable int count;
};
int main() {
Counter counter;
counter.increment();
counter.increment();
std::cout << "Count: " << counter.getCount() << std::endl;
return 0;
}
```
```codebyte/cpp
#include <iostream>
class Counter {
public:
Counter() : count(0) {}
void increment() const {
++count; // 'count' is mutable, so this modification is allowed in this constant method
}
int getCount() const {
return count;
}
private:
mutable int count;
};
int main() {
Counter counter;
counter.increment();
counter.increment();
std::cout << "Count: " << counter.getCount() << std::endl;
return 0;
}
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Term Entry] C++ Variables Mutable Variables
2 participants