Learning C++: 2011

Pages

Wednesday, August 24, 2011

Finding length of string in C ? (Not for C++ Readers)

In below program, I have find out string length and print each character of string in new line..

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char string[10];
 int i=0,lengh=0;
 clrscr();

 printf("Enter the string: ");
 scanf("%s",&string);
 lengh=strlen(string);

 for(i=0;i<lengh;i++)
 {
 printf("%c",string[i]);
 printf("\n");
 }
 getch();
}

Monday, August 22, 2011

What are the basic arithmatic operations in C++ ?

Addition, subtraction, multiplication and division are basic operations of any programming language.
In C++, basic arithmetic operations are
1) Addition (+)
2) Subtraction (-)
3) Multiplication (*)
4) Division (/)
5) Increment by 1 (++)
6) Decrement by 1 (--)
Here I am demonstrating example of addition in C++, I want you guys to try other operations by your self. If you will find any problem, comment me.

Program Definition: Take two number from user and add them and display result.

How to solve: (Logic)
1) Take number 1 and save in no1 Variable.
2) Take number 2 and save in no2 variable.
3) Take no3 variable to save result of addition.
4) Add no1 and no2 and assign result in no3.
5) Display variable no3.

#include<stdio.h>
#include<iostream.h>
void main()
{
     int no1,no2,no3;
     //Clears output screen
     clrscr();
     cout<<"Enter first number: ";
     cin>>no1; 
     cout<<"Enter second number: ";
     cin>>no2;
     //Assign result of Addition of no1 and no2 to no3
     no3=no1+no2;
     cout<<"Addition of both numbers is: "<<no3;
     getch();
}

Try it, and try with all arithmetic operations and share your experiences.
Enjoy Coding :)

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 :)

Wednesday, August 10, 2011

What is "for loop" and "array" in C language ? (Not for my C++ readers)

Hi ! I am here putting simple C program to demonstrate Loops and array in C language. Code is only for my C programmer friends,not for my C++ reader please wait for my next post regarding C++. This post is just because it urgently required by my C programmer friends.

Problem Definition: Program to count the number of student who has  pass in a particular subject in a given class and count Passing percentage

void main()
   {
    /* Variable Declaration */
    int student[100]; 
    // Declare integer array assuming max students are 100
    int nCount=0,i=0,passCounter=0;
    float passPercentage=0;
    clrscr();

    printf("\nEnter Number of Students: "); // Printing Message
    scanf("%d",&nCount); // Get count of number of students

    //Save marks for each student
    printf("\nEnter marks of student in sequence\n");
    for(i=0;i<nCount;i++)
    {
        printf("\nEnter mark for student number %d :",i+1); 
        // i+1 because for loop starts from 0
        scanf("%d",&student[i]); 
  // Dont write i+1 here because it saves number from 0 in Array
    }

    // Calculating count of pass students, here criteria of Passing is 40

    for(i=0;i<nCount;i++)
    {
         //checking mark of each student starting
         if(student[i]>40)
         {
        passCounter++; // Incrementing counter of Passing student
         }

    }

    // Calculating Percentage of Passing student

    passPercentage=(passCounter/nCount)*100;

    //Displaying result

    printf("\nCount of Pass Students are %d",passCounter);

    printf("\nPercentage of Pass students are%f",passPercentage);

    getch();
   } 

Comment me for any query regarding program.

Monday, August 8, 2011

What is Object oriented programming (OOP) ? (Using c++)

Object Oriented Programming (OOP) is programming paradigm (METHOD or you can say STYLE). In C++, everything happens around Object, that's why it is called as object oriented programming,
Now what is Objects? To understand object you first need to know what is class?

CLASS IN C++ : Take an example of a CAR. Each car has wheels, roof, color and many more features. But every car is not similar to each other. (Here i am talking about type of cars), still we categories them into car section. Put break here and read it once.......

Now this category of car is Class in C++ language and types and variety of cars are its Object. Hence Class is general representation of objects.

Now in reality our streets don't serves services to cars only, there are BIKES, BICYCLES, TRUCK and many more. These also represent them selves to some class. 

This object oriented programming has many concepts which makes any programming language rich like our C++.
These concepts are 

1) Class (ADT-Abstract data type)
2) Object
3) Inheritance
4) Encapsulation
5) Polymorphism
6) Method overloading
7) Operator overloading
8) Method overriding 
9) Friend function
10) Exception handling

If i missed something here then i will tell you later. but for mean while read it once. And don't scare we will learn each concept very easily.
Keep your IDE (Editor) ready for next time.....

Sunday, August 7, 2011

What is C++ and Programming Language?

"Computer Program" sounds good in hearing... But I can bet you will feel its magic when you start doing programming your own. I will give you small background of Programming language world. Programming era started on early before when computer was in its stage of maturation. 
          But serious first Programming language was started between 1969 and 1973, i.e. "C Programming". It was best ever product of Bell's Factory. It was built by  Dennis Ritchie. C is still booming programming language in this modern world. Its one of the main ingredient of UNIX Operating systems, Linux OS and many more... Android OS also contains some part of C. Here I am not going to elaborate you C Programming language. Don't worry many more concepts of C++ is similar to C. Even if you have any question or doubt put it into comments or ask me.... 

          Moving forward to next lap of programming language, i.e. Object Oriented Programming language, headed by C++. Its true that modern computer language takes lesson from C++ for its build up. Many recent languages are build on concept of C++. and of course its also a product of Bell's Laboratory. build by Bjarne Stroustrup, on one year before when Deepika Padukone was born.. :) means 1983.
Here through this blog i will post concepts of C++, I will explain this in short and sweet way. I want my readers to implement logic and programes at least once.DON'T WORRY IF YOU DON'T KNOW C EVEN IF YOU CAN LEARN C++ VERY WELL. 
          For implementation i will suggest you to use "turbo c", you can say it as IDE(Integrated development Environment ) or Editing software. I will recommend you to use VISUAL STUDIO 2008 or 2010. You can download it from torrent. Now why VISUAL STUDIO because you can create professional level programes here and I am also using this. My all C++ Programes here, are tested in Visual Studio. Of course it will run on Turbo C also.. but even if you face any problem let me know. 
My C programes on this blog are tested in Turbo C. You can download my notes or many other ebooks from here. So stay update and visit here to get smart.
Enjoy Coding.... :)