Access and Mutator Practice
Tarefa
Swipe to start coding
- Finish an implementation of a class called Vault that simulates a secure vault for storing a message with a password.
- Implement the get_message() method that should return the stored message if the provided password matches the vault's password otherwise return an empty string or some suitable indication.
- Implement the set_message() method, which takes two parameters, a new message and the current password. This method should allow changing the message only if the current password matches the vault's password.
- Implement the set_password() method that takes two parameters, a new password and the old password. It should change the vault's password to the new one if the old password matches the current password.
Solução
solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
class Vault {
public:
Vault(std::string message, std::string password)
: message(message), password(password) {}
std::string get_message(std::string current_password)
{
return password == current_password ? message: "Denied";
}
void set_message(std::string new_message, std::string current_password)
{
if (password != current_password)
return;
message = new_message;
}
void set_password(std::string new_password, std::string old_password)
{
if (password != old_password)
return;
password = new_password;
}
private:
std::string message;
std::string password;
};
int main()
{
Vault vault("Gold", "c<>definity");
std::cout << vault.get_message("c<>definity") << ' ';
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 3. Capítulo 5
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
class Vault {
public:
Vault(std::string message, std::string password)
: message(message), password(password) {}
___ get_message(___ ___)
{
// Implement
}
void set_message(std::string new_message, std::string current_password)
{
// Implement
}
void set_password(std::string new_password, std::string old_password)
{
if (password != old_password)
return;
password = new_password;
}
private:
std::string message;
std::string password;
};
int main()
{
Vault vault("Gold", "c<>definity");
std::cout << vault.get_message("c<>definity") << ' ';
vault.set_message("Silver", "c<>definity");
vault.set_password("c<>delimited", "c<>definity");
std::cout << vault.get_message("c<>definity") << ' ';
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo