Wednesday, 7 January 2015

CONST FUNCTION OVERLOADING

Const function overloading

Today we are try to overload const function:--
class A
     {
             public:
                    int func(int a) const;
                    int func(int ) ;
     };
int A:: func(int x) const
       {
                  cout<<"I am in Const"<<x;
       }
int B::func(int y)
      {
                  cout<<"I am in func"<<y;  
      }
int main()
     {
            A y;
            A const x;
            x.func(20);
            y.func(10);
    }
Output:-
       I am in Const 20
       I am in func 10
if you observe the output, you  can see a const object called the const function and object y called the func().
It means C++ allows member methods to be overloaded on the basis of const type.

Extern and Static

                                                  Why we cant used static with extern ?                                                  



First  I try to explain static:--
        Static as a global variable is visible only in its defined file that means internal linkage and the value of static variable exist until the termination of program.
       Static as a local variable or in a function or block it's visibility limit to that block. The value assigned to static variable during the first call remains in it for next call.
Extern
      Extern is used to give a reference of global variables so that it is visible to all  files in a project and you can use it.
But as per my question you can't used static with extern.........
for example you have a .h file in which you declare a variable extern int i;, now you want to used it as static in .c or .cpp file like "static int i=0" then here you  get a compiler error because of visibility scope of extern and static and due to restriction of static you can't used one static variable in different file.



Tuesday, 6 January 2015

Mutex and Binary Semaphore

                                Difference Between Mutex and Binary Semaphore in progrmming

So Guys first we have to know that what is mutex and binary semaphore............
Mutex ...............

if you extract this word you got two different words "Bhaichara" ha.....ha...... means
mutual exclusion, i think you got some idea about Mutex
Mutex is generally used in multithreaded programming, where two or more threads fight together to
access part of code or variable at a same time in it's critical section.
So for creating peace between two or more "DAEMON" like thread which fight to eat whole things at a same time we have a solution called "mutex".
but we need to go more tch..
so, Mutex control access to shared single resources between two or more thread.
Explanation:---
       Lets thread  T1 , T2 , and T3 try to access a critical section.
       For that we have mutex_lock or mutex_unlock.....
                    Now what T1 do before going to critical section, it locks the mutex by using mutex_lock...means door closed no any thread can access critical section that time because they are not
Rajnikant but whenever T1 unlock the mutex by using mutex_unlock then T2 or T3  can get access to critical section and lock the mutex.  Untill it  finished it works in lock room "dirty mind" int it's critical section..............
before going to binary semaphore i want to explain Semaphore...........
A semaphore control access to shared a pool of resources, basically it is a integer variable which may be greater than 1.
For example we have a semaphore variable with int i=4; then we can give  mutex _lock to more than one thread at a same time untill i becomes to 0.
Now question arises can one thread have more than one lock and answer is big YES. According to your code or logic a thread may have more than one lock.
Now come to the main point Binary Semaphore

Binary Semaphore: - It is also a integer variable like semaphore but with only 0 or 1 values and when you are trying to explain it , you get confused between mutex and semaphore because both seems to be same but actually it is not. Let's feel it ...........
In Binary semaphore when one thread try to access critical section it set the value of int i to 0 and untill it finished the work no any thread can access it and after accessing the critical section it again set to int i=1 and continue..................
it same as mutex but here is one difference mutex has ownership means thread which acquire the lock only that thread can free it but in binary semaphore other thread can also free it  and get access to critical section
you can say that
A mutex is semaphore but a semaphore never be a mutex..................



Monday, 29 December 2014

Why we used const function???

We Used Const function why??

#include<iostream>
using namespace std;
class A
     {
           private:
                    int a=5;
           public:
                int func1() const;
     };
int A::func1() const
     {
           cout<<a;
     }
int main()
     {
          A const a;
          a.func1();
     }
Now here i have a small example of const function................
So, why we need const function 
because a const function can't able to modify the class member variable in it. For example we have 
a meter reading class which reads the value of meter in km in its member variable ""M" now we want to display it so we use use a show () const which is only able to read it and display it.
                                   Let's you want to modify a class member variable in const function so you can use mutable keyword ........

GCC Compiler Warning: extended initializer lists only available with c++0x

How do you remove the compilation warning : 
extended initializer lists only available with c++0x  ??

You can remove it by compiling your code with :-
g++ -std=c++0x test.cpp

Thursday, 25 December 2014

Declaring a const variable outside the class ??? Why and How

class A
        {
                 const  int    a; 
                 a=5;/////////// error
                 static const int b =6 ; running
         }
now here we can see that const int  may causes error in some compiler because
const variable specifies whether a variable is modifiable or not. But  initializing anywhere other than constructor causes default initialization followed by assignment...
so a is a const and you can't assign value to it....
Now question is that how can you define a const variable outside the class:::
                            here you can do that
                                              A():a(5)
                                                {
                           
                                                 }
whenever constructor is called value of a is initialized by 5

Wednesday, 24 December 2014

Initilization and assignment

                                    Difference between initialization and assignment
*Initialization is about creating a object but assignment is about set some value to object.
 for example:
                      int x=5; ///////initialization
                          x=6; assignment