Intro to C Plus Plus

From KruelWiki

Jump to: navigation, search

Contents

Introduction to C++

C++ is the programming language we have chosen for Kruel's projects. We may use other languages at times, but if you want to help out, C++ is the language to use. This short guide will help you learn some of the basics of C++ and hopefully get you keen enough to want to learn more on your own. I'll assume you've got your computer installed with windows or linux. I'll focus on linux because i find it to be a much better development environment.

There are a number of great books out there which would be worth investing in, rather than relying on my incomplete tutorials! JonCruz ("chief Agitator") recently recommended Bruce Eckel's books. I'm a fan of deitel and deitel. See what your local library has!

Setting up your compiler

Most distributions of linux come with a compiler on the CD, although many are choosing not to install it unless instructed to. The compiler we use is GNU's C++ Compiler which is called 'g++'. To install it under a debian style distribution become root (thru the "su" command and the password) and type "apt-get install g++". On Ubuntu, by default there is no root password, so you will need to type "sudo apt-get install g++", then enter your password when prompted.

To test if you have the compiler installed, at a prompt type "g++ -v" and the last line should say something like:

gcc version 3.4.3 20041125 (Gentoo Linux 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7)

If you dont think your compiler is working then get some help.

Hello World!

Hello World is the classic "first program" for you to type out (or copy and paste into a file). Be sure to save the file as 'hello.cpp'

Code Listing: hello.cpp

#include <iostream>

using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}

Compiling and Running

In the location where you saved the file:

g++ -o hello hello.cpp

If everything compiled ok, to run the program type:

./hello

Explanation

#include <iostream>

This line tells the compiler (the software that converts source code to something the computer can understand) to give us the functionality provided in iostream. This is more or less just basic input output stuff, so you'll probably end up using it often.

using namespace std;

A namespace is grouping of objects and functions (I'll explain what they are later) to avoid conflicts between code written by different people. In this case we're using the 'std' (or Standard) namespace to access the functionality

int main() { 

This is what the computer runs when you run the program. You'll need one of these per program, and dont forget the closing brace '}' at the end of the program, or else your compiler will get confused.

cout << "Hello World!" << endl;

This is the meat of the program. 'cout' is short for 'console output' (others say 'common out' but thats another story). The << thingy (technically its called a 'stream insertion operator') tells the program that the term on the right of this, should be sent to whats on the left of it. In this case that means that "Hello World!" should be sent to console output.

The last terms '<< endl' simply mean to put an end of line on the end. Note the semicolon. This tells the compiler that you're finished with this line of code so that it can better understand what you're writing.

return 0;

Tell the operating system that there were no errors in running this program. When you write more advanced software you'll need to give consideration to telling the operating system if something goes wrong, for which you'd use 'return 1;' or any number higher than zero.

Code Review

  1. What happens (ie. what error message do you get) if you leave out the line containing:
    1. #include
    2. using namespace
    3. the closing brace at the end?
  2. Try to change the program to say "C++ is fun" or something equally corny. Which line do you change?
Personal tools