Lecture Notes by [email protected]
To get started, do the first thing first:
- Read README.
- Read Syllabus.
- Read 'GettingStarted' - this file
- Follow instructions in 'GettingStarted' as soon as possible(ASAP).
These reading materials is available at my github.com/idebtor/nowic.
There are two ways to join Piazza, go the www.piazza.com.
-
To join Piazza, you may need the following information and
- School: Handong Global University
- Course: Data Structures and C++ for C Coders
-
If you have an email address that ends with ~.hgu.edu or ~.handong.edu domain and use it everyday, go the www.piazza.com and follow the instructions in the website.
-
On your request with your email address, I may register it for you. We'll be conducting all class-related discussion here this term. The quicker you begin asking questions on Piazza (rather than via emails), the quicker you'll benefit from the collective knowledge of your classmates and instructors. We encourage you to ask questions when you're struggling to understand a concept—you can even do so anonymously.
-
Follow this good installation guide available. For mac, good installation guide
-
After the installation, change (add) the following two folder names into user's PATH environment variable:
C:\MinGW\bin C:\MinGW\msys\1.0\bin
-
To check your installation, do the following in cmd windows or in PowerShell;
g++ --version
-
If you encounter the following error message during the first compilation of your program such as 'HelloWorld.cpp', Refer to this site.
c:/mingw/bin/../lib/g++/mingw3 ........ mingw32/bin/ld.exe: cannot find -lpthread
-
Atom is a text editor that most professional programmers love nowadays.
-
Start Atom.
-
Install some of essential packages recommended for C/C++ programmers listed below:
- Autosave
- It automatically saves files when the editors loses focus, are destroyed, or when the window is closed. Believe or not, it is disabled by default. You must check
enabled
in config setting or from the Autosave section of the Settings view.
- It automatically saves files when the editors loses focus, are destroyed, or when the window is closed. Believe or not, it is disabled by default. You must check
- Platformio-ide-terminal
- On successful installation, you will see a +sign at the bottom-left corner of the editor. Click + sign to open a console.
- File-icons
- Mini-maps
- Markdown-preview
- Open a rendered version of the Markdown in the current editor with
ctrl-shift-m
.
- Open a rendered version of the Markdown in the current editor with
- language-c
- Adds syntax highlighting and snippets to C/C++ files
- gpp-compiler
- Allows you to compile and run C++ and C within the editor.
- To compile C or C++, press F5 or right click the file in tree view and click Compile and Run.
- To compile C or C++ and attach the GNU Debugger, press F6 or right click the file in tree view and click Compile and Debug.
Themes of my personal preference:
- UI Theme - Atom Dark,
- Syntax Theme - Oceanic Next
Note for Multi-screen users: If Atom is displayed off-screen, do the following:
- Alt + Tab to choose the atom window
- Alt + Space to open the context menu
- Press 'm' to select move
- Press any arrow key once
- Move your mouse (The misplaced window will follow your cursor.)
- Autosave
After installation of GitHub Desktop, be a member if already not.
-
Clone the GitHub
nowic
repository into your local computer: -
How to clone a repository from GitHub: Refer to this site.
-
Click 'watch' and 'star' at the top of the web page.
-
Then, in your computer, you may have the following github/nowic folder as shown below:
C:\Users\user\Documents\GitHub\nowic
-
Since this
nowic
repository can be updated anytime, keep this local repository as "read-only". Don't code yours here!. -
Copy them into your own repository or your own local development folders in your computer you can easily access them. They should look like the following:
~/nowic/c++cc # c++ for c coders ~/nowic/include # include files ~/nowic/labs # labs ~/nowic/lib # nowic.lib, libnowic.a ~/nowic/src # all source files except labs ~/nowic/psets # problem sets - homework
- For example: The pset01(problem set 01) folder provides you with a few files for your first homework assignment.
~/pset01/pset01.pdf # this file ~/pset01/hellox.exe # a solution to compare your work ~/pset01/names.txt # a list of names used in Step 5.
Note for Multi-screen users: Remove the following file if GitHub Desktop is displayed off-screen. Restart Desktop GitHub. (
user
below may be different in your system.)C:\Users\user\AppData\Roaming\GitHub Desktop\window-state.json
JoyNote: How do I force
git pull
to overwrite local files?- Go to the ~/nowic folder.
- Open a console and run the following two commands.
git fetch --all git reset --hard origin/master
Explanation:
git fetch
downloads the latest from remote without trying to merge or rebase anything. Then thegit reset
resets the master branch to what you just fetched. The--hard
option changes all the files in your working tree to match the files in origin/master Causion: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven't been pushed will be lost.JoyNote: How do I keep my local files clean after trials?
- Go to the ~/nowic folder.
- Open a console and run the following command.
git clean -f
Explanation: To delete all untracked files.
- Open a console. (may use cmd or powershell)
- You may use cmd or powershell at this point. Sooner or later, however, we are going to use a different kind of console called mintty. You may read UsingMinttyInMinGW.md for the Mintty installation at
nowic
folder.
- You may use cmd or powershell at this point. Sooner or later, however, we are going to use a different kind of console called mintty. You may read UsingMinttyInMinGW.md for the Mintty installation at
- Open hello.cpp file with Atom
Atom hello.cpp
- Build an executable. The output name depends on your OS: hello.exe, a.exe for windows; hello.out, a.out for linux
g++ hello.cpp -o hello # hello.exe or hello.out g++ hello.cpp # a.exe or a.out
- Run the executable.
./hello # PowerShell ./a # PowerShell hello # Cmd, Linux, OSX a # Cmd, Linux, OSX
It is a tradition to write a Hello World!
program when you learn a new programming language. The following program prints Hello World!
on a console. Create a file with the following contents. Its file name is hello.cpp
// file: hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
}
//
The line begins with two slashes is not a part of source code. This line will be excluded when the program runs.#include
The line begins with#
is called apreprocessor directive
. The preprocessor reads your program before it is compiled and only executes those lines beginning with a # symbol. When you send the file to the compiler, the preprocessor substitutes the#include
with the contents of the specified file.<iostream>
Theiostream
file must be included since this program usescout
to display console output. This kind of file is called a header file. In this way you can create and use modules that define functions that you only add to your code when you need them. There are lots of different header files available for many different purposes.using namespace std;
C++ uses namespaces to organize the names of program entities. This line declares that the program will be accessing entities whose names are part of the namespace calledstd
. Every name created by theiostream
file is part ofstd
namespace. In order for a program to use the entities such ascout
iniostream
, it must have access to thestd
namespace. You may rewrite hello.cpp as shown below:
// file: hello.cpp
#include <iostream>
int main() {
std::cout << "Hello World!";
}
Observe two versions Hello World!
program to see the difference. The difference is whether you are using the namespace or not.
To run the program, you must make an executable from the source code file, hello.cpp
. We use the compiler to generate an executable (or runnable) file from the code in the source file. The executable file ends with .exe
or .out
extension as our source file ends with .cpp
extension. We might compile the file as follows:
- On the console, type the following line (without $) and press
Enter
key.
$ g++ hello.cpp # generates a.exe, a.out
$ ./a.exe # runs a.exe
$ ./a # runs a.exe, a.out
- The line above compiles (and links) the source code and generates an executable called
a.exe
ora.out
. $
is a console prompt. (You don't need to enter it.)- The line above compiles (and links) the source code and generates an executable called
a.exe
g++
is the name of our compiler, and#
begins a command-line comment. We can run the executable file,a.exe
ora.out
, which will run our main function. If you want to name the executable, you may provide the file name with-.o
option as follows:
$ g++ hello.cpp -o hello # generates hello.exe
$ ./hello.exe # runs hello.exe
$ ./hello # runs hello.exe, hello.out
JoyNote:
- Run
hello.exe
orhello.out
without./
in front, when you run it at Linux, OSX and mintty console^^.
For further study of C or C++ basics, the following two video lectures in YouTube are recommended. A solid background in C will do good for C++ study in this course.
- Beginning C Programming by Bluefever
- C++ Programming in One Video by Derek Banas
- C++ Tutorial by Derek Banas
- Watch Derek's lecture or more.
- Read ArgcArgv.md. It may help you do the first problem set(pset).
- Read UsingMinttyInMinGW.md at
nowic
and installMintty
console.
One thing I know, I was blind but now I see. John 9:25