Monday, June 10, 2013

Starting with c++

                 Why Learn C++ ??

Let's  start by knowing why to learn C++ :
Well, there are several reasons why you should learn c++.
Some of them are : 
1> It is de-facto standard language :           That is, it is used as the language of choice(in majority of the places) because it one of the
oldest oops language. It has huge resources for anybody who is willing to learn it like books, sites, blogs etc. 
2> Portability for C knowing people :         If you already know C,
then the journey becomes easier for you. Because C++ is nothing but an extension of C.
3> It is Fast : Being an advanced level language it is faster than all of it's present counterparts(C#, Java) if coding is done properly and proper optimisations are appllied and less memory intensive.
4> Wide Range of Compilers and Editors availabe : It has lots of compilers and editors available for free which you can download 
and run your code on.
5> Short Code : With c++ you can write relatively short codes saving you typing efforts(exceptions being scripted languages like perl, python etc).

there are several other points written in the link below :

Let's Get Started :

c++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs
At first we need to get you ready to run and test a c++ code.
I will be using Microsoft Visual C++ for all intents and purposes.
Here is a link to it :
2012 version :
2010 Version :

Just click the link and the download will start.
The 2010 Version is a Web Installer. So, first it will download  a
setup file which you have to run and then the installation will satrt
or
you can download any other c++ compiler like Dev c++, GNU C++.
For linux users :
1> For Fedora : 
Write this command in the terminal in super user mode :
yum install gcc gcc-c++

2>
For Ubuntu users :
Write this command in the terminal :
sudo apt-get install g++

For further help goto any Ubuntu or fedora forums.

Introducing  the Editor


This is the welcome screen of the Editor(MS visual studio).
From here goto :
1>File -> New Project.
this should open a window below like this :






















On the left hand size click the "visual c++" template and then click the "Win32 Console Application" now either you can leave the Name of the project as it is or you can give any name you want after naming the project click done.
2> After that the project's solution explorer will open :
solution explorer is the interface that allows you to add source files(.cpp, .c) and header files(.h) to your project. It should be something like this :
3> Now let's create our first C++ Source File :
A C++ source file is a file in which you will write your code and it has a extension of ".cpp".
Right click Source Files -> New Item
a window looking like this should open up.

now select the .cpp option marked in the image and either name it or leave it as it is.

Compiler: 

This is a program that is used to convert the high level language instructions into low level machine instructions so that the CPU can 
understand and execute the instructions and this process is known as compiling.

Run:

This is a process in which your souce code is compiled and executed and the output is shown on the displaying device.

Our Very First C++ Program :

Now let's analyze the the components of the program one by one :
1> Header Files :
These are the files that are required by every C++ program.
These Files contain in them the code that is required to use all the basic functionalities like "cout" here.
the "#include<Header File Name>" is a command which is used to instruct the compiler which header files to include.
In our case the name of the header file is "iostream".

2>The Main Function : 

This is a function* that is used as the entry point by the compiler.
That means the compiler starts compiling the program from the first line of main(). So the existence of this function in your c++ source code is necessary for the code to compile and run successfully.

3>"cout<<" :  

This is a stream* object. For know you just know that this serves as the middle man between your code and your machine's display device. "<<" this means you are inserting the string "Hello World"
into the stream which in turn is taking it to the Display unit.

Running and Compiling the Code :


click the button highlighted in the image to run the program.
you will find that the program output(A black console screen)
comes on the screen and disappears.
to remove this problem add the header file "conio.h" and a function getch(); in the last line of your command.
After all the modifications here is the the program with the output :


























No comments:

Post a Comment