top-rated free essay

Cpp Learning an Effective Way

By omi785 May 28, 2013 5520 Words
|Chapter 0 | |Introduction / Getting Started | | |0.1 | |Introduction to these tutorials | | |0.2 | |Introduction to programming languages | | |0.3 | |Introduction to C/C++ | | |0.4 | |Introduction to development | | |0.5 | |Installing an Integrated Development Environment (IDE) | | |0.6 | |Compiling your first program | | |0.7 | |A few common C++ problems | |  | | | | |Chapter 1 | |C++ Basics | | |1.1 | |Structure of a program | | |1.2 | |Comments | | |1.3 | |A first look at variables (and cin) | | |1.4 | |A first look at functions | | |1.5 | |A first look at operators | | |1.6 | |Whitespace and basic formatting | | |1.7 | |Forward declarations | | |1.8 | |Programs with multiple files | | |1.9 | |Header files | | |1.10 | |A first look at the preprocessor | | |1.10a | |How to design your first programs | | |1.11 | |Comprehensive quiz | |  | | | | |Chapter 2 | |Variables Part I | | |2.1 | |Basic addressing and variable declaration | | |2.2 | |Keywords and naming identifiers | | |2.3 | |Variable sizes and the sizeof operator | | |2.4 | |Integers | | |2.5 | |Floating point numbers | | |2.6 | |Boolean Values | | |2.7 | |Chars | | |2.8 | |Constants | | |2.9 | |Hungarian Notation | | |2.10 | |Comprehensive quiz |

