How to change the attribute of an object received per parameter in a method?

Asked

Viewed 91 times

-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;
}   
  • 1

    There are several problems in this code, even solving this issue will still have a bad code on hand.

  • It helped a lot. Thank you!

1 answer

1


What you want is to pass the object to the method by reference, this is passing the address in memory to the object, instead of passing it by value as written in your code, where the method copies the value of the object in another object and the changes in the data in the new object created by the method will have no effect on the object that was passed in the function call.

To solve this you can use pointers.

Your rewritten method would look like this:

        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;
        }

And in the call to the function, given that the cliente1 object is not a pointer, you would pass the address in his memory, thus:

cliente2.transferir(&cliente1, 550);
  • That’s exactly what I needed, Raphael! I had actually done this with the use of pointers, but I received some problems at the time of compilation. They were solved. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.