Sunday, March 18, 2012

Introduction to C++

C,C++ are the basis for all the high level languages today!!!
C++ is an intermediate level language

Different levels of languages 

Low level languages are those languages which are closer to the hardware and can manipulate memory much more easily than high level languages.
example - assembly language

High Level Language are machine independent programming languages, they cannot manipulate the memory easily but they give a good and interactive foreground to the program.
example - java,python, perl

Intermediate language are those that have features from both the languages.
example - C,C++.

Let us start with C++

The first and the foremost important thing that we require is a compiler. A compiler is something that converts your code into machine language and makes it executable.
There are many compilers in the market but the one's that are pretty well known are Code::Blocks for windows and gedit for Linux or XCode for mac users.

Now, to start with the C++ coding, each line that we write in the editor is a command to the system to perform certain function.
This collection of commands makes a source code.
a command consists of keywords, variables and instructions.

Keywords are predefined words that the language uses and are not allowed to be used as variable names. for example - if, int, char, for, while - these are all keywords and these words have some significance in the library of the compiler and a user cannot use these names as  variable names.

Variables are locations in the memory of the computer created by the user where the user can store data and from where he can retrieve data.
a variable is always preceded by a data type. 

different data types -
int - to store a numeric or integer value
char - to store a letter
String - to store a sentence (not present in C)
float - to store a decimal value
boolean - store true or false(not present in C)
Double - to store large float values
long - to store large int values.

To perform certain inbuilt functions like printing value on the screen or to take values from user or do complex mathematical function like square and square roots there are inbuilt programs created in the library of C and before we write the simplest code it is mandatory to call a few of these library files.
The example below will show you how to call them

There are two parts to a program i.e Instructions and comments
Instructions are used to ask the program to perform a certain function.
anything and everything written on the editor is considered as an instruction until it is preceded by "// " or "/*".

 //line comment
/*block comment or multiline comment */

Comment is something that a programmer writes for future reference, so that when he reads the code sometime later he knows what a few statements mean and what do they do.

here is a simple example to start with

#include<iostream>
#include<string>   
using namespace std;

int main(){
    int a,b;      //This is a comment. Here a,b are variables, int is a data type
    int c (6);   // constructor initialization. here 6 is the initial value of c
    int d = 5;  //here initial value of d is 5
    int result;
    string new1,new2;
    string new3;
    new1 = " this is my first program";// initializing values to new1 and new2
    new2 = " this is my second string";
    cout<<"Please enter the first number"<<endl;
    cin>>a;
    cout<<"please enter the second number\n";
    cin>>b;
    cout<<"enter the new string"<<endl;
    cin>>new3;
    cout<<new3<<endl;
    result = a + b;
    cout<<"final result = "<<result<<"\n the value of c is = "<<c<<endl;
    return 0;
}

in the above code
#include<iostream> is a header file used to include predefined program to print out on the screen and take value from the user.
to print something on the screen we write
cout<<"Print on the screen";
 to take values from the user
cin>>variable name ;

#include<String> is used to as we are making use of String as a data type.

endl is used to go to the next line.

2 strings can be concatenated using "+" sign.

using namespace std - if we want to use cout and cin directly we need to specify this else we'll have to type as follows

std::cout<<
std::cin>>

i will explain you the significance of :: later. 

No comments:

Post a Comment