|Chapter 3 | |Operators | | |3.1 | |Precedence and associativity | | |3.2 | |Arithmetic operators | | |3.3 | |Increment/decrement operators, and side effects | | |3.4 | |Sizeof, comma, and arithmetic if operators | | |3.5 | |Relational operators (comparisons) | | |3.6 | |Logical operators | | |3.7 | |Converting between binary and decimal | | |3.8 | |Bitwise operators | | |3.x | |Comprehensive quiz | |  | | | | |Chapter 4 | |Variables Part II | | |4.1 | |Blocks (compound statements) and local variables | | |4.2 | |Global variables (and why they are evil) | | |4.3 | |File scope and the static keyword | | |4.4 | |Type conversion and casting | | |4.5 | |Enumerated types | | |4.6 | |Typedefs | | |4.7 | |Structs | |  | | | | |Chapter 5 | |Control Flow | | |5.1 | |Control flow introduction | | |5.2 | |If statements | | |5.3 | |Switch statements | | |5.4 | |Goto statements | | |5.5 | |While statements | | |5.6 | |Do while statements | | |5.7 | |For statements | | |5.8 | |Break and continue | | |5.9 | |Random number generation | |  | | | | |Chapter 6 | |Arrays, Strings, Pointers, and References | | |6.1 | |Arrays (Part I) | | |6.2 | |Arrays (Part II) | | |6.3 | |Arrays and loops | | |6.4 | |Sorting an array using selection sort | | |6.5 | |Multidimensional arrays | | |6.6 | |C-style strings | | |6.7 | |Introduction to pointers | | |6.8 | |Pointers, arrays, and pointer arithmetic | | |6.9 | |Dynamic memory allocation with new and delete | | |6.10 | |Pointers and const | | |6.11 | |References | | |6.12 | |References vs pointers, and member selection | | |6.13 | |Void pointers | |  | | | | |Chapter 7 | |Functions | | |7.1 | |Function parameters and arguments | | |7.2 | |Passing arguments by value | | |7.3 | |Passing arguments by reference | | |7.4 | |Passing arguments by address | | |7.4a | |Returning values by value, reference, and address | | |7.5 | |Inline functions | | |7.6 | |Function overloading | | |7.7 | |Default parameters | | |7.8 | |Function pointers | | |7.9 | |The stack and the heap | | |7.10 | |Recursion | | |7.11 | |Namespaces | | |7.12 | |Handling errors (assert, cerr, exit, and exceptions) | | |7.13 | |Command line arguments | | |7.14 | |Ellipses (and why to avoid them) | |  | | | | |Chapter 8 | |Basic object-oriented programming | | |8.1 | |Welcome to object-oriented programming | | |8.2 | |Classes and class members | | |8.3 | |Public vs private access specifiers | | |8.4 | |Access functions and encapsulation | | |8.5 | |Constructors | | |8.6 | |Destructors | | |8.7 | |The hidden “this” pointer | | |8.8 | |Constructors (Part II) | | |8.9 | |Class code and header files | | |8.10 | |Const class objects and member functions | | |8.11 | |Static member variables | | |8.12 | |Static member functions | | |8.13 | |Friend functions and classes | | |8.14 | |Anonymous variables and objects | |  | | | | |Chapter 9 | |Operator overloading | | |9.1 | |Introduction to operator overloading | | |9.2 | |Overloading the arithmetic operators | | |9.3 | |Overloading the I/O operators | | |9.4 | |Overloading the comparison operators | | |9.5 | |Overloading unary operators +, -, and ! | | |9.6 | |Overloading operators using member functions | | |9.7 | |Overloading the increment and decrement operators | | |9.8 | |Overloading the subscript operator | | |9.9 | |Overloading the parenthesis operator | | |9.10 | |Overloading typecasts | | |9.11 | |The copy constructor and overloading the assignment operator | | |9.12 | |Shallow vs. deep copying | |  | | | | |Chapter 10 | |Composition | | |10.1 | |Constructor initialization lists | | |10.2 | |Composition | | |10.3 | |Aggregation | | |10.4 | |Container classes | |  | | | | |Chapter 11 | |Inheritance | | |11.1 | |Introduction to inheritance | | |11.2 | |Basic inheritance in C++ | | |11.3 | |Order of construction of derived classes | | |11.4 | |Constructors and initialization of derived classes | | |11.5 | |Inheritance and access specifiers | | |11.6 | |Adding, changing, and hiding members in a derived class | | |11.7 | |Multiple inheritance | | |11.8 | |Virtual base classes | |  | | | | |Chapter 12 | |Virtual Functions | | |12.1 | |Pointers and references to the base class of derived objects | | |12.2 | |Virtual functions | | |12.3 | |Virtual destructors, virtual assignment, and overriding virtualization | | |12.4 | |Early binding and late binding | | |12.5 | |The virtual table | | |12.6 | |Pure virtual functions, abstract base classes, and interface classes | |  | | | | |Chapter 13 | |Input and output (I/O) | | |13.1 | |Input and output (I/O) streams | | |13.2 | |Input with istream | | |13.3 | |Output with ostream and ios | | |13.4 | |Stream classes for strings | | |13.5 | |Stream states and input validation | | |13.6 | |Basic file I/O | | |13.7 | |Random file I/O | |  | | | | |Chapter 14 | |Templates | | |14.1 | |Function templates | | |14.2 | |Function template instances | | |14.3 | |Template classes | | |14.4 | |Expression parameters and template specialization | | |14.5 | |Class template specialization | | |14.6 | |Partial template specialization | |  | | | | |Chapter 15 | |Exceptions | | |15.1 | |The need for exceptions | | |15.2 | |Basic exception handling | | |15.3 | |Exceptions, functions, and stack unwinding | | |15.4 | |Uncaught exceptions, catch-all handlers, and exception specifiers | | |15.5 | |Exceptions, classes, and inheritance | | |15.6 | |Exception dangers and downsides | |  | | | | |Chapter 16 | |The Standard Template Library | | |16.1 | |The Standard Template Library (STL) | | |16.2 | |STL containers overview | | |16.3 | |STL iterators overview | | |16.4 | |STL algorithms overview | |  | | | | |Chapter 17 | |std::string | | |17.1 | |std::string and std::wstring | | |17.2 | |std::string construction and destruction | | |17.3 | |std::string length and capacity | | |17.4 | |std::string character access and conversion to C-style arrays | | |17.5 | |std::string assignment and swapping | | |17.6 | |std::string appending | | |17.7 | |std::string inserting | |  | | | | |Appendix A | |Miscellaneous Subjects | | |A.1 | |Static and dynamic libraries | | |A.2 | |Using libraries with Visual Studio Express 2005 | | |A.3 | |Using libraries with Code::Blocks | | |A.4 | |Debugging your program (stepping and breakpoints) | | |A.5 | |Debugging your program (watching variables and the call stack) | | |A.6 | |Fixed-width integers | |  | | | | |Appendix B | |C++11 (Formerly C++0x) | | |B.1 | |Introduction to C++11 | | |B.2 | |Long long, auto, decltype, nullptr, and enum classes | | |B.3 | |Range-based for statements and static_assert | | |B.4 | |Initializer lists and uniform initialization | | |B.5 | |Delegating constructors | | |B.6 | |New virtual function controls (override, final, default, and delete) |

