]> 4ch.mooo.com Git - 16.git/blob - doc/ojoww.cpp
modified: Project 16.bfproject
[16.git] / doc / ojoww.cpp
1 ?!
2 This  is a quick C++ tutorial, first in C++ you can use C files, you make a C  file and give it extension .c (if no header), next to use it in your  C++ you include it as follows
3 #extern "C" {
4      #include "myfile.c"
5 }
6 I don't remember if it's # for extern anyway...
7
8
9 In  C++ and well, C too, you use macros and stuff and do things to the  compiler does not goof, one typical construction is "if (something) has  been defined, then add this code, and after that code end if" for  example it looks like this:
10 #ifdef WIN32
11      #include <windows.h>
12 #endif
13
14 And you get different commands too like #ifndef which means if it is not yet defined, and #else, and #define  which defines something, and so on. So another useful construction you  can use for C++ to avoid stuff like confusing the compiler into  including code more than once is by saying, well if you haven't included  it yet, then include it please, or rather, if you haven't defined my  flag,...
15 ...then  define my flag, and then add my code, and then close that block. So  next time it looks at the same file it goes, oh I already have that one,  ok next!
16 You do it like this
17 #ifndef _FILENAME_H_
18 #define _FILENAME_H_
19 //code here
20 #endif
21 The  _FILENAME_H_ is just supposed to be something unique, so usually when  you write out a filename with caps and underscore you are guaranteed a  unique flag that wont mess up your code. Remember #define defines something AT COMPILE TIME, not runtime.
22
23
24 In  C++ you really, really, really have to separate your code more than in  maybe C. But the same rules apply, you have to alert the compiler of a  function before you implement the function. For example:
25 void myfunc();
26 int main()
27 {
28      myfunc();
29      return 0;
30 }
31 void myfunc()
32 {
33     printf("huehuehuehue");
34 }
35 In  C++ you do this also, this is for the compiler otherwise it doesn't  know what the hell you are doing you moron. But it is also for yourself  and other people. This is because you might want to change exactly how  your functions do what you want them to do, but you don't necessarily  want to change that you have a function that is supposed to do something  important. That is why you declare...
36 ...everything  as "in the best situation I have this function, it takes these  arguments and returns this to me" and separate that from how you  actually do it.
37 The  declarations all go in header files with an extension .h and are  included in all the files that use the code, including the file that  implements the functions. The file that implements the functions has  extension .cpp and you never include this file anywhere. NEVER EVER!   Because these are your implementations, not your descriptions. You only  include the headers, in their respective .cpp...
38 ...file,  hopefully with the SAME NAME except for extension, and include only the  header also in any file that uses that code too.
39 The header (.h)  is the file you want to start with the #ifndef, #define, and end with #endif,  stuff. Not the implementation file (.cpp), if it's only in the header  it works find, if it's in the .cpp too then you will probably break it  at compile time.
40 A  class is like a struct, or object. Think of them like defining your own  types, with complex properties/members, and functions/methods. An  example of using classes, already made could be this:
41 #include "cheetamen.h"
42 int main()
43 {
44      Cheetamen cheeta_one; //one cheetamen
45      Cheetamen cheeta_two; //two cheetamen
46      cheeta_one.happy();
47      cheeta_one.nr = 57;
48      cheeta_two.sad();
49      cheeta_two.nr = 12;
50      printf("Cheetaman %d is very %s", cheeta_one.nr, cheeta_one.get_state());
51      printf("Cheetaman %d is very %s", cheeta_two.nr, cheeta_two.get_state());
52      return 0;
53 }
54 And that would return
55 Cheetaman 57 is very happy
56 Cheetaman 12 is very sad
57 Something like that, depending on what get state does.
58 You define instances of a class with new, and get free up memory with delete.
59 Cheetaman *cheeta;
60 cheeta = new Cheetaman(1, "happy");
61 cheeta = new Cheetaman();
62 delete(cheeta);
63 You  now have a memory leak you baka!!  Ok remember to delete everything  after using it, or save a pointer to it somewhere you can find it again  later.
64 So  a class is like a struct but instead it is called class, with access  modifiers, public so anything can be accessed, protected it can be  messed with, and private it can only be affected internally by the class  itself. This is important. If you do this:
65 cheeta.nr = 7;
66 /*Then  you set the nr member to 7, but if nr is private you can't, so you need  a function part of Cheetamen to update it. The reason for this is  because you can tell other people what to do, you can only ask them to  do something.*/
67 Add classes to a namespace.
68 namespace hereisaname {
69  // all functions and stuff in the name space
70 }
71 You  can declare namespace in other files, it just adds them to the  namespace, this way you can have a lot of similar functions and stuff in  different namespaces. It qualifies their unique names to (namespace  name)::(member name) so it works like a prefix, I guess?  Anyway moving  on.
72 class Cheetaman {
73 public:
74     //this is a class
75     Cheetaman(); //default constructor, this is always called, unless you use another constructor
76      Cheetaman(int x, int y); //overloaded constructor, this is called  when you put two ints in, so this is called instead of default.
77     void happy();
78     void sad();
79     const char * get_state();
80      int nr;
81     ~Cheetaman();
82 private:
83     const char *state;
84 };
85 That  is your declaration of the class, it has two constructors, a default  and one overloaded, and some functions, and variables. When you use a  class you can use the dot operator to use the members .  like this:
86 Cheetaman cheeta;
87 cheeta.happy(); //note the dot...
88 For  your implementation (.cpp) you use the include the (.h) and use the  namespace too, and then define them statically as members I guess you  could say. So Classname::membername like this:
89 #include "cheetaman.h"
90 namespace Cheetas {
91      Cheetaman::Cheetaman()
92      {
93        //default, set all the standard values for member variables and init code here
94       }
95       Cheetaman::Cheetaman(int x, int y)
96       {
97              //do extra stuff with x y
98       }
99      
100      void Cheetaman::happy()
101      {
102       this->state = new //well I forget the specifics for const char types but you get the point...
103       this->state = "happy";
104      }
105      
106      const char* Cheetaman::get_state()
107      {
108             return this->state; //you return that state member of the  actual class, you see?  So it can change (happy/sad) but you always  return the correct one.
109      }
110      //and so on and so on.
111      Cheetaman::~Cheetaman()
112      {
113           //deconstructor, delete all memory allocated here
114       }
115 }
116 this-> is used in the class as . is use outside of the class.
117 And that is more or less the gist of it. Oh yeah and you can inline assembly too, and use boolean types (bool) and so on. 
118 sparky4
119 sparky4!!!!!
120 sparky4: put in a file and upload it!
121 sparky4: put in a file and upload it!
122 sparky4: put in a file and upload it!
123 sparky4 no I am lazy
124 http://boards.4chan.org/jp/res/9468991
125 sparky4
126 sparky4 >/jp/ confirmed for having the hottest white girls
127 sparky4 I knew I should had tried to become a /jp/ idol.
128 sparky4: http://piratepad.net/INvoPrHMSN paste here
129 sparky4: mlet me get some drugs~
130 ^
131 |
132 recursive chatlog