Tuesday, 30 August 2016

FUNCTION OVERLOADING


We can create as many functions as we want but what will happen if they belong to the same class category, Let's say that there are two function printing some values but of different types.In this case we will be needing two functions. But we can make the functions with the same name but the condition is with the different number of Parameters or arguments. 
It will be more clear with the help of the program.

PROGRAM: 

#include<iostream.h>
#includ<conio.h>

void show(int a)      //Function 1
{
cout<<"This function contains Integer value = "<<a;
}


void show(float b)    //Function 2
{
cout<<"This function contains Float value = "<<b;
}

void main()
{
clrscr();

// Now we will Call the functions
show(2.3);      
show(10);
getch();
}  


OUTPUT:

This function contains Float value = 2.3
This function contains Integer value = 10

Now the first calling was 'show(2.3)'. This was a float value, so the compiler compared both the functions and their Return types and Parameters. When it found the float value, the function 2 was called then. Then the next call 'show(10)' was having an Integer value, so comparing with the parameters it executed the First Function. 

Also :


Saturday, 27 August 2016

Functions


Functions are the small parts or we can say sub-parts of the program that perform special tasks. They can be reused again when required.

Syntax: type function-name(parameters)

              {body of the function }

type means it's return type and if the function is not returning any value then its void. For Example if the function is returning an integer value then 'type'= 'int'. If no type is specified then Compiler assumes an integer value by default. So it is good to specify the Return type. Then in the parentheses is parameter_list. It can be leaved blank but it's up to the programmer whether he wants variables or not.



Program: 

#include<iostream,.h>
#include<conio.h>
void main()
{
clrscr();
void show();
{
 cout<<"Hello World";
}
getch();
}

Output:  Hello World

This will create a function 'show' inside the main. When the program will execute the compiler will go inside the function 'show' and will display the statement "Hello World" and will come out of the function and the program will terminate.

Function Prototype is the declaration of the function that tells the return type and the number of arguments and type of arguments that are going to be used in the program. If the function's definition is written first then there is no need for function prototype because function's definition is declaration itself( i.e. prototype of the function).


A function can be invoked in two manners:

1. Call by Value
2. Call by Reference


Call by Value

Call by value means calling the function by the matching of the number of arguments. The Call by Value copies the value of actual parameters(These are the parameters that appear in the function call statement or you can say in the 'main' function) into the formal parameters(These are in the function's definition). The function creates it's own copy of values and then uses it.


Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void show(int);
int num;
cout<<"Enter a Number: ";
cin>>num;
cout<<"Original Number: "<<num<<"\n";
show(num);
cout<<"Number after change: "<<num<<"\n";
getch();
}

void show(int num)
{
num= 5;
cout<<"Value of Number in function: "<<num<<"\n";
}


Output: 

Enter a Number: 10
Original Number: 10
Value of Number in function: 5
Number after change: 10

'show' function copies the value from the actual and shows it in formal. So by call be value does not changes the original values(i.e. actual parameters). So even after calling the function 'show' there will be no change in actual.

For Understanding Better: 


Array Initialization


It is a simple method to initialize an array, whether One-Dimensional or Two Dimensional.
The values given in the brackets is separated by the comma. All the elements in the list will have the same base type because as you know an array consists of elements of  same type of elements whether float, int,etc.




Syntax: type array_name[size]={values_list};


Example:

int num[6]={23,56,12,81,10,1};
Now the values would be assigned like:
num[0] =23;
num[1] =56;
num[2] =12;
num[3] =81;
num[4] =10;
num[5] =1;

Two Dimensional arrays are also initialized in the same manner.

Syntax: type array_name[size][size]={values_list};


Example:

int matrix[3][3]={1,2,3,4,5,6,7,8,9};
Now the values would be assigned like:
matrix[0][0]=1;
matrix[0][1]=2;
matrix[0][2]=3;
matrix[1][0]=4;
matrix[1[1]=5;
matrix[1][2]=6;
matrix[2][0]=7;
matrix[2][1]=8;
matrix[2][2]=9;

It can be also be written as
int matrix[3][3]= { 1,2,3,
                               4,5,6,
                               7,8,9 };

C++ also allows you to skip the size of the array in initialization statement. The main advantage of not declaring the size is that we can increase or decrease the value_list without changing dimensions.

Example:      int square[]={2,3,4,5};

                     char name[]="User";
                                   


Friday, 26 August 2016

Single Dimensional Array


It is a simple form of array. It consists of type, name and its elements(subscripts or indices).

Syntax: type array-name[size];
It's type is also called the base type. It starts with 0 and goes till 'size-1' . Let's understand this with an example.


Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, marks[3];
for(i=0; i<3; i++)                /* i is taken zero because our array starts with zero
                                             so it will make us easy to understand it's
                                            elements position
                                          */
{
 cout<<"Enter your Marks in paper "<<i+1<<": ";
 cin>>marks[i];
}
cout<<"\nHere are the Marks of Student\n";
for(i=0; i<3; i++)
{
 cout<<"Paper "<<i+1<< "= "<<marks[i]<<"\n";
}
getch();
}


Output:

Enter your Marks in paper 1: 20
Enter your Marks in paper 2: 17
Enter your Marks in paper 3: 18

Here are the Marks of Student
Paper 1: 20
Paper 2: 17
Paper 3: 18

The above array takes the Marks from the Student. The first values goes at position 0, second stores at position 1 and last at position 2. Then in the next Loop values stored in the Array marks is displayed.

Thursday, 25 August 2016

ARRAYS


Array is having a large number of data of same type at a single place. The proper definition will be: An  Array is a collection of  variables of the same type that are referenced by a common(same) name.
It can be of any type : char, int, float or even user defined types.

Array are of different types:

1. One- dimensional arrays: have only one Row or column.

Syntax: type array_name[size];


2. Multi-dimensional arrays: It has both rows and columns. You can also say that having many single arrays in one array. The simplest example is Two-dimensional array but it can have more than two-dimensions.

Syntax: type array_name[rows][columns];

Nested Loops


A loop containing another loop in its body is a Nested Loop. In Nested Loop the inner loop terminate before the outer loop.

Program:

//Assuming that all the header files are included
.
.
.
for(i=1; i<5; i++)    //Considering that variable i and j are already created
{
cout<<"\n";
for(j=1; j<=i; j++)
cout<<"*";
}
.
.
.

Output:

*
**
***
****

First the value of i is assigned as 1, then comes the inner loop j =1(also assigned). Now inner loop having j will run until the test expression becomes true and then value of i increases from 1 to 2. Then inner loop will run again and the will go on like this until the test expression of i becomes true.Then it will terminate.

The do-while Loop


It is an exit controlled loop. It means that it evaluated its test expression after executing the loop body because its test expression lies at bottom of the loop. It runs atleast once whether the condition is true or false. Other loops(for and while), first checks the test expression then executes accordingly.



Syntax: do
              {
                   statement;
              }
              while(test_expression);   //Put semicolon after while statement


Program: 


#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();  // For clear screen
int num=1;
do {
           cout<<num<<"\n";
           num++;
      }
     while(num<10);
getch();
}


Output: 

1
2
3
4
5
6
7
8
9
The do while loop will run as long as 'num' is smaller than 10.
You can ask questions if you have doubts regarding C++.

THE WHILE LOOP


It is an entry controlled loop. The variable should be initialized before the loop begins as uninitialized variable can be used in an expression. Then variable is updated inside the body of while Loop.


Syntax: while(expression)
                       {
                          loop body
                        }


Program:


#include<iosteam.h>
#include<conio.h>
void main()
{
int num=1;
while(num<10)
{
cout<<num<<" ";
num++;
}
getch();  
}

Output: 1 2 3 4 5 6 7 8 9

The loop will run as long as the condtion is true i.e. as along as num<10. When it becomes false, the loop will terminate.

Wednesday, 24 August 2016

The for Loop


It is a simple loop. Loop means repeating the same things again and again until the condition has been met.
It is called the simplest because all the loop control elements gathers at same place making it more convenient to use.

Syntax:  for(initialization expression; test expression; update expression)

                       body of the loop;


Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
cout<<i<<" ";
getch();
}