0.1 — Introduction to these tutorials
BY ALEX, ON MAY 27TH, 2007

Welcome to the C++ tutorials! The tutorials in this section are aimed primarily at beginning level programmers, including those who have little to no prior programming experience. Intermediate level programmers will probably also find plenty of tips and tricks that may be of use in improving their programming skills.

Each tutorial is broken down into sections, with each section containing a number of related subtopics. There is no suggested amount of time that you should spend with each section; progress through the material at a pace that is comfortable for you. Since the concepts from each section tend to build on those from previous sections, it is a good idea to ensure you generally understand each section before you move on.

The entire series of LearnCpp tutorials is provided free to you. We are hosted on a virtual private server to ensure these tutorials are consistently available any time you need them!

Before we get started, lets hit on a couple of important goals of these tutorials:

1) Cover programming topics as well as C++. Traditional textbooks do a pretty good job of teaching the basics of a given programming language, but they often do not cover relevant programming topics that are incidental to the language. For example, books will omit sections on programming style, common pitfalls, debugging, good/bad programming practices, and testing. Consequently, by the time you finish the book, you understand how to program in a language, but you have a ton of bad habits that will come back to bite you later! One of the goals of these tutorials is to make sure that all of these incidental topics are covered along the way, in the sections where it naturally makes sense to discuss them. When you finish, you will not only know how to program in C++, you will know how NOT to program in C++, which is arguably as important.

2) Provide a lot of examples. Most people learn as much or more from following the examples as they do from reading the text. It is often the case that the text will describe the theory of something, but the examples show how to apply it. This tutorial will endeavor to provide plenty of clear, concise examples to ensure that you are able to apply the concepts you’re learning. We will also avoid the twin evils: the magic hand wave (also known as …), where in the interest of space part of an example is omitted, and the unexplained new concept, where a new concept that is integral to the example is introduced without any mention of what it is or how it works. Both of these tend to lead to “getting stuck”, which makes moving on to the next concept rather difficult.

3) Provide practice programs. Unlike riding a bicycle, programming topics will fade from memory if not practiced. We strongly encourage you to not only read the examples, but compile and run them to see the result. If you are unsure how they work, use the debugger to walk through them line by line (we will cover how to do that in the appendix). Also, you can learn a lot by modifying the examples and recompiling them to see what happens. However, the best way to learn is to write your own program utilizing the concepts that you’ve learned. To encourage you to practice, the end of each section will contain some exercises that you can attempt to answer on your own, along with solutions. You can compare your solution against ours to see what we did differently, or, if you get stuck, how we solved the problem. Then you can go back and refocus on the areas you need more work on.

4) Most importantly: have fun. Programming can be a lot of fun, and if you’re not generally having fun, you’re not in the right mindset to be programming. Tired or unhappy programmers make mistakes, and debugging code tends to take much longer than writing it correctly in the first place! Often you can save yourself some time by going to bed, getting a good night’s sleep, and coming back to a problem in the morning. Tired or unhappy tutorial writers have many of the same issues. :)

