Intro to C++, learning to do things you can already do in Java
C++ Programming at WikiBooks.
All of the readings are free online, though note that the book is under constant revision. If something looks crazy, ask me about it.
I will lecture about this material in class. These readings are meant to be used in reviewing/studying.
- Why our code is split into .h files and .cpp files: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/File_Organization (6p)
- What is the :: operator for? https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Scope Stop at "unnamed namespace"
- What does the compiler do? What does it NOT do? What does the pre-processor do? What does the linker do? https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Compiler Stop at "Compile speed" (4p) https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Compiler/Preprocessor (13p) https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Compiler/Linker (4p)
- Bitwise operators. https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Variables/Operators#Bitwise_operators (2p)
- Arrays, and the subscript operator. https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Variables/Operators#Subscript_operator_.5B_.5D (5p)
- Pointers. https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Variables/Operators#address-of_operator_.26 and https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Variables/Operators#Pointers.2C_Operator_.2A (9p) Skip the part about multi-dimensional arrays, and the part about pointers to functions.
- Dynamic memory allocation. https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Variables/Operators#Dynamic_memory_allocation (4p)
- Fork this repo
- Fill in the blanks in the main.cpp with code that produces the correct result. Read the comments to find out what they ought to do.
- Be sure to
git commitandgit pushoften - Update this file with your documentation and the answers to questions.
- When you are ready to turn in your homework, submit a pull request from your repo to mine.
- Be sure to check the pull requests for my repo on github to make sure it worked, and that your work has been submitted.
For each of the following functions in main.cpp, tell me whether or not you think it is working in your submission.
- prime - Yes, it's working. My loop is a bit odd, but it works efficiently, I guarantee it. The for-loop in this method starts at 3, stops at sqrt(n), and steps by two. I explained why this is in a comment section above the method. The only problem I have with my own code is that I had to check 1, 2, and 5, which I wish I didn't have to do, but these checks are necessary for my... let's say unique for-loop.
- defix - This method was simple, I completed it in two lines. It works fine.
- sumSlice - Yes, it works just as it is supposed to.
- square - I like this one, the little ASCII-type drawings are neat. Mine works.
- listPrimes - Working fine, sir
1. In C++, the compiler compiles each .cpp file separately, without looking at the others. Explain why this leads to the need for .h files.
We can easily #include any header file and allude to it in our .cpp files. Header files are for storing little methods or declarations to save space in .cpp files, so we can just reference the relevant .h file when needed.
2. Explain the individual roles of the preprocessor, the compiler, and the linker. What type of inputs do they take? What kind of outputs do they produce? What is the purpose of each?
The preprocessor directs the compiler on how to process the source code. The compiler can also hold directives to header files and C++ Standard Library (classes, etc.) using the preprocessor (example: #include uses preprocessor directives). The compiler translates source code from programming language (source code) to the computer's native language. It translates our code to something the computer can read. The linker makes executable files and deals with file/class/object references and links from one place to another.
A pointer object "points" to another object by storing the address of that object. These are denoted by an asterisk next to the variable type in declaration.
4. If I have a variable declared as int x, how do I find out what memory address that variable is stored at?
By printing out &x like so: std::cout << x << "," << &x; This would print out "x,[address of x]".
A pointer: int* p
6. Just like Java, C++ has a new command. But C++ also has a delete command that Java does not have. Why do we need delete in C++, but not in Java? What is delete good for?
In c++, as long as a variable is being pointed at, it is using memory to store the address. The delete command is to free up the space used by pointing to objects.
How can make Call of Duty