Learning C++: 2011-08-14

Pages

Friday, August 19, 2011

What is basic input output operations in C++?

Hope you tried Hello world program, and at least got a idea how C++ programs looks like.

Now its time to learn basic input output operations in C++.
With few suggestions now i am improving my blogging skills. So I suggest you all my readers to please comment, like review of post, or is my posts are beneficial for you or not??

To learn Basic Input Output operation in C++ first you need to clear your concept of C++ most useful object i.e. "cin" and "cout".

1) cin object is use to take input from keyboard, its always associated with its operator sign >> , after this sign you need to specify variable name.
Example: cin>>VariableName

2) cout object is use to display content of variable or custom message to screen, its serves as output operation. It is also associated with operator sign << , after this message or variable is used to specify.
Example: cout<<"Hi";

Now here is program with C++ basic input / out operation.

In below program, we will show message and save number in integer variable "no1".

#include<stdio.h>
#include<iostream.h>
void main()
{
    int no1;
    cout<<"Enter number: ";  // Show message
    cin>>no1;  //Save Integer value in variable no1
    
    //Showing number which was entered by user
    cout<<"You have entered "<<no1;
    getch();
}

Now notice while showing variable value using cout,  we have used operator  << twice, this is because first operator << will show our custom message and second << will show value of  no1.

Run this program using run option of your IDE and enjoy. If you have any doubt comment me.
Enjoy Coding :)

Thursday, August 18, 2011

First C++ program or “Hello world” program in C++


Here is the first C++ program or simplest of all C++ program. While writing program, first its structure is required to understand, structure means its “PROGRAM FLOW”.
There is some steps of C++ which needs to be follow while writing C++ program, these steps are as follows....

1) Including header files or namespaces:
Including of header files for C++ program is compulsory. Header files are basically predefine functions which simplifies complexity of code.
Example: To display character on screen in C++ program it requires number of lines of code, but with the help of <iostream.h> it’s very simple as to write only keyword cout<<”character to display”. 
      
 2) Writing main function:
Now what is an importance of main function? Main function is starting point for any C++ program. Compiler starts reading code from main function. Main function for C++ is written as void main() or int main()




     3)  Declaring variables
Many programs needs operations of entities, like addition of numbers, here it will require variables (Space in program) for numbers.
In C++ we can declare variable anywhere in program but I recommend declaring it earlier in program.

       4) Operations
Next step is operations over variable declared as per requirement of program.

       5) Commenting
Comment is most important part of any program, because a good programmer never forgets to comment its code. Comment is basically statement written in natural language to understand the code. Its starts with // sign. It can be written anywhere in program as per programmer’s wish.
Compiler never reads code after // sign, so it has no effect on code, but it increases understandability of code to other than you. 


Program to display word “Hello world”

// Including header file


#include<stdio.h>

#include<iostream.h>



//start of main function


void main()

{


// Displaying message under double braces
 

cout << “Hello world ” ; // cout is keyword


//require to sudden stop of program, getch gets character


getch();



Try this program once with turbo C IDE. Comment me for any doubt or query.
Enjoy programming :) 

Wednesday, August 17, 2011

What are the Keywords and DataTypes in C++ ?

Moving towords first C++ program, there is something which need to learn, Which will help us all the time whenever we will use C++.  Keywords and DataTypes are vital parts of any programming language.
    Keyword are special words which is reserved by Compiler. As we will proceed in our path, I will explain importance of keywords. Here we are using Visual Studio 2010 for C++ programming, click here to view all C++ keywords.
    DataTypes are classification of data used in any programming language. Mainly Integer, Character and Floating Points are broad classification of C++. Each data type has its range based on machine architecture and programming language constraints. Click here to see all C++ DataTypes.
   Comment me if you have any doubt or question. Enjoy Programming :)