0
#include<iostream>
#include<fstream>
using namespace std;

class Integer {
public:
    int val;
    Integer(int val = 0) {
        this->val = val;
    }
    void setVal(int val) {
        this->val = val;
    }
};

int main()
{
    int val;
    Integer i;
    i.setVal(8);
    cout << val << endl;
}

When I execute my code I got 0.I am new to C++,I do not understand this. Can someone elaborate more regarding this issues?

Holt
  • 36,600
  • 7
  • 92
  • 139
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
  • why not use a search engine for an explanation of this in C++? – hoijui Jul 31 '15 at 07:20
  • C++ isn't java, so you should not just jump in and use getters/setters everywhere. `const` references eliminate almost all nead for getters and setters. Anyway, [they're evil](http://typicalprogrammer.com/doing-it-wrong-getters-and-setters/). – RamblingMad Jul 31 '15 at 07:33

2 Answers2

5

You're outputting the wrong val. As written, you could call i.val since you made the data member val public, but you could also choose to create a function (a "getter") for the Integer class. In any case, I recommend learning about private data members and how they can be used.

The getter approach would look something like

// in the class
int getVal() {
  return val; // equal to return this->val
}

// in main()
cout << i.getVal() << endl;

Note that if you change the main function you're not using your previous val in main() anymore. That's also the point - you're now using the class data member instead!

For a discussion on how one can get around the need to use the this pointer like you asked, check out this question: Is using underscore suffix for members beneficial?

It also seems that you don't need to include fstream at this point.

Community
  • 1
  • 1
Victor Sand
  • 2,270
  • 1
  • 14
  • 32
  • Ok,works fine with i.val .Why should I introduce function? – Richard Rublev Jul 31 '15 at 07:12
  • @RichardRublev It generally improves encapsulation, which is a way of saying that you as a programmer will be in more control of your data. If you don't want users of your function to be able to change your variable without using the "setVal" function for example, the variable needs to be private. Read more here: http://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm – Victor Sand Jul 31 '15 at 07:14
  • 1
    @RichardRublev IMHO you do not need to introduce function , the "getters" and "setters" are cargo cult junk – M.M Jul 31 '15 at 07:37
  • 1
    @MattMcNabb I kind of agree, but I think it's still worthwhile to learn about the concepts given that OP is new to C++. – Victor Sand Jul 31 '15 at 07:39
  • Yes but without the "you should do this, you should do that" added on top. – Lightness Races in Orbit Jul 31 '15 at 09:47
1

You invented a relationship between the val in main and the val inside the object i.

They have no relationship other than sharing a name.

  • remove the unset val in main;
  • output i.val instead.
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055