What is the access Specifier in cpp

In cpp We can customize the accessing of data member or member function.
What  does it mean, Let's consider you created a class named "acess".put the function and data which you want. 

Class access
{ 
    int data;
   
       int Function()

      {

        //definition of function;

      }

};
If data,function can be accessed by any where of program then Object Oriented Programming loss its data hiding concept.

So,CPP gives you permission to hide,show your data to the other part of program .
                for this purpose Cpp gives three Access Specifier
Public:

Private:


protected :
Public : if we want to share the data member every where of program we use this keyword .
    

                                    By default cpp apply this keyword for member ,like as above we haven't declare any access specifier ,so cpp by default apply it as public.
Thus it will be same as :

 class access 
  {
      public://By default 

      int data;

      int function()

         {
            
        //Definition of function ;
         
        }
  
}

Consider this program to understand ,how accessing is performed .


class dog
  {

     public:  // Public access specifier 

        int leg;

         void bark()
          {
             cout<<"Barking dog seldom bites ";
          }

   };
     
int main()

 {
        dog tomy ;

         cout<<"How many legs tom has ?";

                cin>>tomy.leg; 


// Above Accessing the "leg "data of that class from 

// out side of class i.e. in main function.

     if(tomy.leg<4) 
 
         cout<<"Tomy need extra care.";

     cout<<"\n See tomy bark or not .";

       tomy.bark(); // Accessing member function.

  }
   So, what we knew that if we have to access member of a class outside or by outsider function we must declare the member as Public.

Private:
 private is another access specifier of cpp ,as the name suggested ,if we have to make the member of a class private ,we use this access specifier .

   But the question is what the mean by making member private .
So,making the member private means


1.Private member can't be accessed by outside or another outsider function.
2.Private member is only accessible in the class in which it exist.
3.Private member only can be accessed by the function of that class.
  


 let's understand all the term by example .
class access 
  {
      private:  

      int data;

      int function()

         {
            
        //Definition of function ;
         
        }
  
}

This is how we declare member as private .

An example for accessing the private member :
class dog
  {

     private:

        int leg;

     public:

      void bark();   

   };
void dog::bark()

          {   
               
            cout<<"How many legs tom has ?\n";

              cin>>leg; //can accessed private member

// because function bark() is of same class.
 
            if(leg<4)
 
                cout<<"Tomy need extra care.\n";
            else

                cout<<"\n See tomy bark or not \n.";

          }
     
int main()

 {
        dog tomy ;

         cout<<"How many legs tom has ?\n";

              //  cin>>tomy.leg; Error we can't access

//private member by outsider function(main)  

    tomy.bark(); // Accessing member function since it

's public.

  }
output:
How many legs tom has ?
4
Tomy need extra care.
As we go through the program,we see that tomy.leg can't be accessed because it's being accessed by a function(main)which is not in the same class.
                                               But we can access the tomy.bark().i.e. bark() function of class dog.again from the bark() function we can accessed all member.because as i say earlier we can access the private member by the function of same class. 
   

Constructor overloading in cpp

If you have some confusion or you don't know what constructor is
go to this link 

what is constructor in cpp

Now, come to the topic ..what is constructor overloading  


constructor overloading  increase the flexibility of a class  

 see how
 => Using  constructor overloading we give different types of constructor
