bhok

Untitled

Dec 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.91 KB | None | 0 0
  1. //// What is Object Oriented Programming (OOP) ////
  2.  
  3. // I still remember my first days when I first attempted to learn how to code. Those dark nights with the blue glow against my face.
  4. // It was very intimidating at first. I literally had just typed in the word "How to program" and there were an endless scroll of
  5. // Google results that always gave me a different answer with each click. Being a naive person, I simply just click on the first
  6. // few search results. Click. Click. I checked out a couple of blog post, Quora posts, and Reddit. They were all very helpful, but
  7. // the biggest problem of them all....
  8.  
  9. // Every site had a different answer. Oh god...I never had a straight answer. I even went on Coursera and Edx to attempt to look at
  10. // the curriculum...Yeah, there were pretty taunting at first. Questions, quizzes, exams. The videos were all very entertaining, but
  11. // I felt too much of a newb and I really struggle hard to acquire the right skills of a developer. (Props to those who are self-
  12. // taught). I felt like I needed something more structured and broken down. And thus.. This is how the long journey goes.
  13.  
  14.  
  15. // Week 1 - I need to know how to basics.
  16.  
  17. // I have seen it all. From the "Learn on your phone, it's easy!" to "Coding Is Easy as 1,2,3" So encouraging! I love it!!
  18. // This is all they all begin. Simple syntax learning. Ummm. Hey, isn't this a book? Where do I code?
  19. // Good question...Umm..Let's start with a simple website! Cpp.sh (C++ shell). You may use any other editor, but for this
  20. // chapter's sake. We are starting with cpp.sh.
  21.  
  22. // Upon pulling up the page, you should get something like this.
  23.  
  24. //----------------------------------
  25. // Week 1 : Understanding The Output
  26. //----------------------------------
  27.  
  28. // Yuck. This is some ugly code already.
  29. // What am I looking at? This looks so fun, doesn't it?
  30. // When I first started, I didn't know what this all meant either and I was basically told
  31. // to ignore everything and just listen to the steps.
  32. // Well...That was helpful...for a bit, but that didn't really contribute to my learning.
  33. // I really need to break down on everything and that is how I really learn.
  34.  
  35. // So let's start!
  36.  
  37. #include <iostream>
  38. #include <string>
  39.  
  40. int main()
  41. {
  42. // (Thoughts) - To be honest, I really don't know what it all means at the moment.
  43. // I think they should have given us a more easier example.
  44.  
  45. std::string name;
  46. std::cout << "What is your name? ";
  47. getline (std::cin, name);
  48. std::cout << "Hello, " << name << "!\n";
  49. }
  50.  
  51. //----------------------------------
  52. // Week 1.1 : Simple Code
  53. //----------------------------------
  54.  
  55. // How about we start with this instead?
  56. // Copy everything below and paste it into your editor of choice. (cpp.sh?)
  57.  
  58. #include <iostream>
  59.  
  60. int main()
  61. {
  62. std::cout << "Whoa, whoa, whoa!!! I'm alive!!! ";
  63. }
  64.  
  65. //----------------------------------
  66. // Week 1.2 : Output - First Crack
  67. //----------------------------------
  68.  
  69. // So, I copied the code into cpp.sh and ran it.
  70. // I saw on the bottom, the words "Whoa, whoa, whoa!!! I'm alive!!! "
  71. // That is pretty creepy... I wonder what happened?
  72.  
  73. // 1) Try changing the words in the " ", and try running the program again.
  74.  
  75. // *****What happened?
  76.  
  77. // I saw that the words changed. Hmm. Okay, interesting.
  78. // Why not, we make our own sentence now?
  79.  
  80. // ******Fill in the blank
  81. // ________ << "I'm some code";
  82. // Now, take this line above and try inserting into your code.
  83. // I wrote an example answer in the next section.
  84.  
  85.  
  86. //----------------------------------
  87. // Week 1.25 : Output Code - Example
  88. //----------------------------------
  89.  
  90. #include <iostream>
  91.  
  92. int main()
  93. {
  94. std::cout << "Whoa, whoa, whoa!!! I'm alive!!! ";
  95. std::cout << "I'm some code";
  96. }
  97.  
  98. //----------------------------------
  99. // Week 1.3 : Output - Second Crack
  100. //----------------------------------
  101.  
  102. // To be honest, I really don't know what I did.
  103. // I literally copied that strange std::cout line and change the words inside the "".
  104. // Yeah, that is basically it. That was literally my first lesson.
  105. // I don't know what happened, but try to what some of the syntax means.
  106.  
  107. // Let's breakdown this line of code
  108.  
  109. // std::cout << "Whoa, whoa, whoa!!! I'm alive!!! ";
  110.  
  111. // What is std::cout?
  112. // std::cout means Standard C Output
  113. // Whenever, I write this word, it means that I want to output something onto the screen.
  114.  
  115. // What is << ?
  116. // << is one of the operator overloads.
  117. // The exact name for "<<" is insertion operator.
  118. // Whenever we use "<<", we want to insert something into something.
  119.  
  120. // std::cout << "Whoa, whoa, whoa!!! I'm alive!!! ";
  121.  
  122. // ****Can you find what is happening in this screen now?
  123.  
  124. // Basically, we are taking the sentence within "", and inserting it into the standard output or aka, our screen.
  125.  
  126. // Now let's take a look at some more lines.
  127.  
  128.  
  129. // #include <iostream>
  130. // int main()
  131. // {
  132. // std::cout << "Whoa, whoa, whoa!!! I'm alive!!! ";
  133. // std::cout << "I'm some code";
  134. // }
  135.  
  136. // What is #include <iostream>
  137. // This line is read as "include input and output stream".
  138. // This is considered as a "library header". Which acts a unique code empower.
  139. // For example, if we were to type in the words std::cout on its own, without the library
  140. // header <iostream>, then std::cout would have no usage and looks like gibberish to the computer.
  141. // However, when we add #include <iostream> to the top of our program, this grants unique abilities
  142. // to a series of words. Now std::cout translates and holds the meaning of
  143. // standard C-Out and it makes sense to the computer. Now does it make sense that it is called a library header now?
  144.  
  145.  
  146. // What is int main()?
  147. // I spent a long time trying to figure out what this was. Nobody actually told me and using Google was too complicated.
  148. // After figuring out what it was, I found out it was called the main function. Basically, this main function is a signal to
  149. // the computer to start here or "THIS IS LINE 1". No matter how big your code is, 100, 500, or 1000. The computer will start at
  150. // int main() because you told the computer that is line 1!
  151.  
  152. // What are { } ?
  153. // These are brackets. They just hold code and it belongs to watever it is on top.
  154.  
  155. // Codie Monster
  156. // { everything inside these brackets belong to Codie Monster }
  157.  
  158. //----------------------------------
  159. // Week 1.4 : Exercise 1 - Make your own code
  160. //----------------------------------
  161.  
  162. // Based on what you learned so far, try writing your own code and getting it to work in cpp.sh.
  163. // Try it and then paste your code into this box.
  164.  
  165. // ***************
  166.  
  167. //----------------------------------
  168. // Week 2 : If and Else Statements
  169. //----------------------------------
  170.  
  171. // Okay, so I know how to output a couple of sentences onto the screen. A week should have already passed if I were studying this on // my own. Outrageous isn't it? I remembered when I first started, I was told to go install this, go install that, make this run.
  172. // It felt like a cat and mouse game. I kept on moving around and I didn't know if I was making any progress or not. There were
  173. // so many code editors out there and I didn't know which was good or not. No tutorial was there to help and it basically took me
  174. // almost a week to get my first code out. Very strange indeed.
  175.  
  176. // By now, the most infamous "Hello World" example should be a breeze and accomplishmentable.
  177. // Hint: (Just output "Hello World")
  178. // I am or should be ready to tackle problems!! Yes? Let's start!
  179. // The next on the list are "If and While Statements".
  180.  
  181. //----------------------------------
  182. // Week 2.1 : If Statements
  183. //----------------------------------
  184.  
  185. // What does the word "If" mean in English?
  186. // I remembered when I first taught English, this was also a very important "grammar structure" that most of my ESOL students
  187. // had to learn. The way I structured it was...
  188.  
  189. // If ..... Then.....
  190. // If I went to play ball, then the dogs would come out.
  191. // If (condition), then (action)
  192.  
  193. // In code, this follows the exact structure.
  194. // Here is the syntax for if C++ statements.
  195.  
  196. // if (condition)
  197. // {then this code run};
  198.  
  199. //----------------------------------
  200. // Week 2.15 : If Statements Example Code
  201. //----------------------------------
  202.  
  203. // Try running the code below and see if it works or not.
  204. // Then try
  205.  
  206. #include <iostream>
  207.  
  208. int main()
  209. {
  210. int x = 10;
  211. if (x = 10)
  212. {
  213. std::cout << "Yes, X is 10";
  214. }
  215. else
  216. {
  217. std::cout << "No, X does not equal 10";
  218. }
  219. }
  220.  
  221. //----------------------------------
  222. // Week 2.175 : If Statements Example Code Comment
  223. //----------------------------------
  224.  
  225. // What the? Did I see an extra line of code?
  226. // What are these extra line of codes?
  227. // Well, let's break it down.
  228.  
  229. // int x = 10;
  230.  
  231. // x is the variable name. A variable name is a name that you
  232. // use to label any variable. It can be x, y, hotdog, pizza, anything you want.
  233. // int is short for integer.
  234. // An integer is a number that is whole; -200, 5, 75.
  235. // int is the datatype that you decided for the variable.
  236.  
  237. // Example:
  238. // When I tell you that I have the letter X in my hand and it means something,
  239. // that doesn't really help much. However, when I tell you that this letter X is an integer,
  240. // now it brings a lot more info to the table. Oh, so I see that the X in my hand is an integer!
  241. // Now afterward, we must set the value of the X, in which we add the "=" symbol and now X equals 10.
  242. // A datatype is used for clarification purposes and allows us to understand what type of data we are working with.
  243. // When declaring (creating) new variables, we must set the datatype before the variable name.
  244.  
  245. // Declaring
  246. // int y;
  247.  
  248. // Intializing (setting a value to a variable)
  249. // y = 10;
  250.  
  251. // Declaring and Initializing
  252. // int z = 50;
  253.  
  254. //// Else Statements ////
  255.  
  256. // Now, what we saw an if statement, but we also saw an else too!
  257. // What does this mean overall?
  258.  
  259. // In English, we would say...
  260. // If I am going to the park, I want to find a cheetah.
  261. // If I go to the park I want to find a cheetah, else I am going downtown to skateboard.
  262. // Mathematically...
  263. // If (this happens), then (do this)
  264. // If (this doesn't happen), else (this happens)
  265.  
  266. // Fill in the blanks
  267.  
  268. #include ________
  269.  
  270. int main()
  271. {
  272. ___ hamburgers = _ ;
  273.  
  274. if (hamburgers = 3)
  275. {
  276. std::cout __ "I am hungry";
  277. }
  278. else
  279. _________ << "I need money"_
  280. }
  281.  
  282. //----------------------------------
  283. // Week 3 : While and For Statements
  284. //----------------------------------
  285.  
  286. // Quick Review
  287. // Can you declare a variable?
  288. // Can you intialize a variable?
  289. // Can you declare and intialize a variable now?
  290.  
  291. // After a week of trying to figure out "if and else statements", I felt like I was finally making some progress.
  292. // Is this really all to coding? If it is, then this is so easy! I mean, come on! If it is that easy, then I should be a pro
  293. // in no time! All I have to do is use these statements and mash up stuff together right? Easy as chicken pie...
  294.  
  295. // Loop Statements
  296. // A statement that repeats if the conditions are true.
  297. // A way to repeat or a loop you may say.
  298.  
  299. // While statements
  300. // In English, we would say...
  301. // While I am eating, I will watch TV.
  302. // While I am driving, I will not text.
  303. // Mathamatically
  304. // While (condition is true), do this.
  305.  
  306. // Fill in the blank exercise
  307.  
  308. #______ <iostream>
  309.  
  310. int main()
  311. {
  312. ___ hamChicken = 3 ;
  313.  
  314. while (hamChicken < 5)
  315. {
  316. _______ __ "ham ham ham";
  317. hamChicken = hamChicken + 1;
  318. }
  319. }
  320.  
  321. // Bonus question: What does "hamChicken = hamChicken + 1" do?
  322.  
  323. //// For Statements ////
  324.  
  325. // For Statements are no different from a while loop. They are similar in every way,
  326. // but the only difference is the way it is structured.
  327. // Let's take a look at how it works.
  328.  
  329. // Syntax:
  330. // for ( initization; condition; increment )
  331.  
  332. // Example syntax:
  333. // for (int steak = 0; steak < 5; steak++)
  334.  
  335. // Now, when I first saw this, I was immediately confused. This looks a bit heavy already and
  336. // does not makes a lot of sense for the normal person I am. I believed I spent many months just copying this structure and
  337. // not know exactly what it exactly does. One problem that really stood out to me was...What does initialization mean? What does
  338. // it really do? Um what? Which led to finally learn what a counter is.
  339.  
  340. // Counters
  341. // A way to keep track of how many loops have occurred.
  342. // When using while loops, the counters are often found outside the while loop.
  343. // Here is a very clear example of how the two are structured. I believe I didn't know the exact difference until maybe now!
  344.  
  345. // While Loop
  346.  
  347. // int counter = 0;
  348. // while (counter < 3) {
  349. // counter++ }
  350.  
  351. // For Loop
  352.  
  353. // for (int counter = 0; counter < 3; counter++) {
  354. // }
  355.  
  356. // Now, you may use a while loop or for loop; whichever you find more appropriate or likable for your needs.
  357. // They both do the exact thing. The only difference is preference and the while loop allows for more control on placement of the
  358. // counter.
  359.  
  360. // Question 1
  361. // What if I put the counter inside the while loop?
  362. // Try creating your own while loop with a counter inside and explain how it works.
  363. // ************bow here
  364.  
  365. // I did not know what it meant
  366. // Try writing your own for statement below
  367. // ***************
  368.  
  369. //----------------------------------
  370. // Week 4 : Assessment Break
  371. //----------------------------------
  372.  
  373. // Before moving on, I suggest developing a little bit and seeing if you can handle the material.
  374. // Problem:
  375. // Develop a code that solves this problem
  376. // Using a while or for loop, I would like to create a pizza delivery screen.
  377.  
  378. // There are a total of 10 orders.
  379. // Every odd number order should be a "L Pizza + 1 Drink"
  380. // Every even number should be "XXL Pizza + Mystery Sauce"
  381.  
  382. // The Output Should Look Like This
  383. // Pizza Order 1:
  384. // L Pizza + 1 Drink
  385. // Pizza Order 2:
  386. // XXL Pizza + Mystery Sauce
  387. // Pizza Order 3:
  388. // L Pizza + 1 Drink
  389. // Pizza Order 4:
  390. // XXL Pizza + Mystery Sauce
  391.  
  392. //----------------------------------
  393. // Week 4 : Arrays
  394. //----------------------------------
  395.  
  396. // It is now week 4. It has been nearly a month and I have barely covered over the basic syntax of coding. If this was an university
  397. // course, I believe they would already finished all of this by week 1! (Actually they really do. I took a real course at a university
  398. // and they quickly and thoroughly explained the differences in two weeks). But hey! I am just a single person learning on my own.
  399. // I don't want to hopping onto shiny toys, but rather I would take the time to really learn my foundation and seeing it how it all
  400. // work together.
  401.  
  402. // English definition: ordered arrangement, in particular.
  403. // An array isn't all that complicated.
  404.  
  405. // Syntax
  406. // datatype arrayName[arraysize];
  407. // The array size is how many elements do you want the array to have.
  408.  
  409. // Declaration
  410. // int ticketNumbers[3];
  411. // Initization
  412. // ticket Numbers[3] = {0,3,4,5};
  413. // Declaring and initization
  414. // int ticketNumbers[3] = {0,3,4,5};
  415.  
  416. // An array ic consist of elements.
  417. // We can access the elements of an array by calling its index.
  418.  
  419. // Exercises
  420.  
  421. int ticketNumber[3];
  422. ticketNumber[3] = {0,3,4,5};
  423. std::cout << ticketNumbers[1];
  424.  
  425.  
  426. // Try running this code.
  427. // What number do you get when you call the index at 1?
  428. // Try changing the index to 0, 3, 4, and 2. What numbers do you get?
  429. // List them below
  430.  
  431. // What are the usages of arrays?
  432. // Say, I got a list of ID numbers. We can store different numbers into each list and access them based on their order.
  433. // int IDnumber[5] = {25,15,16,13,14}
  434.  
  435. //----------------------------------
  436. // Week 5 : Functions
  437. //----------------------------------
  438.  
  439. // If I was learning on my own, a year has already passed and I did not know the correct usage or meaning of a function.
  440. // I believe there are many ways to explain a function, but after so many online lessons and tutorials, I never did grasp
  441. // the meaning of a function. Is it because the material wasn't so in depth or I wasn't a great learner? When I took my first
  442. // programming class in university, we spent nearly a whole semester on understanding functions very throughly and
  443. // getting everything to work. It was very in depth and it explained a lot of things that I needed to know. Some books do
  444. // go into depths of breaking down a function and explaining it well and many online resources just skims it and explain
  445. // what is needed to be practical immediate.
  446.  
  447. // For this section, I will do the hybrid approach of giving you the knownledge of what needs to be practical and going into
  448. // enough depth in what I believe is cruical for all developers.
  449.  
  450. // Placement
  451. // There are two ways to place a function in C++
  452.  
  453. // Standard Procedure
  454. // 1) Prototype
  455. // 2) Function Implementation
  456.  
  457. // Immediate Implementation or Small Program
  458. // 1) Function Implementation above main
  459.  
  460. // syntax
  461. // returnType functionName (datatype argument1, datatype argument2) {
  462. // };
  463.  
  464. // function example
  465. // int formulaMath ( int input1, int input2){
  466. // }
  467.  
  468. // A prototype (or function declaration) tells
  469. // the compiler the name, return type, number and type of parameters to expect
  470.  
  471. // What is a function and what is it's purpose?
  472. // When I first learned this, it took me a while to figure out what a function really is.
  473. // There were many explanations about functions and what it did, but I never really understood it
  474. // until it was compared to an actual real-life example. In fact, a function works exactly like it
  475. // does in math.
  476.  
  477. // If you recall Algebra I or II, a programming function is
  478. // exactly like the math function F(x). If you don't recall this concept, don't worry
  479. // instead here is a better metaphor to explain it.
  480.  
  481. // A function is simply a set of instructions that accepts inputs, performs on it, and outputs it.
  482. // Still confused? Here is a diagram that I found really helpful.
  483. // This is a function -> [ ]
  484. // Now I am going to input some numbers into it and it is going to output a new number. Just watch.
  485. // 5 ---> [ ] --- > 10
  486. // Now, why did this happen?
  487. // A function is a set of operation. If we look inside at the diagram and see more of it.
  488. // 5 ---> [ x * 2 ] ---> 10
  489. // Hmm. Does this make sense now?
  490. // How about explaining it in this box
  491. // *******
  492.  
  493. // Basically, this is what a function is except, it not only deals with numbers, but it could be any set of instructions.
  494. // We can create words and then use a function to alter the words too!
  495.  
  496. ////// Parameters
  497.  
  498. // Now that we understand what functions are, we can now push for the syntax understanding of a function.
  499. // int functionMath (int input1, int input2);
  500. // Looking at this prototype, we can break it down into pieces.
  501. // ( ) is considered the parameters, and we pass arguments, which would be the individual inputs.
  502. // Now, looking at the arguments, they are all integers. We are using integers inputs to insert into our function.
  503. // Next, do you notice the int before functionMath? That specific datatype is considered the return type. The return type
  504. // is the datatype that is returned by the function.
  505. // Take a look at the diagram again.
  506. // arguments (int) ---> [ functions] ----> returntype (int)
  507. // Basically, we have inputs that are integers and we are asking for an output that is also an integer.
  508. // And that is the returntype!
  509.  
  510. //// Try explaining this concept down to a duck and see if it works.
  511.  
  512. //
  513.  
  514. // Is an Oreo cookie still an Oreo cookie without the filling inside?
  515.  
  516. // Empty Functions - Be able to recognize them
  517.  
  518. // A function that is empty doesn't accept or returns any information.
  519. // What is the use? Treat it as a skeleton for a function and be able to recognize it for the moment.
  520.  
  521. //Section 1
  522.  
  523. void emptyChickens();
  524. void noBeef( void );
  525.  
  526. int main()
  527. {
  528. emptyChickens();
  529. noBeef();
  530. system("pause");
  531. }
  532.  
  533. void emptyChickens()
  534. {
  535. std::cout << "This function has no arguments" << std::endl;
  536. }
  537.  
  538. void noBeef ( void )
  539. {
  540. std::cout << "This function accept no arguments either" << std::endl;
  541. }
  542.  
  543. // By now I guessing you may or may not know what void means and its usages
  544. // Can you explain what you think it means and its practical usages?
  545. // You may find more information on the web
  546.  
  547.  
  548.  
  549.  
  550.  
  551. //----------------------------------
  552. // Week 6 : Pointers
  553. //----------------------------------
  554.  
  555. // I remembered when I was studying object oriented at the university level and had visit the student tutor.
  556. // I had asked him to explain what is a pointer and can you explain it to me?
  557. // The only thing I recalled from that particular lesson was "follow the pointer". Yeah...That wasn't really that helpful.
  558. // To be honest, it took me a while to figure out what a pointer truly meant. Even today, I think my grasp of pointers is pretty good,
  559. // but my mastery of advance level of pointer still needs working. Let's start with the fundamentals first.
  560.  
  561. // What is a pointer?
  562. // The best metaphor that I came up with is, a pointer is a finger and nothing more.
  563. // A finger can point at things and tell you what's at that specific location.
  564. // When it comes to development, a pointer is meant to "point" at a specific address and tell you what's at that address.
  565. // Is it an integer, is it a character, or is it a string? Who knows.
  566.  
  567. // What is the purpose of a pointer?
  568. // To conserve memory and make efficient programs.
  569. // Instead of working with actual data, wouldn't it be easier just to point at what you need instead?
  570.  
  571. // What I didn't know until recently is that a pointer can only point to three things.
  572. // A pointer can only point at another address, another pointer, or a null value. You can't have a pointer point at something else
  573. // besides these three.
  574.  
  575. // So, let's start.
  576. // Declaring a pointer
  577. // There are three ways to declare a pointer, but they all mean the same thing.
  578. // int* pointer1;
  579. // int * pointer1;
  580. // int *pointer1;
  581. // I generally like the first declaration of the pointer.
  582. // What you see here is a "*" symbol, correct?
  583. // That just means that you have declared a pointer that is pointing to an int dataspace.
  584. // So, you must be dealing with integer only.
  585.  
  586. // Intializing a pointer
  587. // int* pointer1 = 5;
  588. // pointer1 is pointing to the value of 5.
  589. // pointer1 is not the value of 5, but rather just points to it.
  590.  
  591. // std::cout << pointer1;
  592. // When we perform this, we can see that pointer1 is the address of the value 5.
  593. // std::cout << *pointer1;
  594. // When we dereference pointer1 with *, we can now go to the address of pointer1 and see that there is a value of 5 at that address.
  595.  
  596. // Exercises
  597. //
  598.  
  599. // How to move a pointer
  600.  
  601. // Now, we see that pointers can be used to point at a single object.
  602. // How about we see how pointers are used on a larger scale.
  603.  
  604. int main()
  605. {
  606. // declare out first array
  607. int array[5] = {0,1,3,4,5};
  608. // declare our pointer and have intialize it to our array.
  609. // pointer1 is now equals to array.
  610. int* pointer1 = array;
  611.  
  612. // This should output the first element of the array
  613. std::cout << *pointer1;
  614.  
  615. // Now can you guess what these numbers are?
  616. std::cout << *(pointer1 + 2);
  617. std::cout << pointer1[3];
  618. }
  619.  
  620. // Exercises Math Pointers****
  621.  
  622. // How to use a pointer in a function
  623.  
  624.  
  625. // Pointer Math Exercises
  626.  
  627. //----------------------------------
  628. // Week 7 : Dynamic Data with Pointers
  629. //----------------------------------
  630.  
  631. //
  632.  
  633. //----------------------------------
  634. // Week 8 : Classes 1 : Setting Up
  635. //----------------------------------
  636.  
  637. //----------------------------------
  638. // Week 9 : Classes 2 : Inheritance
  639. //----------------------------------
  640.  
  641. //----------------------------------
  642. // Week 10 : Classes 3 : Polymorphism
  643. //----------------------------------
Add Comment
Please, Sign In to add comment