I'm Going on a C++ Journey!
Tags: cpp
Published: 2020-11-15 03:04:56

---
title: I'm Going on a C++ Journey!
published: true
description: Learning C++
tags: CPP
series: C++ Journey
//cover_image: https://direct_url_to_image.jpg
---

The other day I had to take a coding assessment in C++ as part of a job application, which meant I had to learn C++ in a couple of hours. I did use C++ for a bit 6+ years ago, never got that deep into it, and haven't used it since. I mostly code in JavaScript or Ruby these days so looking back at C++ was like reading a foreign language. I not only had to wrestle with the syntax, I also struggled with getting my IDE to compile. 

In the end I did complete the assessment and I'm proud of what I accomplished. C++ is something I've been wanting to really learn for some time now, since so many game dev positions I see require proficiency with it, and this challenge was the perfect push I needed to dive in. 

For now I'm going to be using a couple of books to learn from: *C++ Programming: Program Design Including Data Structures* 8th ed. by D.S. Malik, which is an updated version of the C++ book I used back when I took some computer science courses, and *C++ Crash Course: A Fast Paced Introduction* by Josh Lospinoso.

For my IDE I will be using Xcode on MacOS. I might switch it up to Visual Studio - I haven't decided yet.

I'll be using this blog series as a way to keep track of what I'm learning, and hopefully it can help others learn something too. 

If anyone has any resource recommendations (Udemy course, EDX course, YouTube videos, etc) please let me know in the comments!

## Breaking Down A Basic C++ Program

```
#include <iostream>
 
int main() {
    std::cout << "Hello, World!\n";
    
    return 0;
}
```


---
`#include` is what is known as a *preprocessor directive*. `iostream` is a Header that defines the standard input/output stream objects. `#include <iostream>` allows us to use predefined input/output stream objects in our program, such as `cout`. Preprocessor directives are processed by the preprocessor (say that 3 times fast), which examines and resolves directives before code compilation.

`cout` is an object of the standard output stream `ostream`, `std` is a namespace ,and `::` is the scope operator. Because `cout` is not in the global namespace, you need to include `std::cout` so the compiler knows where to find it. 

If you don't want to type `std::` constantly, you can tell the compiler to use the standard namespace, which will put everything from `std` into the global namespace.

```
#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!\n";
    
    return 0;
}
```

While this is more convenient and has better readability, it could cause problems when using other libraries. I've seen discourse on this in the past where some people argue to never use `using namespace std`. 

Another option seems to be to use a *using-declaration* within your function:

```
#include <iostream>

int main() {
  using std::cout;
	
  cout << "Hello, World!\n";
    
  return 0;
}
```

I think I'll be sticking to prefacing with the namespace until I read something credible that says I shouldn't. Here is a resource that recommends to just use the prefix `std::`: [Should I use using namespace std in my code?](https://isocpp.org/wiki/faq/coding-standards#using-namespace-std)

The line `cout << "Hello, World!\n"` is an output statement; it tells the computer to output whatever is between quotes to the console. `<<` is called a *stream insertion operator*.

`main` is where your program starts and is a required global function. `int` stands for integer and it is the return type of the `main` function.

`return 0` is the [exit code](https://www.geeksforgeeks.org/exit-codes-in-c-c-with-examples/#:~:text=%20Some%20of%20the%20Exit%20Codes%20are%3A%20,program%20was%20aborted%20%28received%20SIGILL%20%29%2C...%20More%20) and means *EXIT_SUCCESS*.

--

In the next post I'll explain more about return types and get into data types and input.