Output:

1 2 3 4 5 6 7 8 9 10

 i=1: The loop starts with the initialization.As soon as the variable's value had assigned then comes the test expression.
i<=10 : it will check whether the condition is true or false. If true, it will execute the loop and then comes the update expression.
i++: this will update the value of i from 1 to 2.
Now it will check again the test expression. It will be true again so it will run the loop. This will go on until the i becomes 11. Now the test expression will become false. So the loop will terminate.

For loops are generally used for making patterns, borders etc.

The Switch statement


Switch is used when we know what our conditions will be or whey they are specific. The select statement test the value of an expression against  a list of integer or character constants. The statement is executed as soon as the match is found.



Syntax: switch(expression)
              {
                case constant1: statement;
                                         break;
                case constant2: statement;
                                         break;
                case constant3: statement;
                                          break;
                default:             statement;
             }
When a match is found the statement executes and then it breaks to come out of the switch. The default statement is executed when no matches are found. It doesn't need break statement.

Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();  // Used for clearing the screen before running the program
int month;  //Variable made of type int
cin>>month;   Taking the value from User
switch(month)
{
case 1: cout<<"January";
            break;
case 2: cout<<"February";
            break;
case 3: cout<<"March";
            break;
case 4: cout<<"April";
            break;
case 5: cout<<"May";
            break;
case 6: cout<<"June";
            break;
case 7: cout<<"July";
            break;
case 8: cout<<"August";
            break;
case 9: cout<<"September";
            break;
case 10: cout<<"October";
            break;
case 11: cout<<"November";
            break;
case 12: cout<<"December";
            break;
default: cout<<"Sorry!";
}
}

So variable month is taking the value from user and comparing it to the switch case.
Output:         2
                February

Let's understand this with another example.
Q. Find whether the giver character is a vowel or a consonant.(With switch)


Program:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char character;  //Variable made of type char
cout<<"Enter a character: ";
cin>>character;
switch(character)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': cout<<"It's a Vowel";
break;
default: cout<<"It is not a Vowel";
}
getch();
}

Now this program will accept a character variable from user and will start to match it with the cases. If the character is Vowel then it will display "It's a Vowel". It will work just fine.


Output:


Enter a character: i
It's a Vowel

If else Statement


If statement

Sometimes you have to make decisions based on the conditions that are created during the program. So, if statement is used in that kind of situations. It checks whether a condition is satisfied or not. If yes then it runs otherwise skips.
Syntax:    if(expression)
                    statement;
statement can be a single statement, a compound statement or can be blank. If the expression is true then the statement is executed otherwise ignored.

Else statement

It is executed when the if statement becomes false. It is optional to include the else statement but I will recommend you that you use it because sometimes you want to see whether your statement worked or failed during execution.

Let's understand this with an example.

Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
a=10;
b=20;

if(a>b)
cout<<"a is greater than b";
else
cout<<"b is greater";
}

Now when the program will execute, it will go to the assigned values, then will start with if statement. So the first statement becomes false because a is not greater than b. Then it will display the else statement. So the output will be: b is greater.

First Program in C++



Program:


#include<iostream.h>
void main()
{
cout<<"Hello world";   //For showing output
}


#inlcude<iostream.h> : This is a header file. Included for the input and output which is needed in every program. 

void main(): this is the main function where the program is executing. It starts with { and ends with }. All statements written within these braces are included in program. It has 'void' return type because our main function is returning no values.

cout : It is used for showing the output ("Hello World" in the above example).

// : This is called a comment and it doesn't affect the program. It's used for keeping tracks of the things as you know large programs becomes complicated, so we need comments. You can also use /* */. Anything written between these are also considered as comments and these can be used for multiple lines comments.
Example:  /*  This is a
                        sample for
                        multiple line comments*/

So this was a simple C++ program.

C++ DATA TYPES


1. int data types(basically for integers)

  Integers are represented with int data type.
  For example:    int num=7;
 This statement will create a variable of type int and assign it a value 7.

2. char data type(for characters)

   An identifier declared as char becomes a character variable.
  Example: char a;
  Letters,symbols etc are represented in memory by associated number codes which are integers only.   So it is often said to be an integer type.