Alright, let’s get on with it.

0.2 — Introduction to programming languages
BY ALEX, ON MAY 27TH, 2007

Todays computers are incredibly fast, and getting faster all the time. Yet with this speed comes some significant constraints. Computers only natively understand a very limited set of instructions, and must be told exactly what to do. The set of instructions that tells a computer what to do is known as software. The computer machinery that executes the instructions is the hardware.

A computer’s CPU is incapable of speaking C++. The very limited set of instructions that a CPU natively understands is called machine code, ormachine language, or an instruction set. How these instructions are organized is beyond the scope of this introduction, but it is interesting to note two things. First, each instruction is composed of a number of binary digits, each of which can only be a 0 or a 1. These binary numbers are often called bits (short for binary digit). For example, the MIPS architecture instruction set always has instructions that are 32 bits long. Other architectures (such as the x86, which you are likely using) have instructions that can be a variable length.

For example, here is a x86 machine language instruction: 10110000 01100001

Second, each set of binary digits is translated by the CPU into an instruction that tells it to do a very specific job, such ascompare these two numbers, or put this number in that memory location. Different types of CPUs will typically have different instruction sets, so instructions that would run on a Pentium 4 would not run on a Macintosh PowerPC based computer. Back when computers were first invented, programmers had to write programs directly in machine language, which was a very difficult and time consuming thing to do.

Because machine language is so hard to program with, assembly language was invented. In an assembly language, each instruction is identified by a short name (rather than a set of bits), and variables can be identified by names rather than numbers. This makes them much easier to read and write. However, the CPU can not understand assembly language directly. Instead, it must be translated into machine language by using an assembler. Assembly languages tend to be very fast, and assembly is still used today when speed is critical. However, the reason assembly language is so fast is because assembly language is tailored to a particular CPU. Assembly programs written for one CPU will not run on another CPU. Furthermore, assembly languages still require a lot of instructions to do even simple tasks, and are not very human readable.

Here is the same instruction as above in assembly language: mov al, 061h

To address these concerns, high-level programming languages were developed. C, C++, Pascal, Ada, Java, Javascript, and Perl, are all high level languages. Programs written in high level languages must be translated into a form that the CPU can understand before they can be executed. There are two primary ways this is done: compiling and interpreting.

A compiler is a program that reads code and produces a stand-alone executable that the CPU can understand directly. Once your code has been turned into an executable, you do not need the compiler to run the program. Although it may intuitively seem like high-level languages would be significantly less efficient than assembly languages, modern compilers do an excellent job of converting high-level languages into fast executables. Sometimes, they even do a better job than human coders can do in assembly language!

Here is a simplified representation of the compiling process: [pic]

An interpreter is a program that reads code and essentially compiles and executes (interprets) your program as it is run. One advantage of interpreters is that they are much easier to write than compilers, because they can be written in a high-level language themselves. However, they tend to be less efficient when running programs because the compiling needs to be done every time the program is run. Furthermore, the interpreter is needed every time the program is run.

Here is a simplified representation of the interpretation process: [pic]

Any language can be compiled or interpreted, however, traditionally languages like C, C++, and Pascal are compiled, whereas “scripting” languages like Perl and Javascript are interpreted. Some languages, like Java, use a mix of the two.

High level languages have several desirable properties. First, high level languages are much easier to read and write.

Here is the same instruction as above in C/C++: a = 97;

Second, they require less instructions to perform the same task as lower level languages. In C++ you can do something like a = b * 2 + 5; in one line. In assembly language, this would take 5 or 6 different instructions.

Third, you don’t have to concern yourself with details such as loading variables into CPU registers. The compiler or interpreter takes care of all those details for you.

And fourth, they are portable to different architectures, with one major exception, which we will discuss in a moment.


