Everything I’ve learned — C++ (Part 1)
The classic “hello world”
Here’s the beginning of everything I’ve learned in C++
Let’s get started with our first C++ program.
I learned C++ on a Mac and the instructions are very similar for those of you on a Linux box, however, Windows users I’m sorry if there’s enough interest, comment below.
Open the “Terminal” program. You can search this in Spotlight or find it in Launchpad > Other:
You’ll be greeted with a screen that looks like this:
Not bad so far! This is a text-based way of interacting with your computer and it’s something you’ll become very familiar with as you learn to code. So here are a few basic commands to help you navigate your computer.
You don’t have to memorize all of them. Use them as a reference when you need them! Really though, there are only a few of these commands you use often. They are:
ls
For listing the files in a directory. Type ls
hit enter and see what happens in the terminal. You should see folders like Desktop
, Documents
, Downloads
, etc…
cd
For changing directories. For example typecd Desktop/
and hit enter and you will move you to into your Desktop
.
mkdir
For making directories. For example type mkdir learn_cpp
and hit enter. You won’t see anything but a folder named learn_cpp
was made.
If you use the cd learn_cpp/
command, you’ll move into that folder.
touch
The touch command can be used to make files. In this case, we want to make a C++ file and the ending for that file-type is .cpp
. Type touch main.cpp
and hit enter. You won’t see anything until you type ls
and hit enter.
You’ll see that we have a main.cpp
file. We want to edit it somehow. There are editors in Terminal
, however, they are probably too complicated for beginners to use.
open
To bypass this complexity, type open main.cpp
and MacOS should open a text editor for us.
In this file type:
int main() {
return 0;
}
Save it and go back to the terminal.
Compiling
To run C++ code you have to compile it, this means converting it into a format that the computer understands. To compile this code we’ll use a program called g++
.
To compile main.cpp
type g++ main.cpp
and hit enter. Calling ls
afterward will show you an extra file you didn’t create a.out
. This is your compiled program.
Running
To run or start this compiled program simply type ./a.out
and hit enter.
Wahoo you did nothing! Let’s go over why
The main.cpp
file did nothing. We wrote an empty program. Let’s see what we actually wrote:
Return Type
Every function has a return type associated with it. In this case, the return type is an int
or integer value. This is a value without a decimal point.
Function Name
Every function has a name. This is the unique identifier that we can call it by. The main
is a special function. It’s the entry point into any program and is reserved for this purpose. It’s paired with int
as the return type. In any application you’ll always see an int main
.
Argument/Parameter List
This is where arguments or parameters can be passed into a function. For example, if you had a sum
function, the arguments could be (int first, int second)
.
Function Body
This is where the magic takes place. It’s where you call code to make stuff happen. In this case, we call the return
reserved word and that exits the function with the specified return value 0
which matches the return type of integer.
Printing Hello World
We can’t write all the code ourselves. And knowing where to begin to write code to print to the screen may seem daunting. This is why libraries were created. Libraries are bits of code other people have written so that we can access certain functionality. In this case, we’ll be using the <cstdio>
library. To use it we call #include <cstdio>
before our main function. This allows us to call the printf
function that prints words to the screen.
The code will look like this:
#include <cstdio>
int main() {
std::printf("%s", "Hello World");
return 0;
}
We’ve looked at defining functions with theint main
example but this is how you call them. We’ve added two new concepts. The first, is header files
. You call bits of libraries to access their functionality with the #include
line. The second is namespaces
. Namespaces
solve an interesting problem of having two different functions with the same name and parameter list. Say that there were two printf
functions and you needed them both. Let’s say one wrote text to the screen Hello World
, the other actually printed a piece of paper with the words Hello World
. Namespaces give us a way of differentiating between those two functions. The printf
function we’re using is in the std
or standard namespace. To call the printf
function in the standard namespace the printf
function prefixed with an std::
will do the trick.
(If you still don’t understand namespaces or include, don’t worry more examples will help assuage this uncertainty. We got so deep into those topics today because we’re trying to print text to the screen and it’s the easiest way to do that)
Coding a Unicorn
Finally, let’s compile back in the terminal one last time with g++ main.cpp
and run the program with ./a.out
. You should see our magical hello world.
And that’s it! You’ve done it! The basics for the next billion-dollar company! Have fun, good luck, and don’t go too crazy (well maybe do)!
If you have any questions regarding anything, feel free to comment or reach out on Twitter at: https://twitter.com/AbhishekPratapa
Thanks!