Printing & Structure
Printing
Use
coutto print something. Like this:
cout << "Hello World!";Or string things together...
cout << "The number is " << num1 << endl;(endl is for newlines)
Structure
A comment can be declared in C++ by using // at the start of a line.
Any line starting with # is a directive. This tells the compiler about a library it should include in the code (a bit like using import in Python). If we wanted to include the iostream library, we could use:
#include <iostream>A namespace is used to group functions and classes (etc) under a name. The standard C++ library is std, and we'll almost always need to use this one. But we could use other namespaces, if there was one called tramcrazy we'd just sub in that name.
If we want to use a certain namespace (usually the one called std) we can use:
using namespace std;The symbol ; is used at the end of lines in C++, and it is called a TERMINATOR!!!!!
It tells the compiler that the command is done.
Functions
To declare a function we use
int functionname ()
{
stuff goes here
return something;
}The main function is special!! This is where our program starts and it is automatically run on execution. It doesn't matter where it is in our code, it's always run first. Here's an example of using the main function with cout:
int main ()
{
cout << "Hello world";
return 0;
}