oop - How should I write classes? C++ -


hey.. don't them. read tutorial classes in c++, , don't few things: in every example , tutorial i've seen, functions never written inside class! example, why write class 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 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 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, don't it!

why need private members??

when , how should use destructors?

how should write classes?

i'm confused here , need clear things me... thanks

the example plausibly separates out setting , retrieving of values because there's more in-between see in given main function. if there wasn't, you're right should combine them, , eliminate class in favor of free function.

for example, code like:

void example(test obj) {   if (something) {     process(obj.tester());   } }  int main() {   int x, y;  // imagine these assigned user input.   test obj (x, y);   example(obj);  // obj.tester may or may not used.  if x + y not   // separated, couldn't "maybe use" it.   return 0; } 

why need private members?

you don't. they're more documentation else. accessibility (public/protected/private) documentation checked compiler, however. check helpful in order encapsulate values, encapsulation much, more marking non-public. example, if (from public method) return reference non-public data member, you've tied implementation of member class' public interface.

when , how should use destructors?

when need special destruction logic. read rule of three. write them if class method named "~" plus class name, taking no parameters , omit return type (even void), ctors.

how should write classes?

you don't have define methods outside of class. in fact, implement methods inside class start out with, move methods out there need. harder screw while learn important basics of language, , more convenient types of classes you'll write initially. use ctor initializers when feasible assignment within ctor body.

struct test {   test(int x, int y)  // use same names rather inventing 'a' , 'b'.   : _x (x), _y (y)   {}    int tester() const { return _x + _y; }   // move outside class when needed, if @ -- , won't needed   // function this.   // because doesn't modify anything, it's suitable const,   // means can called on const test object.    int _x, _y;   // technically public, "non-public name".  mark private   // wish, or design settles down. }; 

note i've used "struct" declare class instead of "class". result identical, except skip "public" on base classes (there none here, bases more public not) , default member accessibility "public" too: results in shorter , more clear code.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -