Cplusplus: Difference between revisions

From Redbrick Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
OMG UNDER CONSTRUCTION LOL
''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.
'''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.
Line 75: Line 75:
Nothing will happen. That's because we haven't told the program to do anything!
Nothing will happen. That's because we haven't told the program to do anything!


Open up your source file again and
==Wow your ma with a basic program==


stands for Gnu C Compiler. It is a fairly popular UNIX C compiler and is based on the standard C Compiler, known as CC :o)
Open up your source file again and delete the comment line. Now it's time to get stuck into some real code.


Once you have written a program in nano or vim and saved it as a .c file (the c suffix indicates it is a c language file), you can compile it to create a program that you can run on redbrick. Here is an example in which I compile a file I saved as prog.c into a program called program :
Change your program so it now reads


  gcc -o program prog.c
  int main()
 
  {
If you want to ensure that the code you compile is ANSI compatible (that is that it fits the latest international standards for the way C code should be written), you can add the -ansi switch to the gcc command, so now you have :
  std::cout << "This is my first real C++ program. Epic lulz!" << std::endl;
 
 
  gcc -ansi -o program prog.c
  return(0);
 
  }
A lot of the time you will want to tidy up your code so that there aren't any warnings even when you compile it, to turn on warnings so you can see any possible errors in your code, add the -Wall switch to your gcc command, so now my example reads :
 
gcc -ansi -Wall -o program prog.c
 
Now that I have my program compiled and there were no errors, an ls on the directory I'm in will show up a file called program. To execute this program I type :
 
./program
 
To compile C++ programs, use :
 
g++ -o program prog.cpp
 
==Compiling and Running Java Programs==
 
In order to use Java, first login to Deathray, like so:
 
ssh deathray
 
The Java compiler on redbrick is called, unsurprisingly, javac. Compiling Java programs is a little more complex than compiling C programs as you need to have two environment variables set, JAVA_HOME and CLASSPATH. To see what these are set to, at the prompt type :
 
echo $JAVA_HOME
  echo $CLASSPATH
 
Echo is a command for printing stuff to the screen, what it does in the above example is "echo" the value of the JAVA_HOME and CLASSATH variables to the screen.
 
Your JAVA_HOME variable should be set to /usr/java and your CLASSPATH should be /usr/java/lib/dt.jar, if they're anything else you can edit the variables by typing the following :
 
JAVA_HOME=/usr/java
CLASSPATH=/usr/java/lib/dt.jar


Of course you can add extra directories to this, seperated by a : so that you can compile programs in your home directory. Another difference between javac and gcc is that the main class in your java program has to have the same name as the file it's in, and the file has to have a .java extension, for example :
If you compile this, the program should execute and print this line onto the screen


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


Then to execute the program (assuming it compiled okay ;o)) you type :
Hurrah! Something useful and/or interesting!


java program
* '''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.


This will execute the program.
To be updated later...


[[Category:Helpdesk]]
[[Category:Helpdesk]]

Revision as of 19:27, 5 December 2007

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 utilused 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.

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!) Save your program in nano by pressing CTRL+O (to bring up the save option) 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

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...