Friday, July 31, 2020

Inline Function in CPP

Now In this article I am going to explain about Inline Function in C++. also explain in such a way that you are able to understand and answer the following questions that arise in your mind  💧What is Inline function ? 💧Why it is useful in C++ programming? 💧When it is used? 💧 How it is used?

Inline Function:

It is an important feature of C++ programming. As the name says Inline function that means it is a function followed by keyword  Inline. Some time we need to call any function many times in a program than Inline function is useful.But if our function have number of line of code or simply we can say that function is big in size than it will take lot of time for compiling each line of code and if we call again and again a lot of memory is wastage. Most of the time it busy for performing following tasks such as 💧 jumping to the function.💧 Saving registers💧 Pushing argument into the stack and return to calling function. 

So we can say that it take lot of extra time for executing the instructions and perform above tasks. 

So, to eliminate the cost of calls to small function, C++ propose a new feature called Inline Function.
Inline function that replace the call with their definitions at the time of compilation.It is just like a macro in C

Syntax:

Inline function_header

{
    function body;

Example :

inline void show()
{
    cout <<"Inline Function";
}

void main()
{
    show();
    show();
    show();
    show();
}

Explanation :

Here inline function replace f() by cout <<"Inline Function" at compilation time.Here time is saved but memory space is waste.
All inline function must be define before they are called. If the function is large in size then inline function work in slower speed. It is beneficial in small function which is written in one or two lines. 

Example:

#include <iostream.h>
#include <conio.h>
inline float mul(float a, float b)
{
    return (a * b);
}
inline double div(double x, double y)
{
    return (x / y);
}
main()
{
    float p=12.345;
    float q=9.82;
    cout << mul (p , q)<<"\n";
    cout << div ( p , q)<<"\n";
}

Note: When we are using inline function then there are some restriction that have to be in mind such as Any inline function doesn't have any looping statements inside inline function.
Any inline function doesn't have any if-else statements inside inline function.
Any inline function doesn't have any break statements inside inline function.
Any inline function doesn't have any continue statements inside inline function.
Any inline function doesn't have any goto statements inside inline function.
Any inline function doesn't have any switch statements inside inline function.

So, we can say that it is a function contain simple statements.



Thursday, July 23, 2020

Data Encapsulation

Let we understand about encapsulation !
Encapsulation is binding of data and function in a single unit or block. Data inside the block is called member data and function inside the block is called member function.

Just like Capsules which bind medicine inside the capsule.We see the capsule but we can not see which medicine inside it.



There are lot of confusion with Data Abstraction & Data Encapsulation in both the cases one thing is common that is hiding of data.

I would like to clear first we need to identify entities like capsule , properties like medicine and behaviours like their effect (effect from humidity) this is abstraction in other words abstraction is simply a thought process to identify entity , properties and behaviours which is followed by encapsulation.

Now we can say that encapsulation is the process of providing physical structure to implement the outcome ( entity , property , behaviour) of abstraction that protect the property from accidental or unauthorised access.

In other words abstraction tells what to do? and encapsulation tells how to do?

Example of Encapsulation:

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

class add
{
private: int a,b,c;

public:
void sum()
{
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
clrscr();
sum s1;
s1.add();
getch();
}

It is a simple explanation and implementation of encapsulation. It is simple concept. 


Data Abstraction

Friends !  For understanding Data Abstraction first you link our self to real world don't see it technically than you can understand easily.

Data Abstraction Simple Means Data Hiding

Now first we see ,  Is it happen in our real life ? yes so many times. just remember 

some time you ask to your mother to make some testy food , she make and serve you , you eat and say wow what a food it is very testy with out knowing their internal ingredients used  and procedure to make that food. That all thing are hidden for you.

When you are purchase some appliances you just use it without knowing their internal mechanism. All internal mechanism and parts are covered by some substance so we can not see it. It is hidden for us.

Some time when you know any person by name without knowing their inner detail such as family detail , their occupation etc.

So I just try to say it is simple concept of hiding things from end user. Don't take it in complicated way.

Now come in technical terms in C++ how it happen and how it is implemented.

I hope you all know about objects in brief every thing in real world is objects which have some properties.

So we can say that "Data Abstraction is a concept by which we can represent any object by it's important features only without knowing their inner details"

In C++ we can achieve data abstraction using classes , Header files & Predefined functions

➤ Class contain all data member and data function in single unit.

Note: We can only access that data members and functions by using access specifiers in C++. Access specifiers may be private or public. Private means it is accessible inside the block and public means it is accessible from any where inside the program.

#include <iostream>    
 using namespace std;    
 class Add    
{    
   private: int a, b, c; // private variables  
   public:    
   void addition()    
{    
   cout<<"Enter two numbers: ";    
   cin>>a>>a;    
   c= a+a;    
   cout<<"Addition of two number is: "<<c<<endl;    
}    
};    

int main()    
{    
  Add ad;    
  ad.addition();    
  return 0;    
}    


➤ Header files & Predefined functions are also example of data Abstraction because in case we only use function like pow() , abs() , exp() etc. with knowing internal functionality or mechanism or algorithm.

Header Files such as : <cmath> , <cctype> , <cstdlib> , <ctime> , <csignal> etc.

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{    
   int a = 3;  
   int b= 2;  
   int c = pow(a , b);         // pow( a , b ) is the  power function  
   cout << "Square of a is : " <<c<< endl;  
   return 0;  
}  







Friday, July 17, 2020

Call By Reference Method

In traditional C call by value is used in function call. But in C++ call by reference is used.

Let we understand how call by value and call by reference by the help of swap function , Also know their working and implementation.

For understanding Call By Reference in C++. let we first understand Call By Pointer in C.

CALL BY POINTER IN C

void swap ( int *a , int *b )

{

int temp ;

temp = *a ;

*a = *b ;

*b = temp ;

cout << a << b ;

}

void main ( )

{

int x , y ;

x = 10 ;

y = 20 ;

cout << x << y ;

swap ( &x , &y ) ;

cout << x << y ;

}

Explanation :

Here in above example int *a and int *b is pointer to integer type of variable. In C & (Ampersand) is used to indicate address but in C++ it is used as reference.





CALL BY REFERENCE IN C++

void swap ( int &a , int &b )

{

int temp ;

temp = a ;

a = b ;

b = temp ;

cout << a << b ;

}

void main ( )

{

int x , y ;

x = 10 ;

y = 20 ;

cout << x << y ;

swap ( x , y ) ;

cout << x << y ;

}

Output : 

                 x = 10  , y = 20
                 a = 20  , b = 10
                 x = 10  , y = 20


Explanation :

In C & (Ampersand) is used to indicate address but in C++ it is used as reference. 

Suppose if

 int a = 5 ;

 int &b = a;

In above statement &b = a it doesn't means that the value of a assign in address of b. But it mean that b is only the another or second name of a , So if a = 5 than b = 5. this is the concept of reference.
When we pass argument by reference the formal argument in called function will only change our name of actual argument in calling function. This mean that when the function is working with its own arguments it is actually working on the original data.


Call By Value methods

In traditional C call by value is used in function call. But in C++ call by reference is used.

Let we understand how call by value and call by reference by the help of swap function , Also know their working and implementation.

CALL BY VALUE

void swap ( int a , int b )

{

int temp ;

temp = a ;

a = b ;

b = temp ;

cout << a << b ;

}

void main ( )

{

int x , y ;

x = 10 ;

y = 20 ;

cout << x << y ;

swap ( x , y ) ;

cout << x << y ;

}

Output : 

                 x = 10  , y = 20
                 a = 20  , b = 10
                 x = 10  , y = 20


Explanation :

Here in call by value method it is clear that in swap function void swap ( int a , int b )  value of a & b can be change so it known as formal arguments. But the final value of a & b is x & y in main () function swap ( x , y ) which can not change so it is known as actual argument.

Now from the above details we can say that changes takes place in formal arguments does not reflect in actual arguments.  




Above diagram show that formal values are change but Actual values will remain same.