(Transferred from the wiki by Peter)
Welcome to C++ !
I am going to teach you some basics of a very powerfull programming language called C++. C++ is language base on the famous C langugae and was created originally by Bjarne Stroustrup in 1979 .
Data types and variables
There are seven basic data types in c++ :
- char : character datatype which represents a character like 'A' or '@'.
- int : integer datatype which represents an integer number like 63.
- float: floating point datatype which represents a floating point number like 63.63 .
- double: double precision floating numbers.
- void: valueless datatype
- bool: boolean datatype which has two possible values true or false
- wchar_t wide character which uses 16 bits for representing data.
OK these are the basic datatypes but what are variables ?
A variable is a container which we declare to hold some data for us. A variable is declared like this : datatype variable_name. Here are some examples :
- int no_of_visitors;
- float temperature;
- bool is_registered;
Notice that at the end of each line of the code above there is a semicolon( we put it at the end of every statement to tell the compiler this is a statement.
We can declare several variables of the same type in one statement like this :
type first_variable , second_variable , third_variable ; .
And we can initialize the variables (give them value) when declaring them like this :
type variable_name = value;
Here is an example :
int no_of_users = 63 , no_of_guests = 100;
In C/C++, the names of variables, functions, and various other user-defined objects are called identifiers. These identifiers can vary from one to several characters. The first character must be a letter or an underscore, and subsequent characters must be either letters, digits, or underscores. Here are some correct and incorrect identifier names:
Correct Incorrect first_user 1st_user user_list user..list
In C++ Input/Output(I/O) is done by means of streams. This means you create a stream and link it to a device and use it. All streams behave in the same way even though they are linked to different devices. There are some predefined streams to use : cin and cout . cin which is used for inputing data is linked to the keyboard by default(it can be redirected) and it is used for inputing data . cin stands for console input.
cout on the other hand stands for concole output and is used for printing data on the monitor, since it is linked to the monitor by default.
To use these streams, we should include the file <iostream> first.We do it in this way :
This line should be placed on top of your source file. Now we can use the predefined streams like this :
std::cin >> no_of_users ;
To avoid typing std:: before each instance of cin and cout we can add a line before our main funcion like this :
using namespace std;
From now on we can use our known cin and cout freely without typing std:: .