Search code examples
c++oopvariablesgetter-settersetter

C++ Setter doesn't change the object's variable value


I'm trying to run a code that its's meant to change the value of object variables after it creates the object, and it isn't changing, then the variable is returning values like -815470397 and not changing. And when I use breakpoints it's like it jumps the inside part of the setter.

Pessoa.h

#pragma once
using namespace std;
class Pessoa
{

protected:
    int dia;
    int mes;
    int ano;
    int idade;

public:
    Pessoa();
    Pessoa(int, int, int, int);

    int GetDia();
    void SetDia(int);
    int GetMes();
    void SetMes(int);
    int GetAno();
    void SetAno(int);
    int GetIdade();
    void SetIdade(int);

};

Pessoa.cpp

#include "Pessoa.h"

Pessoa::Pessoa() {

}

Pessoa::Pessoa(int dia, int mes, int ano, int idade) {

    this->dia = dia;
    this->mes = mes;
    this->ano = ano;
    this->idade = idade;
}

int Pessoa::GetDia() {
    return this->dia;
}

int Pessoa::GetMes() {
    return this->mes;
}

int Pessoa::GetAno() {
    return this->ano;
}

int Pessoa::GetIdade() {
    return this->idade;
}

void Pessoa::SetDia(int dia) {
    this->dia == dia;
}

void Pessoa::SetMes(int mes) {
    this->mes == mes;
}

void Pessoa::SetAno(int ano) {
    this->ano == ano;
}

void Pessoa::SetIdade(int idade) {
    this->idade == idade;
}

Exame_especial.cpp

#include <iostream>
#include <string>
#include "Pessoa.h"
using namespace std;

string ImprimeIdade(Pessoa*);
int Calc_Idade(Pessoa*, int, int, int);

int main()
{
    Pessoa* Einstein = new Pessoa();
    Pessoa* Newton = new Pessoa();

    Einstein->SetDia(14);
    Einstein->SetMes(3);
    Einstein->SetAno(1879);
    Newton->SetDia(4);
    Newton->SetMes(1);
    Newton->SetAno(1643);

    cout << ImprimeIdade(Newton) << endl;
    cout << ImprimeIdade(Einstein) << endl;

}

string ImprimeIdade(Pessoa* nome) {
    nome->SetIdade(Calc_Idade(nome, 29, 6, 2021));
    return "A idade de Einstein seria " + to_string(nome->GetIdade()) + "\n";
}


int Calc_Idade(Pessoa* nome, int dia, int mes, int ano) {
    int idade = ano - nome->GetAno();
    if (nome->GetMes() > mes) {
        idade = idade - 1;
    }
    else {
        if (nome->GetMes() == mes) {
            if (nome->GetDia() > dia) {
                idade = idade - 1;
            }
        }
    }
    return idade;
}

Solution

  • You get these strange values due to the fact that your data members are of built-in type and they remain uninitialized when you use them. They remain uninitialized because your setters aren't setting anything. What they actually do is a comparison (the ==).

    Pay attention to the difference in these two:

    void Pessoa::SetDia(int dia) {
        this->dia == dia; // Equal? Returns bool value which is lost 
    }
    

    Vs

    void Pessoa::SetDia(int new_dia) {
        this->dia = new_dia; // Assigns a new value
    }
    

    This goes for all setters in your code.

    Also, note that inside the body of a member function, you can refer to the data members directly, without the need to dereference them via this:

    void Pessoa::SetDia(int new_dia) {
        // Assigns new value to the dia data member of this
        dia = new_dia;
    }