Cplusplus

From Redbrick Wiki
Revision as of 20:41, 5 December 2007 by Coconut (talk | contribs) (→‎g++)
Jump to navigation Jump to search

Under construction, LOL.

C++ is an object-oriented programming language, developed as a superset of the language C, which means that the language is, essentially, C with more features, hence the name. However, it has since evolved into a separate language and compatiblity with C is no longer guaranteed.

It is a very popular all-purpose language which is commonly utilised for game programming.

g++

The page Programming On Redbrick explains how to compile programs written in C++. Here we will go into more depth as to how the compiler g++ works.

A compiler works by translating your programming language (in this case, C++) into assembly language, which is the basic language that a processor understands. While C++ is a universal language (source code written on one architecture will generally work on another unless special libraries are used), programs must be recompiled if they are to be used on different architectures - for example, if you are compiling on murphy, which uses the UltraSPARC architecture, you will have to recompile your program if you wish to use it on carbon or deathray, which use the Intel x86 architecture, because UltraSPARC and x86 assembly languages are completely different.

g++ is the free, open-source C++ compiler which is included with many distributions of Linux as part of the gcc package, the latter of which is a C compiler. Other popular C++ compilers include Borland C++ and Microsoft Visual C++, however these are proprietary. gcc (and g++) is the most widely ported and universally available compiler in the world.

Getting started

To begin programming in C++ on RedBrick, you first have to log in. Once you've done that, you can create a new folder for your C++ files by utilising the following command

mkdir C++

This will create a directory called "C++" in your home directory. Open up this directory by typing

cd C++

Now you're ready to create your first C++ program. For this tutorial, we are going to use the nano editor, as it is simpler than the alternative, vim. Make a new C++ source file, that we will call "first", with

nano first.cpp

C++ source code files will generally use the .cpp extension - you may have noticed that C programs use .c

nano should be open, awaiting input. The first thing you should do is import the necessary libraries to allow input/output in your program. This is the only library we will use at this point. Make the following line 1 of your program

#include <iostream>

iostream (or, archaically, iostream.h) is a C++ header file, which is a file containing C++ code designed to be imported by multiple programs.

Now we want to create our main() function. main() is the first part of code that will be executed upon running your program. main() will typically look something like this

int main()
{
  // insert some code here
  
  return(0);
}

There are a couple of things that have to be explained here.

  • The function begins with int - an int in C++ means integer, which is simply a type of variable - in this case a number. Having the function declaration as int main() means that the function main() will return an int at the end of its execution. main() will always return an int.
  • Chain brackets { } are used to denote the code which main() contains. When you call main(), the code between these brackets will be executed.
  • // is used to make a comment. A comment is a line in your source code which will be completely ignored by the compiler. This is handy for explaining what your code does and you should quickly learn to use them frivilously.
  • return(0); is placed at the end of your main() function. The 0 is the int which is returned by the function - in this case, 0 means the program executed without any problems. Yay!!! The ; is used to mark the end of a line where a command is issued. If you omit this semicolon, the compiler will whinge at you.

So, at this point your program should look like this

#include <iostream>

int main()
{
  // insert some code here
  
  return(0);
}

The Programming On Redbrick page explains how to compile C++ programs. Just to recap, we'll go over it again (oh joy!) Press CTRL+O to bring up the save option, enter to save and then CTRL+X to exit.

g++ -o first first.cpp

Your program should compile. If it does, run it by entering

./first

Nothing will happen. That's because we haven't told the program to do anything!

Wow your ma with a basic program

Open up your source file again and delete the comment line. Now it's time to get stuck into some real code.

Change your program so it now reads

#include <iostream>

int main()
{
  std::cout << "This is my first real C++ program. Epic lulz!" << std::endl;
  
  return(0);
}

If you compile this, the program should execute and print this line onto the screen

This is my first real C++ program. Epic lulz!

Hurrah! Something useful and/or interesting!

  • cout is the C++ command for issuing an output. By default, this will go to the screen. Use left-facing arrow thingies (<<) to put your output into the cout command. You can link several things together by using multiple pairs of arrows as seen in the code, without having to issue cout twice.
  • endl moves output to the next line on the screen. It also flushes the stream... whatever that means.
  • std is the namespace which contains cout and endl. We could include the entire namespace in the code, but for now we don't need to since we're only using two of its functions - therefore we'll access them directly by putting std:: in front of cout and endl.
  • The text between the " " is called a string of characters. In C++, strings are always put between inverted commas like this.

To be updated later...