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

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)