1
Write a post

Beginners Guide to C++ & Programming

Published Jan 12, 2017Last updated Jan 18, 2017
Beginners Guide to C++ & Programming

Introduction

In this tutorial, I will teach you the basics of C++. I will introduce you to the principles of both C++ and programming in general.

What are IDEs

In order to write, edit, save, and run your programs, you will need an IDE or Integrated Development Environment. This is a programming-specific tool that is the most common way for developing programs.

For this tutorial, we will use an online IDE to keep things simple. Later, you could use a desktop IDE of your choice, for example, CodeBlocks (Linux, Windows), Visual Studio (Windows), or XCode (Mac). Or if you're wondering what is the best IDE, perhaps this article can help.

Your first program

Now let's start with some code, but don't worry, I will explain everything. This program will print the famous "Hello world!" when run. So let's start!

#include <iostream>
using namespace std;

int main() 
{
  std::cout<<"Hello world!";
}
#include <iostream>  
using namespace std;

The first two lines are telling the IDE that we want to include and use a pre-defined C++ program component (we need it to print "Hello world!"). The <iostream> part of this line means that this program component is located in a pre-defined C++ code file called iostream, which is a file known by all C++ IDE.

int main()

Between the starting and the ending curly brace is where the actual functional part of our program lays. int main() is the name of this part (the main part) of our program.

So this is the part of all C++ code:

int main() 
{
   Something..
}

Something.. is the core of our program and in our case:

   cout<<"Hello world!";

Pretty short now, isn't it?

cout<<"Hello world!";

The C++ program component cout outputs a text string, a number, a variable (see later) or a combination of them. The two << simply adds something to the list of the printables. The following code segments produce an equal output (note that the use of more than one << won't automatically include spaces between the text strings following them).

cout<<"Hello "<<"world!";

cout<<"Hello"<<" world"<<"!";

cout<<"Hello"<<" "<<"world"<<"!";

Even this (although this is not so sensible):

cout<<"Hel"<<"lo "<<"wo"<<"rld!";

Play around with cout by copying the first code here and changing:

cout<<"Hello world";

Wrapping up

In the IDE, choose C++ 4.9.2 (Gcc-4.9.1), delete the sample code, and start experimenting! You will see your program's output if you scroll down.

Discover and read more posts from Márton Magyar
get started
Enjoy this post?

Leave a like and comment for Márton

2
Be the first to share your opinion

Get curated posts in your inbox

Learn programming by reading more posts like this