Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Library Index]

[View category: C++] [Discuss Article]

Intro to Basic C++ Programming

Article Rating: Excellent (# of votes: 1)
Author:      Obscurity
Submitted:      28-Apr-2007 19:41:03
Imported From:      The CyberArmy University (original author: Obscurity)


Introduction To Basic C++ Programming
C++ is probably one of the most rewarding languages to learn in the 21st century, more and more companies are using C++ to build programs, which means if you are an experience C++ programmer you will be making some really nice money. Ironically though, C++ has become the most popular programming language to this date. It has a rather easy syntax compared to most previous programming languages and C++ allows the user to be much more lenient on the code. Today though, we are going to need a compiler. A compiler is a program that will read the source code and create a program out of it. There are many compilers out for C++ but I suggest using either Microsoft VisualC++ or Dev-Cpp. Dev-Cpp is a free program that is probably the most user friendly on the market, Microsoft VisualC++ on the other hand, is not free but it does have many more functions and accessories available with it. I will be using Microsoft VC++, so I hope you will be to. Frankly, I'm not going to bore you with the history of C++ cause right now, in my opinion you should be working with codes. So without any further disruptions we will move onto Section 1.

[Note to Microsoft VisualC++ users] - Microsoft VisualC++ is a tad bit more difficult to start coding in, so I will help you open it up here. After installing Microsoft VisualC++ (hence forth known as VC++) open the program. Go to File - New. You will see an applet pop up, go to the Projects Tab, and click the Win32 Console Application. Find the Location you want to put it in, and dub it a name. You will next see another applet come up that says "Win32 Console Application Setup - Step 1 of 1" Click on Empy Project and click Finish. Finally you will see immediatly below the File tab, there is a small icon that looks like a piece of paper, open that up and this is where we will be writing our code. More about compiling and saving files a little bit later.

Section 1 - Hello World.

This is our first code, entitled Hello World. It's a rather easy code, but I will be explaining it in full detail. Remember do not copy the codes, type them out yourself, you learn much faster that way. I'm also assuming you have a compiler.
//Hello World
#include <iostream.h>
void main()
{
cout << "Hello World!";
}
Alright, that doesn't look too intimidating does it? Well don't worry I will be going over everything that is there, you will know everything thats on that program by the time I'm done with this chapter. Now befoe we start dissecting, the code in the red is what we call Source Code. The Source Code, also called Source, is the coding of the program. Alright let's do this. Go ahead and save the source code. The compiler is only going to allow you to save it as a txt file. So instead type this, HelloWorld.cpp The cpp extension tells the computer that this is going to be a c++ source file. Now if you are using Dev-Cpp, go ahead and compile it.

[Note to VC++ users] - You are going to need to save the code as a .cpp file. Next you need to look at your project, on the right hand side and there will be a tab on the bottom of the project saying File View. Go ahead and click on that. Go to Source Files, and right click. Click on Add Files, and add in the Source file. Next you will need to go to the Build tab on the main bar at the top and select Build Project1.Exe. After that, click on Execute File

Descriptions and Definitions of the Code

//Hello World

You see these slashs in front of the words Hello World, this is what is called a comment. Comments are used in coding as notes, the compiler ignores anything following the two slashs, and only the viewers of the source code are allowed to view it. In C++, there are two ways to use comments, the first way that I have shown you // and /* place words here*/. The first way, can only be used on one line, but the second way can be used for multiple lines. You will want to use comments when you start writing pages and pages of codes.

#include <iostream.h>

The #include command tells the compiler to include the iostream.h header file. Iostream means Input Output Stream, it contains the basic functions and commands such as Cout, Cin, etc. These things will be explained a little bit later. Other then that, all this says is to include this header file and all of its commands.

void main()

This was one of the harder things to completely understand once I entered the wonderful world of C++, but rather easy to understand once it's been broken down. Void Means that there will be no return of a number at the end of the main procedure. main() is our main procedure. C++ is broken into several procedures in more complex programs. main() tells the compiler that this is the begining of the main procedure.

{ & }

C++ is broken into statements, these curly brackets group up statements to have it become a procedure. Do not confuse these with the brackets of 0 and 9. On almost every program written you will use brackets multiple times in one procedure such as the If Else statements, and Loops. We will see more about If Else statements and Loops later.

cout << "Hello World!";

Ah yes, this is our program, this is what will make Hello World appear on our console application. Cout stands for Console Output; what this means is that the string Hello World! will be put on the program. Cout will be used to put characters on the screen. A string, is any item within quotation marks such as is Hello World. The << symbol represents a way of passing an item over to the cout instruction so that it can be printed on the screen. As you can see from the visual affect that its pointing towards cout, meaning that cout will be taking the string and putting it on the screen. Next we see the "Hello World!" string, everything that is not a datatype and wants to be put on the screen must be in quotation marks. Finally we see ;, the semicolon is a very odd thing. C++ is tolerant of different layouts, so if we do not put a semicolon at the end of the function/string then we will get a compiler error. We will see more about different layouts and datatypes in a later section.

Final Thoughts on Section 1

This is your first program, so if you get a compiler error then just go back and look at the code provided. See if there is anything you copied incorrectly. Study all the pieces of information provided and never give up. We have gone through basic functions, and statements. In the next section we will go through a little bit more advanced pieces of code, but also remember that the first section is always the hardest once you pass it, there is nothing stopping you from going on.

Section 2 - Integers and Console Input

Okay now we are going to move onto Integers and Cin (console input). This is a rather easy concept to grasp, and needs little explenation. First though, variables are things in a program or in math that need defining. They change between programs and problems rather then just a set rule. Today we are going to look at variables that are used in a C++ program. Usually if you have taken pre-algebra these concepts aren't that hard. Now lets look at a code using Integers Variables and Cin.
//This Program Adds Numbers
#include <iostream.h>
void main()
{ int first, second, result;
cout << "Please enter the first number here ";
cin >> first;
cout << "Please enter the second number here ";
cin >> second;
result = first + second;
cout << "The sum of the two numbers is " << result << endl;
}
int first, second, result;

This is the first new statement we see. First off Int stands for Integer. Integer is a type of datatype used for numbers only, it allows a very broad range of numbers. It allows from 32767 to -32767, but it does not allow decimal points. Int, is the basic datatype for adding, subtracting, multiplying, dividing and finding square roots. Next we see first, second, result; These are our variables. We can replace the words first, second, and result with whatever we want as long as these few rules aren't broken - It doesn't start with an underscore, it doesnt start with a number and as long as it doesn't contain a space.

cout << "Please enter the first number here ";

I know you're probably saying, why are you putting this back up here, we juts went over that in the last section. Well I'm putting this up here, because of one small tiny detail. As you can see if you notice closely, that right after the word here and before the quotation mark there is a space. We put this space here for entering a statement. Well in this program, we will be entering a number.

cin >> first; & cin >> second;

Now, as I have told you already Cin stands for Console input. Basically this means that rather then printing out the variable that's been printed, it takes it in and stores it. So if we use 3 as first and 2 as second, they will be stored in the console input until the time arrives were we print the variable or the variable sum. As you can see, cout and cin are as I call "Opposites" of each other.

result = first + second

Well as you can see this is a basic addition problem. But instead of putting numbers there, we put variables. Result is going to be our overall sum of the two variables provided by the user using the program. For example, if first = 5 and second = 6, the result will be 11. Result is also a variable, but used as the sum of the numbers.

cout << "The sum of the two numbers is " << result << endl;

Finally this is the last thing for this section. Now, this is our basic Console Output string, but with a few additions. As you can see the open space right before the quotation is there once again, this is for the allotment of variable to be printed.<< result passes itself to the previous string to be printed on the screen. So what we will see is not just the words, but also the sum of the first two variables. << endl; passes itself along as well, except endl actually means end this line and start a new one.

Final Thoughts on Section 2

Basically we have made a very very simple calculator that only adds, and only has room for three variables, one being the sum. But with this, we see how C++ can be used to make almost any mathematical function possible. For a note, on datatypes if you are looking for a datatype that will use decimals change int to float. Float only allows up to 8 decimal places.

Written by Obscurity (17 June 2003)

Member of CAUniversity staff

This article was originally published by CyberArmy.net in the CyberArmy Library.

You must be logged in to vote on an article

About Us | Privacy Policy | Mission Statement | Help