(differ by parameter within same class and thus we being able to initialize object in more than one way of same class .

Instead of one we can create many constructor in same class using the concept of constructor overloading...... 

understand  term using this code 

#include<iostream.h>
class room
  {
    int length ,width,height ;
     public:
   room() // default constructor 
    {

    }
   room(int x,int y) // constructor with
                      // two argument 
    {
      length=x;
      width=y;
    }
   room(int x,int y,int z) // constructor with 
                           // three argument 
    {
      length=x;
      width=y;
      height=z;
    }

  };
int main()
 {
   room obj; //calling default constructor 

   room obj1(20,10); // calling constructor with 
                     //     two argument 

   room obj2(20,10,12); // calling constructor with                         
                        //        three argument 
   }




 




What is constructor in cpp

To know about constructor firstly have a look on object and class , concept of constructor is generated due to object so its better to go to this below link if you are not familiar with object and class of cpp

class and object in cpp

Now ,constructor is  a function which give the memory existence of an object 


1.Constructor is used  to initialize the  data member of an object .

2.It's allocate the memory for object .

3.Constructor  has the same name as name of class.

 4.Whenever a object is created ,constructor is called 

5. Constructor doesn't return any value and it's get called whenever an object is created.

Ex-

#include<iostream>

using namespace std;

class Box

   {
int length,width,height ;
public: Box() // By default Constructor { } int volume() ; }; int Box ::volume() { cout <<"Enter the length height and width of box"; cin>>length>>width>>height ; return length*width*height; } int main() { Box b; 
//object created(constructor calling without parameter 

    
   cout<<"volume of the Box is :"<< b.volume();    
    return 0 ;

  }
                                                      output :  
                                           
 Enter the length height and width of box 
 
12 10 20  
                                                                                                 
 The volume of Box is :2400 

you can see here the constructor Box() ,with the same name of class but it's doing nothing. this is By Default constructor called by compiler
if we pass any value to this constructor that will be user defined constructor and benefit of user defined constructor is we can initialize the data member of object according to our need  .

Next program will explain this concept  .

#include<iostream>

using namespace std;

class Box

   {
     int length,width,height ;

     public:

     Box(int x,int y,int z)// user defined Constructor     {
       length=x;
       width=y;
       height=z;

     }
 int volume() ;
 };

int Box ::volume()

  {
       return length*width*height; 
  }

int main()
 { 
     Box b(10,20,12); // object created(constructor with parameter) 

    
   cout<<"volume of the Box is :"<< b.volume();    
    return 0 ;

  }

    
OUTPUT

volume of the Box is :2400

As we know a constructor is called whenever a object is created So in both example the constructor will be called but the only difference is
In 1st program the default constructor is called and in 2nd program user defined constructor is called .


we can categories the constructor in three types 

1.Default constructor  -Default Constructor is also called as Empty Constructor which has no arguments and It  is Automatically called when we creates the object of class  but Remember name of Constructor is same as name of class and Constructor never declared with the help of Return Type. Means we cant Declare a Constructor with the help of void Return Type. , if we never Pass or Declare any Arguments then this called as the Copy Constructors.


2. Parameterized Constructor  :-This is Another type  Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class. When we pass some Arguments to the Constructor then this will automatically pass the Arguments to the Constructor and the values will retrieve by the Respective Data Members of the Class.


class show 
 {
    show(int a,int b)  // constructor with parameter 
      {

      }   

 };


3.Copy constructor :-This is also Another type of Constructor. In this Constructor we pass the object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into the another Object of Class .This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we wants to Copying and When we are using or passing an Object to a Constructor then we must have to use the & (Ampersand or Address Operator.

#include<iostream>
using namespace std;
class show 
  {  
     public:
    int a ,b;
  //copy constructor 
    show(show &arg)  // taking object as parameter
     {
        show obj ;
        obj.a=arg.a;  
      }
};
int main 
 {
    show ob1,ob2;
     ob1.a=10; 
     ob1.b=20;
    ob2=show(ob1) // sending ob1 as parameter 
   return 0 ;

}




           

Next article is Constructor overloading 


 





Storage class in c

Storage class tells about the storage location ,life time and initial value of a variable/function.

There are four types of storage class in c

1.auto storage class
2.register storage class 
3.static storage class 
4.extern storage class 

let's understand with the help of example

1. auto storage class 
 example declaration-   auto int a ;
       
or simply -    int a ;


A)Storage location -> Storage location of this variable is main memory i.e. if we declare the variable in auto storage mode then that variable will store into RAM 

 B)Default value -> Default value of this type of variable is GARBAGE , i.e.until we don't assign any value to this variable it will contain GARBAGE value.
    
 C)Scope ->      local to the block in which it's defined. i.e. we can use this variable in only the block in which it's defined.

 D)LifeTime -> Till the control remains in block .

 understand with example !!

main() 

 { 

int a;

printf("%d",a);// garbage value(initial value)

a=10;

printf("%d",a); // 10 

{ 

int  b=10; 

printf("%d",b);

}

printf("%d",b); // error b is not in its scope


}

if we ignore the third printf
 //printf("%d",b);     then code will work smoothly and first printf the GARBAGE value because initial value of auto class is GARBAGE  ,
in next time 10 will be printed, but in next printf there will be an error 

because the variable b is declared in another block and we are printing the value of b in another block.
that's why we say the scope of auto variable is block in which that is defined
 .

