DEV Community

loading...

I'm Going on a C++ Journey!

robotspacefish profile image Jess Updated on ・3 min read

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;
}
Enter fullscreen mode Exit fullscreen mode

#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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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?

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 and means EXIT_SUCCESS.

--

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

Discussion (2)

pic
Editor guide
Collapse
blender profile image
Saloni Goyal

Kate Gregory has a few (beginner, intermediate and advanced level) courses on Pluralsight. She has over 30 years of experience with C++ and is a Microsoft MVP. Highly recommended.

Collapse
robotspacefish profile image
Jess Author

Thanks Saloni! :)