Home » Programming » Writing your first C++ program

Writing your first C++ program

In this tutorial for C++ we will create and run our first C++ program. Remember that the best way to start learning any programming language is through practice. So lets get started!

 

First thing we need to do is create our project file.

Open up Microsoft Visual C++ 2010 Express and browse through the following settings:

File -> New -> Project

draalin-Writing-your-first-C++-program-3

 

Select Win32 Console Application as we are using a simple command prompt to show our program.

draalin-Writing-your-first-C++-program-4

 

Click Finish.

draalin-Writing-your-first-C++-program-5

 

Remove all of the code that is currently sitting in the draalin-MyFirstProgram.cpp file.

draalin-Writing-your-first-C++-program-6

 

Now I will go over what this program contains below.

Comments

/*
This is a programmers block! 
*/

Anything in here is a comment, which can be used to create a programmers block. Very useful for anyone opening your program and wanting to understand exactly what it is about! 

Headers

#include "stdafx.h"

Is a preprocessor directive, meaning it tells the compiler to insert code from the required library functions. This  is required for the Microsoft Visual C++ 2010 Express edtion.

#include <iostream>

Is a preprocessor directive, meaning it tells the compiler to insert code from the required library functions. This one is used for input/output of screen messages.

Namespaces

using namespace std;

Contains all the classes, objects and functions of the standard C++ library that we need.

Functions

int main()
{
Code inside here
}

Every C/C++ program must contain the main() function. In most C++ programs you will put quiet a bit of your code in here.

Output stream

cout << "This is my first program";

Cout is a way to print messages to your command prompt. It requiress the <iostream> library functions as it is an object of them.

Return

return 0;

This is a requirement for the main() function. It means that if it executes this line of code your program has executed without any errors!

Example of the C++ program below

You can see a small example of a simple program that outputs “This is my first program” to your command prompt.

/*
Program Name: draalin-myFirstProgram.cpp
Developer: Tyler Bailey
Environment: Microsoft Visual C++ 2010 Express
Description: Simple output program
Variables: N/A
Constants: N/A
Functions: main
*/

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

using namespace std;

int main()
{
cout << "This is my first program";

cin.get();
cin.get();
return 0;
}

Paste in the following code above so it looks like the screenshot below!

draalin-Writing-your-first-C++-program-7

 

Now lets save our c++ program so we can run it after.

File -> Save draalin-myFirstProgram.cpp

draalin-Writing-your-first-C++-program-8

 

Now we can run our c++ program by doing the following:

Debug -> Start Debugging

draalin-Writing-your-first-C++-program-10

 

You can now see your program is running!

Press any to exit the prompt and go back to Microsoft Visual C++ 2010 Express.

draalin-Writing-your-first-C++-program-11

 

That is it, you have created and ran your first C++ program!

draalin-Writing-your-first-C++-program-12

 

If you require additional support on this tutorial or want some more help check out our forums found here.

 

Added gallery for additional support below for writing our first C++ program:

This slideshow requires JavaScript.

About Tyler Bailey

Tyler Bailey is a university student who owns and runs draalin.com. Follow me on