Hey all, here is a basics tutorial for c++:
_________________________________
The basic syntax
c++ may seem like a scary language but in reality, its just like any other programming language, it has a main function and a proper syntax. For example, the most basic program in c++ is:
see! not so bad Laugh... Lets look at this piece by piece,
this is needed so that you can perform the basic functions of a program (print, if statements, input, etc.)
std is the standard 'namespace' in c++, this is also used in every program so that the basic functions of a program can be performed.
this is the main method, in java this would be public static void main(String[] args). As you can see, this function uses brackets to show where the code should go, these are called body-brackets, all your code should be inside the body brackets of functions.In addition, you can see the 'return 0;' inside this code brackets, this tells the compiler that whenever it gets to there that the code is finished and it should terminate the program with status report '0'. a ';' is placed at the end of the line to tell the compiler that there will be no more code on that line. The return statement IS NOT NEEDED in all programs.
_________________________________
Hello World!
c++ is like any normal programming language, it has a printing function, an input function, arrays, lists, etc. However, the first thing we will be learning is the printing function for c++:
the above code will print out "Hello World!!". In this line of code, 'cout' means "console-out" and << is telling the compiler that after the print statement (cout), text will need to be read. Futhermore, "endl" means 'end line' which creates a new line, you can also do this in the line of code with '\n':
Code:
both will come out with the following in your command prompt:
uoirghjeiogjwepigAutoGeneratedCommandPromptText4ryghurgijrg
Hello World!!
press any key to continue...
_________________________________
comments
A comment is a line within the code that the compiler does not read, it is made for the creator and any other people who should happen to see the source code. Comments are mainly used to explain what a line of code/ block of code does. For Example:
_________________________________
variables
Just like every programming language, c++ has some basic variables these are:
string - a line of text
int - a whole number
double - a number with decimals
float - an extremly long number with decimals
char - a single charachter, such as 'a' or '!'
All of these can be used together in the following way:
this will print out:
Code:
Hello, my name is Daniel and I am 15 and it is 7.59 o'clock!
this is because, we used the << operator to append (add) all the variables we created to the original string of text in front of the cout statement.
_________________________________
math
like anything in life, math is a large part of programming. In c++ the math operators are:
+ || addition
- || subtraction
* || multiplication
/ || division
% || get the remainder of something
< || less then
> || greater than
<= || less then or equal to
>= || greater than or equal to
this can used to make a simple calculator:
you can also do this within the print statement:
_________________________________
user input
User input is basically the user entering a string or a number and it/them being set as integers to be used in another time. To do this we use the 'cin' operator which means console-in.
this program will print out whatever name you specified. As you can see, instead of << like we use in cout, for cin we use >> and then the variable name.
_________________________________
calculator
Now we will be making a basic addition calculator program with user input.
_________________________________
That is all for part 1, in part 2 I will be covering loops, if statements and recursion.
I hope you liked this tutorial!