2.Resister storage class 

  A)Storage location ->Storage location of this variable is CPU register,they are stored in CPU register.but sometimes if the CPU register are not free they will get stored in main memory.

  B)Default value -> default value of this type of variable is GARBAGE .initial value to this variable is GARBAGE.

 C)Scope  ->  Scope of this variable is local to the block in which it is defined .
 

 D)LifeTime ->Till control remains within the block 

ex- register int a ; 


3.Static storage class 

  A) Storage location -> Storage location of this type of variable is main memory .

  B) Default value -> Default value of this variable is ZERO , initially the variable will contain ZERO.

  C) scope -> Local to the block in which the variable is defined.

  D)Life Time -> Till the value of variable persists between different function calls.


Declaration method -
 static int a ;
program to understand the term 
int func(); 

#include<stdio.h>
void main() 
 {
    int i ;

 int a=10;

   for(i=0;i<10;i++)
       { 
          func();
       }
 }
int func()
  {
static int c=10;
    printf("%d\n",c);c++;
return (0);
  }
            
Output:









10
11
12
13
14
15
16
17
18
19
  

Here you can see the value of c is persisting in function calling 

 4. External storage class

  A)Storage -> Storage class of this type of variable is Main memory ,variable of this type is stored in main memory. 

  B) Default value -> Default value of this type variable is ZERO 

  C)Scope  -> Global ( we can access this variable from any where any block . it's accessible for all block of program.

D)Life Time -> As long as the program execution doesn't comes too an end . 

method of declaration

extern int a ;

remember one thing about extern variable 
extern int a ; 
// it's a declaration so we  have to define it later to use in program

like :

1
2
3
4
5
6
7
8
9
10
extern int a ;
#include<stdio.h>
int main() 
 {
     int a=10;
  printf("%d",a);

   
return 0;
 }



Output:
1
10





if we use it wihout definition like below
1
2
3
4
5
6
7
8
9
10
extern int a ;
#include<stdio.h>
int main() 
 {
     //int a=10;
  printf("%d",a);

   
return 0;
 }


it will be error ! 
Output:
1
2
In function `main':
undefined reference to `a'










Class and object creation in cpp

For the more Basic details about class and object of OOP go to this link
What is object oriented programming language

After reading above article i think you got the basic idea about class and object ,here now you will know how we create class and object in cpp

A basic program to understand these terms

#include<iostream>

using namespace std;

class basic

{

  int  length,width ;        // variable of class. 

   public: //described below,what mean of public is.  
  int  make_rect()        // function of class 
    {  
      cout<<"Enter the length of rectangle ";

        cin>>length;

      cout<<"Enter the width of rectangle "; 

        cin>>width;

     return length*width ;
   }
};

int main()
  {      

     int  result;
      basic b ;        // creating an object 

   // calling the function make_rect() of class basic 
       result=b.make_rect();
 
      cout<<"The rectangle area is "<<result;
  }


Now understand the program line by line

first we make class named "basic " and it has threee member
two variable and one function
function is using both variable and returning their multiplication .

  public: 

    This keyword is given just before the function make_rect().This is called access specifier.
                                       use of this is to make the function public i.e. this function can be accessible from outside the class .
                                           By default  the member of a class in cpp is private so variable "lenght,width " are private and no problem becouse these variable is using inside the class but the" function make_rect()" is using in main i.e. outside the class so we must have to make it public

That's why keyword public is given .

For know more about access specifier click below link 

what-is-access-specifier-in-cpp

now in main,
Basic b means we are creating the object of class "basic". because we can access member of a class using only object and we can two or more object of a class .

 b.make_fun();

that means we are accessing the function make_fun() of object b that is of class basic .

if we declare the variable as public in class then this will be also a valid statement and we can access the variable also from out side the class .

this program will illustrate this

      

#include<iostream>

using namespace std;
class basic
  {  
     public:  
      int  length,width ;  // variable of class basic.
    int  make_rect()       // function of class basic. 
      {           
         return length*width ;

      }
  };

int main()
 {      
    int  result;
       basic b ;  // creating an object 

      cout<<"Enter the length of rectangle ";
          cin>>b.length;

      cout<<"Enter the width of rectangle "; 
            cin>>b.width;

    // calling the function make_rect() of class basic 
          result=b.make_rect(); 

     cout<<"The rectangle area is "<<result;

 }


    

