Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

565 lines
6.3 KiB

  1. ## Namespaces
  2. ## Input/Output
  3. ```
  4. using namespace std;
  5. #include <iostream>
  6. int main()
  7. {
  8. cout << "Hello World" << endl;
  9. int a;
  10. cin << a;
  11. cout << "You entered: << a << endl;
  12. return 0;
  13. }
  14. ```
  15. ## Global Variable
  16. ```c++
  17. using namespace std;
  18. #include <iostream>
  19. double bar = 64;
  20. int main ()
  21. {
  22. double bar = 12;
  23. cout << "Local bar: " << bar << endl;
  24. cout << "Global bar: " << ::bar << endl;
  25. return 0;
  26. }
  27. ```
  28. ## Multiple Names for a Variable/Aliasing
  29. ```c++
  30. double pi = 3.145;
  31. double &x = pi; //pi is x
  32. x = 2.1;
  33. cout << "pi: " << pi << " x: " << x << endl; // prints pi: 2.1 x: 2.1
  34. ```
  35. ## Passing Variables by Reference
  36. ```c++
  37. void change (double &r, double s)
  38. {
  39. r = 100;
  40. s = 200;
  41. }
  42. int main()
  43. {
  44. int x = 1;
  45. int y = 2;
  46. cout << x << ", " << y << endl;
  47. change(x, y);
  48. cout << x << ", " << y << endl;
  49. return 0;
  50. }
  51. ```
  52. Same code in C.
  53. ```c
  54. void change(double *r, double s)
  55. {
  56. *r = 100;
  57. s = 200;
  58. }
  59. int main()
  60. {
  61. int x = 1;
  62. int y = 2;
  63. printf("%d, %d", x, y);
  64. change(&x, y);
  65. printf("%d, %d", x, y);
  66. return 0;
  67. }
  68. ```
  69. ## Functions Returning Variables not Values
  70. A reference can be used to have a function return a variable -- not a value.
  71. ```c++
  72. using namespace std;
  73. #include <iostream>
  74. int &smallest (int &x, int &y)
  75. {
  76. if (x < y)
  77. return x;
  78. else
  79. return y;
  80. }
  81. int main ()
  82. {
  83. int k = 33;
  84. int m = 2;
  85. cout << "k: " << k << " m: " << m << endl; // prints k: 33 m: 2
  86. smallest (k, m) = 10;
  87. cout << "k: " << k << " m: " << m << endl; // prints k: 33 m: 10
  88. return 0;
  89. }
  90. ```
  91. ## Namespaces
  92. ```c++
  93. namespace foo
  94. {
  95. int a, b;
  96. }
  97. int main()
  98. {
  99. first::a = 2;
  100. return 0;
  101. }
  102. ```
  103. ## Inline -- similar to Macros
  104. Inline can be used to replace a function which contains very simple logic -- no
  105. for loops, etc. Like a macro, this will be inserted everywhere the code is used; a
  106. draw back to inline methods is that the compiled source will be larger. But, they
  107. typically run faster.
  108. ```c++
  109. inline int square(int x)
  110. {
  111. return x * y;
  112. }
  113. int main()
  114. {
  115. int k = 4;
  116. cout << square(k) << endl; //prints 4
  117. return 0;
  118. }
  119. ```
  120. ## Exceptions
  121. ```c++
  122. int x;
  123. cout << "Type a number: ";
  124. cin >> x;
  125. cout << endl;
  126. try
  127. {
  128. if(a > 150)
  129. throw 150;
  130. if(a < 15)
  131. throw 15;
  132. throw a % 2;
  133. }
  134. catch(int result)
  135. {
  136. cout << result << " was thrown." << endl;
  137. }
  138. ```
  139. ## Default Parameters for Functions
  140. ```c++
  141. double multiply(double x, double y = 5)
  142. {
  143. return x * y;
  144. }
  145. int main()
  146. {
  147. cout << multiply(4) << endl; // 20
  148. cout << multiply(4, 4) endl; // 15
  149. return 0;
  150. }
  151. ```
  152. ## Function Overloading
  153. ```c++
  154. double add(double x)
  155. {
  156. return x + x;
  157. }
  158. double add(double x, double y)
  159. {
  160. return x + y;
  161. }
  162. int add(int x, int y)
  163. {
  164. return x + y;
  165. }
  166. int main()
  167. {
  168. cout << multiply(4) << endl; // 20
  169. cout << multiply(4, 4) endl; // 15
  170. return 0;
  171. }
  172. ```
  173. ## Operator Overloading
  174. ```c++
  175. using namespace std;
  176. #include <iostream>
  177. struct tuple
  178. {
  179. int x;
  180. int y;
  181. };
  182. tuple operator + (int a, vector b)
  183. {
  184. vector r;
  185. r.x = a + b.x;
  186. r.y = a + b.y;
  187. return r;
  188. }
  189. int main ()
  190. {
  191. tuple k, m; // No need to type "struct tuple"
  192. // also no need to typedef
  193. k.x = 3;
  194. k.y = 6;
  195. m = 2 + k; // Voodoo witchcraft
  196. cout << "(" << m.x << ", " << m.y << ")" << endl;
  197. return 0;
  198. }
  199. ```
  200. ## Functions with Generic Parameter Types
  201. ```c++
  202. template <class ttype>
  203. ttype max (ttype a, ttype b)
  204. {
  205. ttype r;
  206. r = a;
  207. if (b < a) r = b;
  208. return r;
  209. }
  210. template <class type1, class type2>
  211. type1 maximum (type1 a, type2 b)
  212. {
  213. type1 r, b_converted;
  214. r = a;
  215. b_converted = (type1) b;
  216. if (b_converted > a)
  217. r = b_converted;
  218. return r;
  219. }
  220. ```
  221. ## Replacement for malloc and free
  222. ```c++
  223. int i*;
  224. i = new int;
  225. *i = 55;
  226. delete i;
  227. i = new int[15];
  228. i[0] = 99;
  229. delete i;
  230. ```
  231. ## Struct Functions
  232. ```c++
  233. struct tuple
  234. {
  235. int i;
  236. int x;
  237. int sum()
  238. {
  239. return i + x;
  240. }
  241. };
  242. ```
  243. # Classes
  244. ```c++
  245. class Tuple
  246. {
  247. public:
  248. int i;
  249. int x;
  250. int sum()
  251. {
  252. return i + x;
  253. }
  254. };
  255. ```
  256. ## Class Constructor and De-constructor
  257. ```c++
  258. class Tuple
  259. {
  260. public:
  261. int i;
  262. int x;
  263. Tuple(int i1, int i2)
  264. {
  265. i = i1;
  266. x = i2;
  267. }
  268. ~Tuple()
  269. {
  270. //delete any memory you have!
  271. }
  272. int sum()
  273. {
  274. return i + x;
  275. }
  276. };
  277. // in main
  278. Tuple t (12, 14);
  279. Tuple tt = new Tuple(12, 15);
  280. ```
  281. ## Scope
  282. ```c++
  283. class Person
  284. {
  285. protected:
  286. int age;
  287. string name;
  288. public:
  289. Person(int age, string name)
  290. {
  291. }
  292. ~Person()
  293. {
  294. }
  295. private:
  296. void increaseAge()
  297. {
  298. age++;
  299. }
  300. }
  301. ```
  302. ## This keyword
  303. ```c++
  304. class Person
  305. {
  306. protected:
  307. int age;
  308. string name;
  309. public:
  310. Person(int age, string name)
  311. {
  312. this->age = age;
  313. strcpy(this->name, name);
  314. }
  315. ~Person()
  316. {
  317. }
  318. private:
  319. void increaseAge()
  320. {
  321. age++;
  322. }
  323. }
  324. ```
  325. ## Class Inheritance
  326. ```c++
  327. class Tuple
  328. {
  329. protected:
  330. int x;
  331. int y;
  332. public:
  333. Tuple(int i1, int i2)
  334. {
  335. x = i1;
  336. y = i2;
  337. }
  338. virtual int sum()
  339. {
  340. return i + x;
  341. }
  342. };
  343. class Triple: public Tuple
  344. {
  345. protected:
  346. int x;
  347. int y;
  348. int z;
  349. public:
  350. Triple(int i1, int i2, i3): Tuple(i1, i2)
  351. {
  352. z = i3;
  353. }
  354. int sum()
  355. {
  356. return x + y + z;
  357. }
  358. };
  359. ```
  360. ## "Abstract" Classes
  361. ```c++
  362. class Animal
  363. {
  364. public:
  365. virtual void speak()=0;
  366. }
  367. class Cat: public Animal
  368. {
  369. public:
  370. void speak()
  371. {
  372. cout << "Meow" << endl;
  373. }
  374. }
  375. ```
  376. ## Method Prototypes for Classes
  377. ```c++
  378. class Cat: public Animal
  379. {
  380. public:
  381. void speak()
  382. {
  383. cout << "Meow" << endl;
  384. }
  385. int fly(); //method prototype
  386. }
  387. // Off in a header file or something
  388. int Cat::fly()
  389. {
  390. return 42;
  391. }
  392. ```
  393. # File IO
  394. ## Reading From File
  395. ```c++
  396. #include <fstream>
  397. //in main or somewhere
  398. fstream f;
  399. char c;
  400. f.open("p022_names.txt", ios::in);
  401. while(!f.eof())
  402. {
  403. f.get(c);
  404. cout << c;
  405. }
  406. f.close();
  407. ```
  408. ## Writing to File
  409. ```c++
  410. #include <fstream>
  411. //in main or somewhere
  412. fstream f;
  413. char c;
  414. f.open("p022_names.txt", ios::out);
  415. f << "stuff in the file " << endl;
  416. int i = 4;
  417. f << i << " this is also in the text file" << endl;
  418. f.close();
  419. ```