The exception to portability is that many platforms, such as Microsoft Windows, contain platform-specific functions that you can use in your code. These can make it much easier to write a program for a specific platform, but at the expense of portability. In these tutorials, we will explicitly point out whenever we show you anything that is platform specific.

0.3 — Introduction to C/C++
BY ALEX, ON MAY 27TH, 2007

The C language was developed in 1972 by Dennis Richie at Bell Telephone laboratories, primarily as a systems programming language. That is, a language to write operating systems with. Richie’s primary goals were to produce a minimalistic language that was easy to compile, allowed efficient access to memory, produced efficient code, and did not need extensive run-time support. Despite being a fairly low-level high-level language, it was designed to encourage machine and platform independent programming.

C ended up being so efficient and flexible that in 1973, Ritchie and Ken Thompson rewrote most of the UNIX operating system using C. Many previous operating systems had been written in assembly. Unlike assembly, which ties a program to a specific CPU, C’s excellent portability allowed UNIX to be recompiled on many different types of computers, speeding it’s adoption. C and Unix had their fortunes tied together, and C’s popularity was in part tied to the success of UNIX as an operating system.

In 1978, Brian Kernighan and Dennis Ritchie published a book called “The C Programming Language”. This book, which was commonly known as K&R (after the author’s last names), provided an informal specification for the language and became a de facto standard. When maximum portability was needed, programmers would stick to the recommendations in K&R, because most compilers at the time were implemented to K&R standards.

In 1983, the American National Standards Institute (ANSI) formed a committee to establish a formal standard for C. In 1989 (committees take forever to do anything), they finished, and released the C89 standard, more commonly known as ANSI C. In 1990 the International Organization for Standardization adopted ANSI C (with a few minor modifications). This version of C became known as C90. Compilers eventually became ANSI C/C90 compliant, and programs desiring maximum portability were coded to this standard.

In 1999, the ANSI committee released a new version of C called C99. It adopted many features which had already made their way into compilers as extensions, or had been implemented in C++.

C++ (pronounced see plus plus) was developed by Bjarne Stroustrup at Bell Labs as an extension to C, starting in 1979. C++ was ratified in 1998 by the ISO committee, and again in 2003 (called C++03, which is what this tutorial will be teaching). A new version of the standard, known as C++11 has been made available since the time these tutorials were written — updates to the tutorial to cover C++11′s additions will be made in the appendix.

The underlying design philosophy of C and C++ can be summed up as “trust the programmer” — which is both wonderful, because the compiler will not stand in your way if you try to do something unorthodox that makes sense, but also dangerous, because the compiler will not stand in your way if you try to do something that could produce unexpected results. That is one of the primary reasons why knowing how NOT to code C/C++ is important — because there are quite a few pitfalls that new programmers are likely to fall into if caught unaware.

C++ adds many new features to the C language, and is perhaps best thought of as a superset of C, though this is not strictly true as C99 introduced a few features that do not exist in C++. C++’s claim to fame results primarily from the fact that it is an object-oriented language. As for what an object is and how it differs from traditional programming methods, well, we’ll cover that in just a few sections.

0.4 — Introduction to development
BY ALEX, ON MAY 27TH, 2007

Before we can write and execute our first program, we need to understand in more detail how programs get developed. Here is a graphic outlining a simplistic approach:


Step 1: Define the problem that you would like to solve.

This is the “what” step, where you figure out what you are going to solve. Coming up with the initial idea for what you would like to program can be the easiest step, or the hardest. But conceptually, it is the simplest. All you need is a an idea that can be well defined, and you’re ready for the next step.

Step 2: Determine how you are going to solve the problem.

This is the “how” step, where you determine how you are going to solve the problem you came up with in step 1. It is also the step that is most neglected in software development. The crux of the issue is that there are many ways to solve a problem — however, some of these solutions are good and some of them are bad. Too often, a programmer will get an idea, sit down, and immediately start coding a solution. This almost always generates a solution that falls into the bad category.