3. float data types(for floating point numbers or numbers with decimal)

  A number having a fractional part is a floating-point number.
  For Example: 5.1245. It is a floating point number. Decimal point makes ensure that it is a floating     number. Let's say 30 is an integer, but 30.0 is a floating-point number.These numbers can also be    written in exponent notation. 349.4561 would be written as 3.494561E02.
 Example: float de=4.67;


4. double data type

  It takes twice as much memory as compared to floating point numbers. It also stores floating  numbers but with larger range and precision.

5. void data type

 void means empty. It is used as the return type when function don't have a return value.
Example: void func();

Tuesday, 23 August 2016

Basic Concepts of OOPs


The general concepts of OOP are given below:

1. Data Abstraction
2. Data Encapsulation
3. Modularity
4. Inheritance
5. Polymorphism



Data Abstraction

Abstraction is just representing the essential features with hiding the background details.
To understand it better, let's take an example, e.g., you are using a calculator and doing the sum by pressing the keys. Now you know every function of the key but you are not able to see what's going inside the calculator to do that simple sum. This is what we call Abstraction.


Encapsulation

The wrapping of the data and functions into a single unit is Encapsulation.
Just like you can see the above image, the capsule can be consider as a class which contains both the data and the procedures or functions into a single thing. 
Encapsulation is most often achieved through information hiding, which is the process of hiding all the secrets of an object that do not contribute to its essential features.

Modularity

Partitiong a program into individual components is called modularity i. e. it reduces the complexity and creates a number of well defined, documented boundaries within the program.
For example consider your computer. It consists of Monitor, Keyboard, Mouse, CPU, speakers, etc. These devices are complete units in themselves but yet are sub part of  your System.

Inheritance

The property of one class to inherit the properties of another class is called inheritance. Just like you own some of the features form your parents and have some of your own. Inheritance is needed many times during a program like when you need some attributes like the previous class you made. You are not gong to add those variables again because you need those variables+new variables. At that time inheritance comes handful. 

Polymorphism

Let's understand this with a simple example. Suppose you have three functions named circle, triangle and square. Now if I pass the comment that I want the area and run these functions, then they will give me the area but in their own way because all have different behaviors. Now the important part, when the message passed every function executed the code but in their own way. 

For Understanding More:

  

Monday, 22 August 2016

Procedural Programming Explained


It lays more Emphasis on procedure than data. C uses procedural programming and C++ is the super set of C so it can also be used as procedural programming language. Inline functions defaults arguments are a good  example of this.

Procedural Programming paradigm separates the functions and the data manipulated by them. By separating the data and the functions problems are created such as:

1. Procedural Programming is susceptible to design changes.

For example: You created a structure like: struct person{  char name[20];
                                                                                             int age;
                                                                                          }
        and there are many other functions using this structure. Now if there is a change in the structure like:                                                     struct person{  char name[20];
                                                                                     int age;
                                                                                     char address[30];
                                                                                 }
Now all the functions working on structure 'person' must also be modified to cope with the change in structure. The functions using the structure 'person' will need to be rewritten. 

2. Procedural Programming waste more time than usual and cost.

As design changes, we have to change many things and have to apply modifications in the code that leads to wasting time and Money.





C++ Basics


It is a programming language that uses object oriented technology, and the most  near to the real world. Well, the real world means it helps us to relate the things or objects as real world objects because real objects have attributes and behavior, just like the objects in C++. It doesn't follow any path whereas in procedural programming you have to.

Consider Phone for example. It's attributes will be sleek design, waterproof(to some extinct), 8 Mp Camera and it's behavior will be making calls, sending messages, using Internet etc. So, now you know the difference between attributes and behaviors.

It has many other features like:

1. It has Abstraction, Encapsulation, Inheritance, Modularity and Polymorphism.
2. It also supports function overloading.
3. It has strict type checking.
4.Inline functions instead of Macros(Inline functions are more safe).
5. Variables can be declared anywhere in the program(But first you have to define them).
6. Data Hiding which prevents accidental change in data.

KNOW MORE ABOUT THE USE OF C++: