-1
I’m trying to implement a very simple example of using classes in the C++ language. Well, in short, there are two objects (cliente1 and cliente2), which represent two bank accounts. Within the class Bill, there is a method transfer(), of course responsible for carrying out the transfer between these two accounts.
Well, the problem is that I can’t update the balance amount of the account that receives the transfer.
In the example of the code, the 'Pedro Gonçalves' account should have the final balance of 1000, however, the balance remains unchanged (450), even after the transfer. The object is passed as a parameter in the method transfer.
I tried to change the attribute balance of this account through the Setter that I defined, but does not work.
Someone can point out my mistakes?
#include <iostream>
#include <iomanip>
using namespace std;
class Conta{
private:
string nome, cpf, rg;
float saldo;
public:
void setDados(string a, string b, string c, float s){
nome = a;
cpf = b;
rg = c;
saldo = s;
}
void setSaldo(float s){
saldo = s;
}
string getNome(){
return nome;
}
string getCpf(){
return cpf;
}
string getRg(){
return rg;
}
float getSaldo(){
return saldo;
}
void transferir(Conta dest, float valor){
if(saldo >= valor){
//O saldo da conta atual possui valor de transferência subtraído.
saldo -= valor;
float saldo_destino = dest.getSaldo();
//O saldo da conta destino é incrementado com o valor da transferência.
saldo_destino += valor;
dest.setSaldo(saldo_destino);
cout << "Transferência realizada com sucesso." << endl;
}
else
cout << "Saldo insuficiente para realizar a operação" << endl;
}
};
int main(void){
Conta cliente1, cliente2;
cliente1.setDados("Pedro Gonçalves", "123456", "1", 450.00);
cliente2.setDados("João Santos", "654321", "2", 1550.00);
cliente2.transferir(cliente1, 550);
cout << fixed << setprecision(2);
cout << cliente1.getNome() << " possui saldo de R$" << cliente1.getSaldo() << endl;
cout << cliente2.getNome() << " possui saldo de R$" << cliente2.getSaldo() << endl;
}
There are several problems in this code, even solving this issue will still have a bad code on hand.
– Maniero
It helped a lot. Thank you!
– Daniel Cavalcante