ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Getting Started with C++

Updated on July 23, 2013

DIVING IN...

It is easy to go with the flow when you have started learning a programming language. However, the hardest part is getting started. Where will you write your code? How will you compile it? How should you go about learning a new language? If you have these question in mind, this article is for you.

So, you have decided to learn C++ and are now clueless what to do. You don't know which tools to use or what to choose. Don't worry, help is at hand.

But first let us make sure that you will stick to programming even when the time's are hard and you start to regret the time you spent programming. Programming is nothing that you will learn in a day nor it is a cakewalk but for those who are passionate enough, it is something that will take you to a whole new level of life.

If you are ready to enjoy the curiosity that tickles your mind every time you find an error and will never get tired of writing thousands of lines of codes, then we are ready to get started.

INTRODUCTION TO IDE

First of all, you write your code. Since every code is just text, you can use just about any text editor to write your code. Even the simplest Notepad can do the task.

Next, you compile your code, which is the process of converting the text you have written into binary form which the computer can understand. You need a special program known as a compiler for this.

Now that the code is in compiled form, you run it and check for errors and see if everything is working fine. This is known as debugging your code.

That seems to be complicated. First you write your code in some editor, then you compile it with another program, and then run it and check for errors. That is a lot of stuff to understand for someone who is just entering the field of programming. Fortunately, there is software known as an Integrated Development Environment (IDE) available nowadays which beautifully pieces together all of these tasks under one single roof and makes the task easy for the programmer, that is you.

This brings us to our first step. Whether you want to make a game or a mobiles application or just a simple quiz, you need an IDE. This is the place where you will write your program, compile it and run it. IDEs are like programmer's crafting bench.

Now the question is, "Which IDE should I download?". There are a number of options available, though I would recommend:

  • Visual Studio, If you are a student and working on a Windows machine.
  • Code::Blocks, if you are using Linux, or using a Windows machine and not a student (because you don't want to spend money on this until you get serious, and Visual Studio is free for students only!).
  • XCode, if your'e on a Mac.

INSTALLING THE IDE

Believe me, although installing the IDE is pretty simple and straightforward, it is THE hardest step you are going to encounter while starting to learn programming. So pick up your sword and finish off this demon right away.

Here, I am going to give you a guide on how to download and install Visual Studio on your computer (No luck for Mac users here, we do not have a tutorial for XCode, but it should be pretty simple. Apple makes it so.).

Although we do not have a guide for Code::Blocks, that is also pretty straightforward. All you need to do is head on to their website, download the latest version of the software (binary release) for your OS, and install it.

[Download links for Code::Blocks and Xcode available at the end of this article]

STEP ONE:

Download the IDE. Head on to http://www.microsoft.com/visualstudio/eng/downloads.

Visiting their download page.
Visiting their download page.

Scroll down and expand Visual Studio Express 2012 for Windows Desktop. (Or any previous version if Visual Studio Express 2012 is not compatible with your computer. You can view the system requirements on the download page itself upon expanding.)

Expanding the download link.
Expanding the download link.

Select install now.

Clicking on the "Install Now" Link.
Clicking on the "Install Now" Link.

A small application will be downloaded. Run it, and a client will download and install the full software.

STEP TWO:

Register the software. When you run it, it should ask you to do so. You can continue to use it as a free trial for three months if you do not wish to pay. If you are a student, you can register for free.

STEP THREE:

Run the IDE. When you start the application, you shall see something like the below image. May be different, depending on the version you downloaded and your computer's background wallpaper.

Start Screen for Visual Studio Express 2012 for Windows Desktop.
Start Screen for Visual Studio Express 2012 for Windows Desktop.

BEGINNING WITH THE IDE

After a while, when the application loads (depending the speed of your machine), it will look something like this. May be different, depending on the version you downloaded.

Start Screen after loading.
Start Screen after loading.

Go to File → New Project.

Creating a new project.
Creating a new project.

From the left menu of the window that opens, expand Visual C++ and select Win32. In the main area of the window, select Win32 Console Application. Give a name to your project and let the wizard do the rest of the work.

Remove whatever is already there from the editor window/white box area for writing. It should now look like this.

The Place where you write your code.
The Place where you write your code.

Congratulations! You are now ready to start programming!

According to convention, whenever you start learning a new programming language, the first program you write is the Hello World! program. It is not necessary that you do, but it certainly helps you identify with the programming community. It is the most basic program and will help you initially grasp the language.

Hello World Program

Copy and paste the following code to your editor window.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
       cout << "Hello World!" << endl;
       return 0;
}

Press F5 to run your code (may differ for different compilers, but you should find Run button on the top tool bar or in one of the drop down menus.). Don't worry if the console window opens and quickly closes because our code does not take care of that. You should see Hello World! written in the brief moment that it is open.

EXPLANATION:
The first line, #include "stdafx.h" is Visual Studio exclusive. If you are using some other IDE, you should skip that. There is a way to disable it in Visual Studio also, but I would recommend letting it be. It kind of speeds up the compiling process, and when you will be writing huge programs, its going to be helpful.

The second line is #include <iostream>. This is known as a preprocessor directive, or a header file. All of C++'s features aren't built into the language itself, and need to be added to the program with the help of these header files. The iostream header file is one which you will be using almost all of the time.

The third line is using namespace std. Just remember it for now. You will learn more about namespaces as you learn the language.

The fourth line is int main(). This is a function in C++, and is the most important function (Since this hub is about getting you started, we won't be explaining functions in detail. You can find links for detailed explanation in the end). Although you can give functions your own name, every C++ program must have a main function, as execution begins from it. Whatever is inside this function will be executed, the rest will not. Even other functions have to be called from within the main function.
int is the function return type, and refers to integer. It is recommended that the return type of the main function be int. Notice that the body of the function is enclosed in curly braces.

The first statement inside the main() function is cout << "Hello World!" << endl;. Every C++ statement is terminated with a semicolon. The ones that aren't terminated by a semicolon aren't statements. cout stands for console out. It is used to print stuff on the screen. << is known as the insertion operator, and is used with cout. The words, Hello World!, which are to be printed are placed inside double quotes. endl is used to end the line (it stands for end line) and move the cursor to the next line.

Finally, we encounter the return statement. You will learn more about these late, but the return value should match the return type. Mostly 0 is given as the return value to indicate normal execution of the program, while negative values are used to indicate errors.

This will seem to be a bit difficult at first, but believe me, it isn't. I would recommend that you memorize this code. It seems difficult because it is unfamiliar. Become familiar with it, because after that you won't need to memorize code, you will write it yourself.

Coding Schedule

Okay, so the demons have been killed and you have finally started your programming journey. It is very important to have a nice coding schedule.

C++ is a language with which you will get familiar over time, so don't expect to gobble it all up in a day. Read tutorials or learn from a good book, and write each and every code you encounter (You don't need to copy the code. Just see what the output ought to be, and write a code for that. If you are stuck somewhere, then take help). Remember, the more you write your own code, the more you learn. Don't just settle with reading the tutorial until you write the code down for yourself.

Be regular, and you will definitely become a great programmer.

Bon Voyage!

~ Written by Lakshya Gupta @WnCoder

Comments

    0 of 8192 characters used
    Post Comment

    • Ceres Schwarz profile image

      Ceres Schwarz 3 years ago

      Good hub on getting started with C++. It can be difficult to start learning a programming language but this will be very useful and helpful to people that are interested in learning C++.

    • chadwiki profile image

      Chad Davis 3 years ago

      Very interesting article, thank you!

    Click to Rate This Article