Let us learn C++ step by step
"A journey of a thousand miles begins with a single step"
C++ is a high level programing language developed by Bjarne Stroustrup ( pronounced as be-yar-ne Stroustrup). There is a difference between C++ and other high level languages - one can program to a very low level (very close to hardware) and extensively used for device (driver) programing. That is why it is also called middle level language means between Assembly level and high level programing. C++ is a language that supports multiple paradism of programing namely procedural, object oriented etc. Simply in C++ we can program without class & object as well as with class & object. It is not a pure object oriented language. C++ is the best programing language to learn. If you are a programer in true sense then you must know C++. Do you know Windows OS and Mac OS are developed using C & C++?
Language:
In any conventional language there is a character set, collection of words and a set rules to form sentencees. Same way in computer languages also we have all these things. C++ uses the English alphabet set and digits. It uses all the character we see in a standard keyboard. So far the words of conventional language concerned, in computer languages these are called commands and the sentence formation rules are called syntax. So before using any command we must know the syntax.
Point to be remembered while programing in C++:
- It is case sensitive, means it differentiates between uppercase letters and lowercase letters.
- Program must be written in small letters only, so keep your caps lock always off.
- All entered data is stored in variables. Variables are named memory locations. There are different types of variables, to be discussed in the subsequent topics.
- All the variable must be declared before they are used. Declare all the variable in one place (can be declare anywhere in the program). Variable name must single word & start with a letter, can be followed by digits. For multiple words use under score(_) to join them. Give such a name to a variable so that it describes its use. Do not use any special characters like dot, comma, semicolon, star etc.
- Program execution starts at the main function and it is from top to bottom.
- int - to store integer data. short, long and unsigned (int, long, short) are two variety of integers which can store integer data in diferent ranges
- float - to store real numbers
- double - to store real number with more precision than float
- char - to store a single letter or symbols.
- To store a word or sentence we need character array, would be discussed later on.
- User defined data types, means we can define data types of our own as per our need.
#include <HEADER FILE-1> #include <HEADER FILE-2> . . // A standard program structure void main(){ declare variables; write C++ statement1; write C++ statement2; . . . }
A standard C++ program structure in Dev C++:
#include <HEADER FILE-1> #include <HEADER FILE-2> . . // Dev C++ program structure using namespace std; int main(){ declare variables; write C++ statement1; write C++ statement2; . . /* comment line -1 comment line-2 */ . return(0); }Header file:
Actually the C++ commands are defined in different file which are called header files. When we use a command we must include the header file where it is defined. We include it as #include<iostream.h> where "iostream.h" is a header file. This way we can include as many header files as we need. The header files become part of our program so in order to keep the size of our pgrogram optimum we should not include header file which are not required.
It is clear from the above two program structure that all the statements of C++ is terminated with semicolon. A block of statement is included within { } as done in main function. The main() function is a special function in C++, there may be number of functions in a progrm (to be discussed leter on). Every function must have a return type - it can be void, int, float etc. If return type is not mentioned in main then by default it takes int type.
In Dev C++ an extra line using namespace std; must be added before main as shown above; and "main()" function to have int as return type.
In order to keep some comments in the program to describe the program like "what about the program is" or "to describe the use of a variable" etc., we can use "//" followed by the comment as shown above. This kind of comment is called single lined comment as it makes a line comment, the compiler simply skips these lines. Multi line comment also possible - start with /* the lines of comments */ as shown above