Jump to content

Cplusplus: Difference between revisions

1,210 bytes removed ,  5 December 2007
no edit summary
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]]
244

edits