Typically, good solutions have the following characteristics: * They are straightforward
* They are well documented
* They can be easily extended (to add new features that were not originally anticipated) * They are modularized

The problem is largely with the third and fourth bullets — while it’s possible to generate programs that are straightforward and well documented without using a lot of forethought, designing software that is extensible and sufficiently modularized can be a much tougher challenge.

As far as extensibility goes, when you sit down and start coding right away, you’re typically thinking “I want to do _this_”, and you never consider that tomorrow you might want to do _that_. Studies have shown that only 20% of a programmers time is actually spent writing the initial program. The other 80% is spent debugging (fixing errors) or maintaining (adding features to) a program. Consequently, it’s worth your time to spend a little extra time up front before you start coding thinking about the best way to tackle a problem, and how you might plan for the future, in order to save yourself a lot of time and trouble down the road.

Modularization helps keep code understandable and reusable. Code that is not properly modularized is much harder to debug and maintain, and also harder to extend later. We will talk more about modularization in the future.

Step 3: Write the program

In order the write the program, we need two things: First we need knowledge of a programming language — that’s what these tutorials are for! Second, we need an editor. It’s possible to write a program using any editor you want, be it Window’s notepad or Linux’s gedit. However, we strongly urge you to use an editor that is designed for coding.

A typical editor designed for coding has a few features that make programming much easier, including:

1) Line numbering. Line numbering is useful when the compiler gives us an error. A typical compiler error will state “error, line 64″. Without an editor that shows line numbers, finding line 64 can be a real hassle.

2) Syntax highlighting and coloring. Syntax highlighting and coloring changes the color of various parts of your program to make it easier to see the overall structure of your program.

3) An unambiguous font. Non-programming fonts often make it hard to distinguish between the number 0 and the letter O, or between the number 1, the letter l (lower case L), and the letter I (upper case i). A good programming font will differentiate these symbols in order to ensure one isn’t accidentally used in place of the other.

Your C++ programs should be called name.cpp, where name is replaced with the name of your program. The .cpp extension tells the compiler (and you) that this is a C++ source code file that contains C++ instructions. Note that some people use the extension .cc instead of .cpp, but we recommend you use .cpp.

Also note that many complex C++ programs have multiple .cpp files. Although most of the programs you will be creating initially will only have a single .cpp file, it is possible to write single programs that have tens if not hundreds of individual .cpp files.

Step 4: Compiling

In order to compile a program, we need a compiler. The job of the compiler is twofold: 1) To check your program and make sure it follows the syntactical rules of the C++ language: 2) To take your source code as input and produce a machine language object file as output. Object files are typically namedname.o or name.obj, where name is the same name as the .cpp file it was produced from. If your program had 5 .cpp files, the compiler would generate 5 object files.


For illustrative purposes only, most Linux and Mac OS X systems come with a C++ compiler called g++. To use g++ to compile a file from the command line, we would do this:

"g++" -c file1.cpp file2.cpp file3.cpp *

This would create file1.o, file2.o, and file3.o. The -c means “compile only”, which tells g++ to just produce .o files.

Other compilers are available for Linux, Windows, and just about every other system. We will discuss installing a compiler in the next section, so there is no need to do so now.

For complex projects, some development environments use a makefile, which is a file that tells the compiler which files to compile. Makefiles are an advanced topic, and entire books have been written about them. We will not discuss them here.

Step 5: Linking

Linking is the process of taking all the object files for a program and combining them into a single executable.


In addition to the object files for a program, the linker includes files from the runtime support library. The C++ language itself is fairly small and simple. However, it comes with a large library of optional components that may be utilized by your program, and these components live in the runtime support library. For example, if you wanted to output something to the screen, your program would include a special command to tell the compiler that you wanted to use the I/O (input/output) routines from the runtime support library.

Once the linker is finished linking all the object files (assuming all goes well), you will have an executable file.

