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


Signature vs Return Type

hey...............
Why we can't have different signature with different return type ?
look ...........
class A
       {
              public:
                    int name1 (int , int);
                    float name1(int, int, int);
                    char name1(int , int);
        }
int main()
   {
            cout<<"Coming to calll ";  
            int a;
            a = name1(5,6);        //////oops cause compiler error;
    };
int A::name1(int ,int)
   {
                  --------
   }
float A::name1(int ,int, int)
   {
                  --------
   }
char A::name1(int ,int)
   {
                  --------
   }


so we got compilation error when we different signature with different return type but WHY?????????
coz....... compiler unable to find the return type of called function ...........





* and &

                                         Difference Between Pass by Reference and Address
Let's we have two variable
one *p  (::) another   is &p \_
now see the difference 
just kidding.................................................
1. *p can be redirected means it can point to different address  but &p can't be redirected...
2. *p can directly access values using '.' or '->' operator but &p can't do it again man "shit happen with &".
3. *p can be initialized as NULL but f***k &p can't ......
Happy Reading.........................