Wednesday 9 January 2013

WELCOME!




Hello everyone! I have created this blog to share my knowledge about the basic programs in C++ which I have learnt and still learning. I hope you find the information in this blog useful.

If you're willing to follow this blog, then you'll need a C++ IDE. If you already have one, then well and good. If you don't you can get it from here: http://www.mediafire.com/?99k8xlxkse32e6w

Happy C++ Programming :)

Tuesday 8 January 2013

WHAT I NEED TO KNOW BEFORE I START

INTRO 


C++ (read as c plus plus) is one of the oldest programming languages. It was developed by Bjarne Stroustrup in the 1980s, in the AT&T Bell Labs. He found C lacking for simluations and decided to extend the language by adding features from his favourite languages simula 67.

CHARACTER SET IN C++:

Letters:     A-Z, a-z
Digits:       0-9
Symbols:  !, @, #, $, _, -, =, (), [], {}, ^, &, etc
Spaces:    blank space, vertical space, horizontal space

Important terms that you must know:-
 Tokens
  The smallest individual units of a C++ program are called tokens
   e.g. main, void, {} .etc
 
    C++ has the following tokens:
    (i) Keywords:
        Keywords are those reserved words which convey a special meaning to the compiler. These cannot be
        used by a programmer for other purposes than for the one it is already reserved.
        e.g. void, main, int, float, if, else, include, getch, cout, cin,.etc

    (ii) Identifiers: 
        An arbitrarily long sequence of letters and digits,  which are used for general terminology for given
        names to different parts of program like variables, objects, classes,.etc are called identifiers.

    (iii) Literals: 
         Literals are those data items that never change there value under any circumstances(or during the
         program run).
       
         Types Of Literals:
          1. Integer-Constants:
              Whole numbers (without fractional parts) are integer constants.
               e.g. 12, 30098, -96, -147
          2. Floating-Constants:
              Numbers having fractional parts are called floating point constants. They are also called real
              constants.
              e.g. 12.03, -99.99, 125.0
          3. Character-Constants:
              One character enclosed in single quotes ( ' ' ) is called a character constant.
              e.g. 'a', 'Q', 'Z'
              ESCAPE SEQUENCE:
              A backslash followed by one or more character contstants are called escape sequences.
              e.g. '\n' (used for new line), '\t' (used for horizontal tab), etc.
              These are non-graphic characters.
          4. String-literals:
              Multiple character constants enclosed within double quotes are called string-literals.
              e.g. "QWERTY", "Hello World"
 
     (iv) Punctuators:
           Characters used as separators are called punctuators.
           e.g.     (  )  {  }   ,    ;    :     etc
         
     (v) Operators: 
          Tokens that trigger some computation when used are called operators.
           e.g.  the + operator triggers addition, the * triggers division

          Types of operators: 
          1. Binary Operators:
              These operators are used with two operands.
               e.g.  + (addition), -(subtraction), *(multiplication), / (division)
          2. Unary operators:
              These operators are used with two operands.
               e.g. ++ (increment operator), -- (decrement operator), & (address operator) etc
          3. Ternary Operators:
              These are used with three operands.
              There is only one ternary operator-the conditional operator.
                     (condition1)?(condition if true):(condition if false)

So, thats all the basic stuff anyone has to know before taking a first look at a C++ program. (•‿•)
           











FIRST C++ PROGRAM

INTRO

Now that we know the basic details  about tokens, the building blocks of a program, let us go on and build a program ourselves!
But wait, first let's take a lookat  the C++ IDE (IDE is the acronym for Integrated Development Environment).


To open a new file click on File and then New.



Now, lets go ahead creating our first program.

//PROGRAM : 1
//To print a string (remember that strings are character constants enclosed in double quotes.) 



type the above code in your IDE. Then compile it. 


Then similarly click on Run>Run to run the program. After that click Window>Output.


the output window appears, with the output of our program.


Click on the small green square on the top left corner to close it and return to the IDE.

So, what's happening here? How did our program run and what made it run?

The iostream.h is a header file (which explains the .h file extension) which is a pre-written code file conatining many functions and their definitions. So, we included iostream with the keyword 'include'  to enable input/output facilities in our program (though we don't take any input, the string we displayed is considered as an output). So, we tell the compiler that first you include the required header file(s) and then you begin the program execution.

Next, the program execution begins from the main() part. The general syntax of the main() function is:

Firstly, the type of the main function is first written. In this case it is void, which tells the compiler than the main() function doesn't return any value. Next, the program body is written inside curly braces{}.The main() is where program execution starts. You can also define your functions (we will come to that later).

Then comes the function cout. cout stands for "console output" whose definitions are written in the iostream header file (so you now see why it is important to include them. with the increasing complexity of the programs, you'll appreciate their importance), which we have already included.
The "put to" operator << is used to indicate what we want to print followed by our string "MY FIRST C++ PROGRAM". Here, the task of  >> is to print whatever string succeeds it.

So, you see the codes that you've written follows some 'logic'. So, in programming what matters is the logic. Hence, if your logic is correct, the programs you design are correct, if anywhere you become illogical, the program too becomes incorrect.

So, I hope you got how to print a string in C++. If you have any queries, please feel free to contact me at
lostsoulofthegeek@gmail.com.
:)


POINTS FOR YOU TO PONDER ON:-
1. Why do you think we included the header file called iostream.h in the beginning and not in the end?
    (HINT: Use the notion of logic I just mentioned.)

2. Why is are pre-written header files provided to us so that we may use them even though we can write codes for almost anything?

[{ ANSWERS IN NEXT POST }]







Sunday 6 January 2013

ANOTHER C++ PROGRAM

INTRO

Now lets create another C++ program which asks the user to input two integers and dispays those integers.

PROGRAM: 2

Here, conio.h is included to enable the clrscr() function. It clears the screen from any text before executing the codes succeding it.
The getch(); function is used to get a character from the user (hey, you know what's character is, don't you? or else check out my first post : WHAT I NEED TO KNOW BEFORE I START). 






Variable Declaration:
We  declared two variables- num1 and num2 of data type int. Then we used cout<< to display our appropriate message. After that we used cin>> to get the input from the user (cin stands for console input, and there are a lot of ways to accept input and display outputs like getline, printf etc.).
Then again we displayed the numbers the user input.
That's all.:)


ANSWERS TO PREVIOUS POST Qs:

Q.1.)
In C++, or in any other programming language, the codes are executed sequencially, i.e, the 1st line is executed first, then the 2nd line and so on. So, if you include iostream in the begining, the compiler gets all the finction definitions in iostream.h, before executing the main( ) function, where you're using cout. But if you include iostream in the end, the main() function will get executed first and as there will be no definition for cout, the compiler will display an error that the function cout must have a prototype.

Q.2)
The iostream. h contains commonly used pre-written functions and codes of about 600 lines. So, if are going to make even simple programs, you'll have to write hundereds of line. So, to make it easier for you, or me or for anyone, header files are aprovided along with the IDE and these header files are kept in the standard library functions.

PROGRAM:3( PROGRAM TO ACCEPT 5 INTEGERS FROM THE USER AND DISPLAY THEIR SUM AND AVERAGE)

PROGRAM TO ACCEPT 5 INTEGERS FROM THE USER AND DISPLAY THEIR SUM AND AVERAGE

Nothing different, programming is just about logic and use your intuitions. If stuck anywhere, just mail me at lostsoulofthegeek@gmail.com 

//PROGRAM



(Why don't you try to write a program that prints the product of 10 integers. If any help needed, I'll be only too glad to provide it.)


I value each and every feedback and criticism. So, please comment and share this blog!