Pages

Showing posts with label CPP. Show all posts
Showing posts with label CPP. Show all posts

Saturday, 20 July 2013

Dev C++ - Review + Variations from Turbo C++




           I was addicted to the old school Turbo C++, until very recently, until I met Dev C++. For those who don't know new C++, Dev C++ may be a little strange, but once you get used to some minor changes from that of TC++ , you will be amazed how sophisticated is Dev C++ Compliler. 

       Some of the features I love the most are :
  • Automatic Indentation 
  • Syntax Highlighting
  • Updated and ready to go!
  • Error identification is BITCHIN'!!!
So if you are not satisfied with Turbo C++, I completely recommend you to move on to Dev C++, but you have to know the changes to be made in the program as for it to work with Dev C++. I am listing here, two of the most basic variations of DC++ from TC++ here:

     
  1. .header extension is removed, and most of the header names are changed. For example in TC++,  you include iostream header as : #include<iostream.h> , whereas in Dev C++, you should remove .h extension and type it like this : #include<iostream>. There are many header variations like this.
  2. Another important change is that, inorder to make the program work, you are required to enter this :          using namespace std;                                                                                                               in between the header inclusion and class/function declaration. 

 Here is the addition program in two compilers:

Turbo

#include<iostream.h>
void main(){
int a=3,b=3,c;
c=a+b;
cout<<c;
}

Dev

#include<iostream>
using namespace std;

void main(){
int a=3,b=3,c;
c=a+b;
cout<<c;
}

So, to conclude, Dev C++ is loaded up with many amazing and sophisticated features. And it is also opensource (FREE!) . You may download it from their website. 
That is all for now guys.. 
Spare a moment to say thanks. :)

Monday, 15 April 2013

#3 Operators in C++ and your first practical program - a simple calculator!

                        Today we are gonna talk a little bit about different "Operators" in C++. Operators are anything, which are coded to do something. We already have talked about one operator : the output operator. Actually, the output ( cout ) operator, is the part of a set of operators called input-output (I/O) operators, which also contains many other similar operators.
           
           Next important operator we are gonna learn from I/O operator is the basic input operator. Similar to "cout", the code for input operator is "cin". The header file needed for cin is <iostream.h> . The general format of this operator is :



'a' can be any variable you want to insert from the user. This can be a bit difficult to digest. But this is very simple... Lets do a practical. Suppose you want to insert a number from the user. First thing you want to do is to declare that a variable (a number which is yet to be known) is present in the program.

Declaration of a variable means, letting the compiler know that a variable is present in the program, so as to run it. To declare anything in C++, you want to inform the program it's datatype.
Now a datatype means the type of the thing you want to add to the program. There are two types of datatypes. They are Basic Datatypes and Derived datatypes.  Any program which involves integer operations, should have the "int main()" function. i.e. we have to replace the "void main()" function of the last program with "int main()".

We will learn more datatypes later. Now all you have to know is, an integer number (eg: 0, 2, 13, 63, -4, -7, -10) is of the datatype "int". The second thing to know about variable declaration is that, a variable has to be given a name. Since it is an unknown number, it is appropriate to name it an alphabet like 'x', or a, just like in algebra, where we name unknowns 'x'. This pseudo-name we give to unknown things like a character, or a number is called Identifiers Now lets name our number to be input by our user 'u'. (An identifier need not to be of a single character, we may also name our number 'unknownnumber' or something, but note that white spaces are not allowed in between the text of an identifier, and also it is for simplicity, we are naming our number 'u'). To letting the program know that a variable 'u' exists, you should insert this code just after opening the braces of the main() function:



Now that your program knows it involves an unknown number, next thing you will need to do to input a number from user, is, you should inform the user that the program is expecting a number from him/her. To do this, we just have to output a message saying to input the number, just like this:

Now as the user reads the message he will try to enter the number, but will fail miserably, because *tadaaa* there is no input operator. This is where "cin" comes into play. Now you should enter this code just after the message to user:



Now the user will be able to enter the number, and as soon as he presses enter, the  value he types get stored under the identifier 'u'. If we call 'u' again, the value of 'u' will be used. To understand this let us include this code to output 'u'.:



Let us look at the final code, and what happens when we run it:

The value entered was 34, and  when 'u' is output, 34 is displayed...

UPDATE : Dev C++ is much better than Turbo C++ if you want to learn updated C++. Here is a review of Dev C++ and variations of Dev from Turbo++, and its advantages over other compilers!

That is it for today guys...
Happy coding... :)



Wednesday, 13 March 2013

#2 Basic Structure of a C++ Program

Let us create a simple 'Hello, World' program... Now just type what you see on the picture in your compiler, and later we will analyze it...
Source code :
After entering the code, you should compile and run your program using your compiler. In the case of Turbo C++ 4.5, for running, press ctrl+F9 or Debug --> Run
Output:
No let us look at the structure of the code..
#include<iostream.h> : This line is to link the program to be run, to a specific file called Header File, in which there are information about things to be done in the program. We will learn about this later...

void main() : This line is called a 'function'. It actually says the compiler that this is where the Program execution should begin. We will learn much more about Fuctions, in coming lessons.

{ & } : Opening and closing curly brackets are the starting, and stopping point of a function. 

cout : Input and output (I/O) operators are integral parts of any programming language. They are the interactive parts of a language. A language without I/O Operators are like a human body without any senses (blind, cannot smell, deaf, dumb, insensitive to the skin)... Whoo! Can't even imagine right? "cout" is one of the output operator of C++ ... That means, cout displays text in the output. Notice that after cout, there are two "<<" less than signs. (NOT ">>"). General form of cout is : | cout<<"text to be displayed"; | Also note that, the operator ends it's line with a semicolon ";". Now try displaying your name in the place of "Hello World" in your program. We will learn many I/O Operators in coming days.

COMMENTS : Comments are those lines in a program which the compiler ignores. In our hello world program, you can see many comment lines on the right side of each line, in dark blue color. Comments can be used for teaching, reminding yourself, and also it is an important part in readability. There are two types of comments :
1. Single line comments - They are those which start with two front slashes "//" These can only be used for commenting in a single line. Their general form is | //commentcontent | 
2. Multiple line comments - Those which start with a front slash and a star and ends with a star and a front slash, are multiple line comments. These can be used to type many lines and paragraphs. The compiler wont even look inside those "/*" and "*/" ! General form is : | /* comment content no limitsssss! */ |
But there are some limitations for WHERE to use multiple line comments. For example, it cannot be used in such a way: | for(i=0;i<n /* Comment invalid */ ; i++) | i.e inside a loop (later, I promise... ;) ) 


In next lesson, we will learn about input functions, where we can INPUT our own values to the program, operators in C++, and EXCLUSIVE : WE ARE GONNA MAKE A SIMPLE CALCULATOR!!! Excited huh??? See you next time... 


Happy Coding.. :)

Monday, 4 March 2013

#1 Introduction to C++ Programming - CPP Tutorials for Beginners

General Introduction




Waah! We are now going to study a brand new and wide subject, C++!
         
            C++ is both an object-oriented and general purpose programming language. There was this long conflict between programmers of 90's whether C or C++ is better. Well, everyone has different Point of Views.
                          I think it in this way : C++ has all features included in C, plus many other new features like Inheritance, Polymorphism (We will study these later). So, C++ is a super-set of C. Or, knowing C++ makes you familiar with almost all the features in C too. Obviously, there are differences in structures, but features are same.

      C++ was developed by a computer scientist named Bjarne Stroustrup, of Bell labs in 80's.

Enough trash talks. Let's get into business!

Beginning C++ 

Prerequisites

NOTHING! I mean it! Nothing!!! There is no need for prior experience in any other languages to learn C++
But a little logic will help.

Things You Need

You must have a not-so-bad computer, with any Operating system.
You must have a compiler of your choice (There are plenty out there in the internet). I am using Turbo C++ 4.5 for demonstration. (Google it, Download it!)

Next Step


Well, be equipped with the things above in the description and
Keep Calm and Wait for my Next Lesson! 

UPDATE : #2 is HERE!
;)

Happy coding! :)