Again, for illustrative purposes, to link the .o files we created above on a Linux or OS X machine, we can again use g++:

g++ -o prog file1.o file2.o file3.o

The -o tells g++ that we want an executable file named “prog” that is built from file1.o, file2.o, and file3.o

The compile and link steps can be combined together if desired:

g++ -o prog file1.cpp file2.cpp file3.cpp

Which will combine the compile and link steps together and directly produce an executable file named “prog”.

Step 6: Testing and Debugging

This is the fun part (hopefully)! You are able to run your executable and see whether it produces the output you were expecting. If not, then it’s time for some debugging. We will discuss debugging in more detail soon.

Note that steps 3, 4, 5, and 6 all involve software. While you can use separate programs for each of these functions, a software package known as an integrated development environment (IDE) bundles and integrates all of these features together. With a typical IDE, you get a code editor that does line numbering and syntax highlighting. The IDE will automatically generate the parameters necessary to compile and link your program into an executable, even if it includes multiple files. And when you need to debug your program, you can use the integrated debugger. Furthermore, IDE’s typically bundle a number of other helpful editing features, such as integrated help, name completion, a class hierarchy browser, and sometimes a version control system.

We will talk more about installing and using IDEs in the next section.

0.5 — Installing an Integrated Development Environment (IDE) BY ALEX, ON MAY 28TH, 2007

As mentioned in the previous section, an Integrated Development Environment (IDE) contains all of the things you need to develop, compile, link, and debug your programs. So let’s install one.

The obvious question is, “which one?”. Keep in mind that you can install multiple IDEs, so there is no “wrong decision” here. During the course of these tutorials, we will be showing you some of the nice features of your IDE, such as how to do integrated debugging. All of our examples will be done using both Microsoft’s Visual C++ 2005 Express Edition, and Code::Blocks. Thus we highly recommend you pick one of these.

However, if you would like to try your hand at another compiler, you are free to do so. The concepts we show you will work for any IDE — however, different IDE’s use different keymappings and different setups, and you may have to do a bit of searching to find the equivalent of what we show you.


If you are developing on a Windows machine (as most of you are), then we highly recommend Microsoft’s free Visual C++ 2010 Express Edition. The installer that you download off of Microsoft’s web page is actually a downloader. When you run it, it will download the actual IDE from Microsoft.

Note: This tutorial was originally written when Microsoft was distributing the 2005 version of Visual C++. Consequently, all references and screenshots are targeted to that version. Running the 2008 or 2010 versions are fine, however, your screens may look slightly different.


MSDN express will give you documentation about C/C++ functions that you will refer to often. If you do not install MSDN Express on your hard drive, then Visual C++ will use the online version at

The installer will likely ask you to reboot your computer after it has finished downloading the components. When it does, your computer may appear to “hang” temporarily. Don’t panic, it’s just installing stuff. Once you are back into Windows, you may need to run the installer again. Choose the same options, and it will seem like it’s going to download everything again, but it won’t. It will install the program.

Linux or Windows

If you are developing on Linux (or you are developing on Windows but want to write programs that you can easily port to Linux), we recommend Code::Blocks. Code::Blocks is a free, open source, cross-platform IDE that will run on both Linux and Windows.

Windows users, make sure to get the version with MinGW bundled.

Alternately, some people prefer to use Bloodshed’s Dev-C++, which also runs on both Windows and Linux.


Mac users can use Xcode if it is available to you, or Eclipse. Eclipse is not set up to use C++ by default, and you will need to install the optional C++ components.

Moving on

Once your IDE is installed (which is one of the hardest things this tutorial will ask you to do), you are ready to write your first program!

0.6 — Compiling your first program
BY ALEX, ON MAY 28TH, 2007

Before we can write our first program (which we will do very soon), we need to know two things about development environments.

