Login Register


C++ basics part 1 filter_list
Author
Message
C++ basics part 1 #1
Before I begin i'd like to say this is 100% MINE i am just transferring it from SF


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:


Code:
#include <iostream> using namespace std; int main() { return 0; }

see! not so bad Laugh... Lets look at this piece by piece,


Code:
#include <iostream>
this is needed so that you can perform the basic functions of a program (print, if statements, input, etc.)


Code:
using namespace std;

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.

Code:
int main() { return 0; }

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++:


Code:
#include <iostream> using namespace std; int main() { cout << "Hello World!!"<<endl; }

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:
Code:
#include <iostream> using namespace std; int main() { cout << "Hello World!!\n"; }

both will come out with the following in your command prompt:

uoirghjeiogjwepigAutoGeneratedCommandPromptText4ryghurgijrg
Hello World!!

press any key to continue...
_________________________________
comments


Code:
#include <iostream> using namespace std; int main() { //this is a one line comment /* This * Is * A * Multi * Line Comment */ }

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:


Code:
#include <iostream> using namespace std; int main() { cout << "Hello World!!";// this will print out hello world /* and it will look like this: * Hello World!! */ }
_________________________________
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:


Code:
#include <iostream> using namespace std; int main() { int age = 15; double time = 7.59; string name = "Daniel"; char exclamation = '!'; cout << "Hello, my name is " <<name<<" and I am "<<age<< " and it is "<<time<< " o'clock"<<exclamation<<'\n'; }
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:


Code:
#include <iostream> using namespace std; int main() { int add1 = 1; int add2 = 2; int mult1 = 5; int mult2 = 5; int div1 = 6; int div2 = 3; int rem1 = 12; int rem2 = 4; int sum = add1 + add2; int multans = mult1 * mult2; int divans = div1 / div2; int remans = rem1 % rem2; cout << sum<<endl;//3 cout << multans<<endl;//25 cout << divans<<endl;//2 cout << remans<<endl;//0 }

you can also do this within the print statement:


Code:
#include <iostream> using namespace std; int main() { int add1 = 1; int add2 = 2; int mult1 = 5; int mult2 = 5; int div1 = 6; int div2 = 3; int rem1 = 12; int rem2 = 4; cout << "sum is "<< add1 + add2<<endl; cout << "multans is "<< mult1 * mult2<<endl; cout << "divans is "<< div1 / div2<<endl; cout << "remans is "<< rem1 % rem2<<endl; cout <<"1+1 is "<<1 + 1<<endl; cout <<"2*2 is "<<2 * 2<<endl; cout <<"4/2 is "<<4 / 2<<endl; cout <<"12%5 is "<<12 % 5<<endl; }
_________________________________
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.
Code:
#include <iostream> using namespace std; int main() { string name; cout << "Enter name: "; cin >> name; cout << "Hello "<<name; }

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.


Code:
#include <iostream> using namespace std; int main() { int firstnum; int secondnum; cout << "enter first number: "; cin >> firstnum; cout << "enter second number: "; cin >> secondnum; int sum = firstnum + secondnum; cout << "the sum of "<<firstnum<<" and "<<secondnum<< " is "<<sum<<endl; }
_________________________________

That is all for part 1, in part 2 I will be covering loops, if statements and recursion.

I hope you liked this tutorial!

Reply





Messages In This Thread
C++ basics part 1 - by Earthly Minds - 06-18-2013, 03:12 AM
RE: C++ basics part 1 - by Nil - 06-18-2013, 05:49 AM
RE: C++ basics part 1 - by w00t - 06-18-2013, 06:40 AM
RE: C++ basics part 1 - by Charon - 06-18-2013, 01:44 PM
RE: C++ basics part 1 - by w00t - 06-18-2013, 05:51 PM
RE: C++ basics part 1 - by Dong - 09-14-2013, 12:30 AM
RE: C++ basics part 1 - by Cyanide and Cynicism - 09-14-2013, 12:37 AM
RE: C++ basics part 1 - by Ghost - 09-14-2013, 12:34 AM
RE: C++ basics part 1 - by cxS - 09-19-2013, 03:15 AM
RE: C++ basics part 1 - by Cyanide and Cynicism - 09-19-2013, 03:54 AM
RE: C++ basics part 1 - by cxS - 09-19-2013, 04:37 AM
RE: C++ basics part 1 - by w00t - 09-19-2013, 05:47 AM
RE: C++ basics part 1 - by cxS - 09-20-2013, 12:19 AM
RE: C++ basics part 1 - by Phoenix2014 - 10-07-2013, 06:13 PM
RE: C++ basics part 1 - by TechSaavy - 10-07-2013, 08:31 PM



Users browsing this thread: 1 Guest(s)