What is object oriented programming language

 Object oriented programming language is a paradigm of programming which use the concept of object and  class ,  if we look at the previous languages like c (procedural programming ) OOP is different .

In Object-oriented programming was developed because limitations were discovered in earlier approaches to programming. To appreciate what OOP does, we need to understand what these limitations are and how they arose from traditional programming languages.  In these languages the most demerit is the data used are less secure like  in c the global variable is used by all the function of a program .there is no any method to make the data private .If the program is too large then it's difficult to handle and its to complicated to understand because we can't classified the data and function to a specific area as we do in OOP using Class and Object
Here you can see how a function is called from another function and we can call any function from any onewe can't make any function private .These are the demerit of the procedural language and OOP is used to come over from these situation .We will see how the function is called ,how is data is used in OOP but first understand that what is the Object and Class .
To understand the concept of class and object look at the image given below , a car company first make the template of car and then give the physical existence   so the template of the car is the class and when the cars comes to market that is object .and these object belongs to the class car .
So we can define the class as template of  data and function and in particular kind of object ,and the instance of the class is called Object.See how the class and object is used in programming (cpp) .If you are little bit familiar to the C , then consider on the use of structurewe use the structure like this .struct car {    char name[20];    int wheel ;    int grear ;  }; Now if we have to use this structure (making physical existence ) then we write like thisstruct car maruti ,swift,bmw ;now we use these variable for programming need ..................The concept of Class and Object is much more similar to thisDeclaring a class(making template of class) class car { char name[20];    int wheel ;    int grear ; }; Now if we have to use these data and it should be in physical existence .....that is the reason we make Object car maruti ,swift,bmw ;  ( making the object)we can now use the variable of maruti ,swift,bmw  as we need . We can define a function in class as we can't in structuresecond thing is the all member of structure is public but a class member can be  public or private both(depending on programmer how he defines )In next article you will be familiar with c++ programming with concept of class and object . :)  






C program for quick sort

/*Program of sorting using quick sort through recursion*/



#include<stdio.h>

#define MAX 30

enum bool { FALSE,TRUE };



void display(int arr[],int,int);



void quick(int arr[],int low,int up);





main()

{



       int n,i;  int array[MAX];

    printf("Enter the number of elements : ");

         scanf("%d",&n);

  for(i=0;i<n;i++)

     {

     printf("Enter element %d : ",i+1);

           scanf("%d",&array[i]);

      }

   printf("Unsorted list is :\n");



display(array,0,n-1);



printf("\n");



quick(array,0,n-1);



printf("Sorted list is :\n");



display(array,0,n-1);



printf("\n");



return 0;



}       /*End of main() */





void quick(int arr[],int low,int up)

  {

       int piv,temp,left,right;

enum bool pivot_placed=FALSE;

left=low;

right=up;



piv=low; /*Take the first element of sublist as piv */



if(low>=up)

  return ;



printf("Sublist : ");



display(arr,low,up);



/*Loop till pivot is placed at proper place in the sublist*/



 while(pivot_placed==FALSE)

    {



   /*Compare from right to left  */



    while( arr[piv]<=arr[right] && piv!=right )

    

      right=right-1;

   



      if( piv==right )



pivot_placed=TRUE;



if( arr[piv] > arr[right] )

 {

       temp=arr[piv];

       arr[piv]=arr[right];

       arr[right]=temp;

       piv=right;

}



/*Compare from left to right */



while( arr[piv]>=arr[left] && left!=piv )

      left=left+1;



if(piv==left)

    pivot_placed=TRUE;



if( arr[piv] < arr[left] )

 {

     temp=arr[piv];

     arr[piv]=arr[left];

     arr[left]=temp;

     piv=left;

 }



}   /*End of while */



    printf("-> Pivot Placed is %d -> ",arr[piv]);



   display(arr,low,up);



    printf("\n");

 

   quick(arr,low,piv-1);

 

   quick(arr,piv+1,up);



 }/*End of quick()*/





void display(int arr[],int low,int up)

  {



    int i;

      for(i=low;i<=up;i++)

        printf("%d ",arr[i]);

}                                                         

You may like these .

 1. Bubble sort in c
 
 2. Heap sort in c
 
 3. Insertion sort in c