First, although our programs will be written inside .cpp files, the .cpp files themselves will be added to a project. Some IDEs call projects “workspaces”, or “solutions”. The project stores the names of all the code files we want to compile, and also saves various IDE settings. Every time we reopen the project, it will restore the state of the IDE to where we left off. When we choose to compile our program, the project tells the compiler and linker which files to compile and link. It is worth noting that project files for one IDE will not work in another IDE.

Second, there are different kinds of projects. When you create a new project, you will have to pick a project type. All of the projects that we will create in this tutorial will be console projects. A console project means that we are going to create programs that can be run from the dos or linux command-line. By default, console applications have no graphical user interface (GUI) and are compiled into stand-alone executable files. This is perfect for learning C++, because it keeps the complexity to a minimum.

Traditionally, the first program programmers write in a new language is the infamous hello world program, and we aren’t going to deprive you of that experience! You’ll thank us later. Maybe.

A quick note about examples containing code

Starting with this lesson, you will see many examples of C++ code presented. Most of these examples will look something like this:

|1 |#include | |2 |  | |3 |int main() | |4 |{ | |5 |    using namespace std; | |6 |    cout

Cite This Document

Related Documents

  • Effective Ways Of Learning Japanese Fin

    ...Effective Ways of Learning Japanese As a student in the Department of Foreign Language and Literature (DFLL) in NTU, you major in literature, linguistics, and languages. In your first year of the university, it’s necessary for you to learn a second language (while some students who are fond of languages may learn their third language, fourt...

    Read More
  • Lecture Classes: Not an Effective Way of Learning

    ...098 1st of November Lecture Classes: Not an Effective Way of Learning In the essay, “College Lectures: Is anybody Listening?,” written by David Daniels, he suggests that college lecture classes should be replaced by classes that provide more of an active learning environment. He also points out that the lecture system is outdated, becaus...

    Read More
  • Effective Ways to Foster Learning

    ...My Research Paper: Effective ways to foster learning Luther H. Graham III 5/19/2011 Dr. C Strayer University Abstraction The article written by Jialin Yi was about effective ways to foster learning. The article was written in January of 2005. According to Yi, learning is viewed as an object or a process; some view it as both of them. T...

    Read More
  • The Way of Learning

    ...Violet Juarez David Paonessa English 802 2-28-12 Essay #3: Final Draft The Way of Learning Throughout these past two semesters I have taught myself how to write better by learning from my mistakes. Coming to a realization that the more I write the more experience and knowledge I gain. By taking the time to analyze and look back on previ...

    Read More
  • Effective Way of Tutoring

    ...Effective way in Tutoring Students 2004 Outline: A) Introduction B) General discussion 1) Personal Experience 2) Tips on Promoting Student Motivation 3) Advantages and Disadvantages of Tutoring C) Conclusion. Foreword: Tutoring is a tool in helping the children in their academic needs. The tutor guides them in answering their a...

    Read More
  • Requirements of an Effective Learning Organization

    ...| 9/9/2012 | Requirements for an Effective Learning Organization My philosophy of an effective learning organization is one in which there is a culture of ongoing training and sharing of information to assist individuals in growing and reaching their potential. Employees are empowered to share ideas and take as many risks they can handle,...

    Read More
  • Learning as a Way of Life

    ...What I Learned “We learn more by looking for the answer to a question and not finding it than we do from learning the answer itself.” People go through their whole lives trying to figure out who they are, look for a sense of purpose or belonging, or attempt to discover the right path for them-self. The lucky ones have somehow found wh...

    Read More
  • “Learning the significance of Effective Communication”

    ... Francis Jose Lean L. Abayata LHS-4 “Learning the significance of Effective Communication” Effective communication is important because it is a root for good relationship building and a fundamental tool for learning. Communication is a vital aspect that everyone must possess. Communication happens when the process of sharing ideas, mes...

    Read More

Discover the Best Free Essays on StudyMode

Conquer writer's block once and for all.

High Quality Essays

Our library contains thousands of carefully selected free research papers and essays.

Popular Topics

No matter the topic you're researching, chances are we have it covered.