Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I write classes? C++
    text
    copied!<p>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:</p> <pre><code>#include &lt;iostream&gt; 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 &lt;&lt; "test1: " &lt;&lt; atest.tester() &lt;&lt; endl; cout &lt;&lt; "test2: " &lt;&lt; atest2.tester() &lt;&lt; endl; return 0; } </code></pre> <p>or like this:</p> <pre><code>#include &lt;iostream&gt; 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 &lt;&lt; "test1: " &lt;&lt; tester.testfunc() &lt;&lt; endl; return 0; } </code></pre> <p>instead of simply like this:</p> <pre><code>#include &lt;iostream&gt; using namespace std; class test { public: int tester (int x, int y) { return x + y; } }; int main() { test atest; cout &lt;&lt; atest.tester(3, 2) &lt;&lt; endl; return 0; } </code></pre> <p>Honestly, I just don't get it!</p> <p>Why do I need private members??</p> <p>When and how should I use destructors?</p> <p>How should I generally write my classes?</p> <p>I'm very confused here and I really need somebody to clear up things for me... thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload