Hey.. I don't really get them. I read a tutorial about classes in C++, and I don't get a few things: In every example and tutorial that I've seen, functions are never written inside the class! For example, why write a class like this:
#include <iostream>
using namespace std;
class test
{
private:
int x, y;
public:
test (int, int);
int tester () {return x + y; }
};
test::test (int a, int b)
{
x = a;
y = b;
}
int main()
{
test atest (3, 2);
test atest2 (2, 6);
cout << "test1: " << atest.tester() << endl;
cout << "test2: " << atest2.tester() << endl;
return 0;
}
or like this:
#include <iostream>
using namespace std;
class test
{
private:
int x, y;
public:
void set_values (int,int);
int testfunc () {return x + y; }
};
void test::set_values (int a, int b)
{
x = a;
y = b;
}
int main()
{
test tester;
tester.set_values (3, 2);
cout << "test1: " << tester.testfunc() << endl;
return 0;
}
instead of simply like this:
#include <iostream>
using namespace std;
class test
{
public:
int tester (int x, int y) { return x + y; }
};
int main()
{
test atest;
cout << atest.tester(3, 2) << endl;
return 0;
}
Honestly, I just don't get it!
Why do I need private members??
When and how should I use destructors?
How should I generally write my classes?
I'm very confused here and I really need somebody to clear up things for me... thanks