Cplusplus: Difference between revisions

Jump to navigation Jump to search
5,492 bytes added ,  6 December 2007
no edit summary
No edit summary
Line 3: Line 3:
'''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.


It is a very popular all-purpose language which is commonly utilised for game programming.
It is a very popular all-purpose language which is commonly utilised for game programming, and in many respects is quite similar to [[Java]], another object-oriented language.


==g++==
==g++==
Line 39: Line 39:
  int main()
  int main()
  {
  {
  // insert some code here
    // insert some code here
    
    
  return(0);
    return(0);
  }
  }


Line 60: Line 60:
  int main()
  int main()
  {
  {
  // insert some code here
    // insert some code here
    
    
  return(0);
    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.
The [[Programming On Redbrick]] page explains how to compile C++ programs. Just to recap, we'll go over it again (oh joy!) To indent your code as shown above, press TAB. (Proper indentation is essential programming practice.) Press CTRL+O to bring up the save option, enter to save and then CTRL+X to exit.


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


Line 103: Line 103:
To be updated later...
To be updated later...


[[Category:Helpdesk]]
==Variables and operators==
 
So, you've created your first C++ program. You must be so proud of yourself. Here's where the '''pain''' begins. We're moving on to variables and operators.
 
A variable is a data container which you assign a value and whose value can, by default, be changed during the execution of your program. That's why it's called a variable.
 
In C++, there are four variable types which you will use most
 
* '''int''' as explained earlier, is a number.
* '''char''' is a single ASCII character - a letter, a number or a symbol.
* '''double''' is a decimal (floating point) number, for example: 5.673 or 1.0
* '''string''' is just a string (array) of characters - you need to #include the string library for this to work as a data type.
 
There are other ones which you won't need as often
 
* '''short''' is, depending on the compiler, a number whose bit length is shorter (or sometimes) equal to an int.
* '''long''' has a bit length twice that of an int.
* '''float''' is a decimal (floating point) with half the precision of a double - shit, in other words. Avoid unless you're running on a system with memory measured in kilobytes and desperately need to conserve space, like the [[D6]]. God help you.
 
To initialise a variable such as an int, simply use the line
 
int a;
 
This reserves memory space for an int called a. But this isn't much good, is it? We need to give it a value. If you declare the variable like above, you can later assign a value like this
 
a = 5;
 
Now a has the value of 5, however, this is the n00bs' way of doing it. Why not declare and assign at the same time?
 
int a = 5;
 
That's more like it. You'll be a RB admin in no time. Let's run through initialising other variables.
 
int a = 5;
double b = 10.463;
char c = 'H';
string d = "Warning: Haskell may contain traces of anti-lulz.";
 
Hopefully you're getting the idea. Now that we're using string, we will have to start using the namespace std in our code for the sake of speed. Because of this, you will notice the absence of std:: which may make things easier for you.
 
We can throw the above code into a program and make it output the variables to the screen
 
#include <iostream>
#include <string> // Remember to include string if you're using a string!
using namespace std; // Also remember this!
int main()
{
    int a = 5;
    double b = 10.463;
    char c = 'H';
    string d = "Warning: Haskell may contain traces of anti-lulz.";
   
    cout << a << endl << b << endl << c << endl << d << endl;
   
    return(0);
}
 
Note how we can print four lines, with four different variables, using one cout command by having multiple <<
 
Also, see how string uses inverted commas " " whereas char uses two apostrophes ' ' - this is called being unnecessary complicated. But at least this isn't Haskell, so cheer up.
 
So, say you want to add two numbers together for some reason which makes sense to you. How? Simple. There are several ways to do it.
 
int a = 2;
int b = 6;
int c = a + b;
 
The value of c is now the values of a and b added together. If you don't want to bother with a third variable, you can just put the answer into a or b by replacing the third line with
 
a = a + b;
 
To output c to the screen
 
cout << c;
 
Or, without having to use c at all
 
cout << a + b;
 
This adds a and b together and outputs it to the screen all in one go. Hurrah!
 
You can also add doubles together in the same fashion.
 
Say you have a few chars and you want to combine them together in order to form a word, and then print it to the screen
 
char a = 'o';
char b = 'm';
char c = 'g';
char d = 'w';
char e = 'a';
char f = 'f';
cout << a + b + c + d + e;
 
This will print "omgwaf" on the screen.
 
Another way to do it, without using variables at all, is
 
cout << "omgwaf";
 
You can also waste everyone's RAMz if you want, by using a string for no justifiable reason. Say you've already declared the chars and assigned their values
 
string rofl = a + b + c + d + e;
cout << rofl;
 
Remember: a string is merely an array of characters so this works with strings too. You'll learn more about arrays later.
 
There are other basic operators in C++.
 
* '''+''' is the addition operator. It allows numbers to be added and chars and strings to be joined together.
* '''-''' is the subtraction operator. With it you can subtract numbers.
* '''*''' is multiplication. Multiply numbers.
* '''/''' is division. Divide numbers.
 
Say you want C++ to do your maths homework for you because you feel Michael Clancy is going to fail you for being such a newb.
 
int a = (5 * ((12/3) - 1)) + 25
 
Easy as [[undone]]'s mother.
 
This will divide 12 by 3, giving 4, then take 1 from it, giving 3, multiply that by 5, giving 15, and then add it to 25, resulting in 40, which is then assigned as the value for a. Try it yourself. Brackets are very handy for mathematical calculations in C++, just like in that yoke known as "real life".
 
C++ can also handle negative numbers, just as you would expect a calculator to, except this time the calculator can run 32 threads simultaneously, is about one hundred times bigger, ten thousand times louder, needs several hundred watts of power to function and only the five richest kings of Europe can afford one.
 
Perhaps that last part was slightly untrue.
[[Category:Helpdesk]
244

edits

Navigation menu