Intro to C++ Programming

Introduction | Text I/O | File I/O | Loops


Preparation
There are dozens of c and c++ compilers available. For beginners, I recommend Dev Bloodshed for windows. It's an excellent programming integrated development environment (IDE) that isn't bloated and bogged down with the useless features that plague eclipse and visual studio. After you're comfortable with programming, you may want to transition to using notepad++ with g++. Many of the advanced programmers use Linux and release their work as open source, so it would be to your benefit to program in a Linux environment since you will have a plethora of code to analyze and learn. You can either set up a dual-booting environment, install Linux on a computer, or run Linux in a virtualization program like VMware. To start programming press the "source file" button (see icon indicated in picture).



See the following table for information about the types of files involved in programming. The project file extension is dependent on the compiler. For dev c++, the extension is dev.
File TypeExtension(s)
Headerh, hpp, rh, hh
Resource Scriptrc
C source filec
C++ source filecpp, cc, cxx, c++, cp

Basic Code Layout
// libraries and files to include
#include <iostream> //used for text I/O
#include <fstream> //used for file I/O
#include <stdio.h>
#include <string.h>
#include <process.h>

using namespace std;

int main(){
	// code goes here
	system("pause");
	return 0;
}
The code given in future examples will replace the "code goes here" line. After creating a program, press F9 to compile and run your program.

#[command]
A command beginning with # is a preprocessor command that tells the compiler how your program should be compiled.

Conditional compilers: #if, #ifdef, #ifndef, #else, #elif and #endif

#define [identifier] [value]
#define PI 3.14 // this tells the compiler to replace all instances of "PI" in your .cpp file with "3.14"
#define double(a) ((a) * 2) // this tells the compiler to replace "double(a)" with "a*2"

#include [item] tells the compiler which headers and libraries you want to include.
#include <iostream> // will include iostream from the c++ standard library
#include <stdio.h> // will include stdio.h from the c standard library
#include "extra.h" // will include extra.h from the same folder as the file you are currently working on

using namespace std;
Namespaces contain a list of defined variables and functions and are used to organize information. When you use the std namespace, you are able to use commands from the standard c/c++ library. You can create your own namespace, but you should wait until you are more familiar with programming before doing so.

int main(){/*code*/}
This specifies the main function of the program. The contents in this function will be executed in the order they are specified.

Comments
Comments are lines of text that do not affect the functionality of your program. They are used to debug, explain the code and provide other relevant information.
// This line is a comment

/* This is another way to comment text.
In this case all the text between
the asterisks are comments */

system("[command]");
This sends the specified command to the OS, which obviously means your options are limited to those available by your OS. These commands are usually typed in cmd, terminal and other console applications. Remember that a system command you use in one operating system may not work for another. In windows, the "pause" command gives the result of "Press any key to continue...". Since console applications close after they are finished (unless you are executing the program in cmd), the system("pause") command is a useful way to let you see the result of your application before manually closing it. The system() function is not optimal for performance, so this should be used as a debugging tool. Once you are a proficient programmer I would suggest you use the console to see your output, or code an alternative function.

Hints
Don't forget to end a line of code with ";"
Use " instead of “

External Resources
Things to Avoid in C/C++

Introduction | Text I/O | File I/O | Loops
Share |



Tags: find, get, program, code, cpp, c, programming, how, info, information, change, computer, windows, xp, vista, 7

Contact Me | Donate | Terms of Service

Copyright © 2008-2011 Wartex8. All Rights Reserved.