Thursday, 5 November 2015

How to Dynamically create Multiple Tabpages with Gridview in Each Tab on Button click


Below is the code how to Dynamically create Multiple Tabpages with Gridview in Each Tab.



private void btn_new_Click(object sender, EventArgs e)
        {
       

//For creating the Headers such as name ,values,class


            DataGridViewTextBoxColumn Name = new DataGridViewTextBoxColumn();
            Name .HeaderText = "Name";
            Name .Width = 150;

 DataGridViewTextBoxColumn values= new DataGridViewTextBoxColumn();
            values.HeaderText = "values";
            values.Width = 150;

 DataGridViewTextBoxColumn class= new DataGridViewTextBoxColumn();
            class.HeaderText = "Class";
            class.Width = 150;

 DataGridViewTextBoxColumn  Time = new DataGridViewTextBoxColumn();
            Time  .HeaderText = "Time";
            Time .Width = 150;

 DataGridViewTextBoxColumn A = new DataGridViewTextBoxColumn();
            A .HeaderText = "A";
            A .Width = 150;


 DataGridViewTextBoxColumn  B = new DataGridViewTextBoxColumn();
            B .HeaderText = "B";
            B .Width = 150;


//now add tab page in main tab.


          TabPage tabpage = new TabPage(class_new_tab.new_tab_name_returned);
            tabpage.BackColor = Color.White;
            tabpage.Name = class_new_tab.new_tab_name_returned;
            tabpage.Width = 200;
            tabpage.Height = 20;

           Tab.Controls.Add(tabpage);


// now add gridview ,this gridview you have to add in tab page..


             DataGridView dgv = new DataGridView();
            dgv.Dock = DockStyle.Fill;
            dgv.BackgroundColor = Color.White;
            tabpage.Controls.Add(dgv);
            dgv.Columns.Add(Name);
            dgv.Columns.Add(values);
            dgv.Columns.Add(class);
            dgv.Columns.Add(Time);
           dgv.Columns.Add(A);
            dgv.Columns.Add(B);
            dgv.RowHeadersVisible = false;//this will hide header from gridview
}










           

Difference between Structure and Union?




STRUCTURE

UNION

1.The keyword  struct is used to define a structure
1. The keyword union is used to define a union.
2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes.
2. When a variable is associated with a union, the compiler allocates the  memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
3. Each member within a structure is assigned unique storage area of location.
3. Memory allocated is shared by individual members of union.
4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
5 Altering the value of a member will not affect other members of the structure.
5. Altering the value of any of the member will alter other member values.
6. Individual member can be accessed at a time
6. Only one member can be accessed at a time.
7. Several members of a structure can initialize at once.
7. Only the first member of a union can be initialized.

Wednesday, 29 April 2015

What is name hiding in C++?


Let us understand name Hiding concept with an example:


#include<iostream>
#include<conio.h>
using namespace std;

class A
{
public:

    void fun()
    {
    }
    void fun1()
    {
    }
};

class B:public A
{
public:
    void fun()
    {
    }
    void fun1(int x)
    {
    }
};

void main()
{
    B obj;
    obj.fun();
    obj.fun1();
}


in the above program it looks like the call of function fun1() in the main should run fine


  • But, in reality that method call results in an error which will say something like “error: no matching function for call to "error C2660: 'B::fun1'"
  • Why would it return that error? The Class B clearly derives from the Class A so it looks like the Class B object in the main function should have access to the fun1() function declared inside the Class A in addition to the fun(int) that accepts an integer  and is defined inside the Class B.



The reason the function call above results in an error is because of a property of C++ called name hiding


Let understand more about Name Hiding by taking another example:

#include<iostream>
#include<conio.h>
using namespace std;

class A
{
public:

    void fun1(string s)
    {
    }
};

class B:public A
{
public:
    void fun1(int x)
    {
    }
};

void main()
{
    string s;
    B obj;
    obj.fun1(s);

}

once again it look life there will be no issue in function fun1() calls from main function.
But when you try to compile your code it will throw an error " C2664: 'B::fun1' : cannot convert parameter 1 from 'std::string' to 'int'"


So, what should be done here?

Functions in derived classes with the same name as the functions in the parent classes, but that do not override the parent classes’ function are considered to be bad practice because it can result in errors just like the one above. So, having a function like fun1()  above is considered to be bad practice. This is technically not method overloading either, because overloading functions is done within the same class – not in an inheritance hierarchy.


The reason it’s bad practice is because it leads to ambiguity, and it’s much easier to just give the functions different names instead.

Solution to Name Hiding Problem

 The problem of name hiding can be solved ,All we have to do is implement the using keyword. let us understand by help of an example:


#include<iostream>
#include<conio.h>
using namespace std;

class A
{
public:

    void fun1(string s)
    {
    }
};

class B:public A
{
public:
    void fun1(int x)
    {
    }
    using A::fun1;
};

void main()
{
    string s;
    B obj;
    obj.fun1(s);

}



When we redeclare someFunction in the scope of Class B (with the “using” keyword), it makes both versions of that function (from both Class A and Claas B) visible in just the Classs B

Tuesday, 28 April 2015

Functions Pointer in C++


  • Function Pointer are similar to Pointers , Only difference is instead of pointing to a variables , function pointer points to Functions.

Let us go through one example for better understanding:

  • int Arr[5]
As we know Arr  is constant pointer to an 5 element array , in order to retrieve value we will deference the pointer either by Arr[index] or *Arr, the corresponding Array values are returned.


  • int fun();
If you guessed that fun is actually a constant pointer to a function, you are correct. When a function is called (via the () operator), the function pointer is deferenced, and execution branches to the function.


Similar way is possible to declare a non-constant pointer to a function, syntax for writing a non -constant pointer to a function is :
  •  int (*fun)(int);
Parenthesis are necessary for precedence reason else it will be interpreted as pointer to a integer instead of pointer to a function.


fun is a pointer to a function that takes one integer parameter and return an integer , it will point to any function that matches its Signature.

example:

#include<iostream>
#include<conio.h>
using namespace std;

int fun(int a)
{  
    a++;  
    return a;
}

int main()
{
    int (*Pfun)(int);
    Pfun = fun;
    int b = Pfun(10);
    cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
    return 0;

}

Note: the signature (parameters and return value) of the function pointer must match the signature of the function. lets see few examples for better understanding.

// function prototypes
int foo();
double goo();
int hoo(int nX);

// function pointer assignments
int (*pFcn1)() = foo;           // okay
int (*pFcn2)() = goo;          // wrong -- return types don't match!
double (*pFcn3)() = goo;   // okay
pFcn1 = hoo;                      // wrong -- pFcn1 has no parameters, but hoo() does
int (*pFcn3)(int) = hoo;    // okay


few Examples of Pointer to function:

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;


//Example 1: simple

If a function pointer needs to point to functions that take parameters, the data-types of these parameters can simply be separated by commas in the function pointer declaration. Take for example the following:

int fun(int a , int b)
{    
    return a+b;
}

int main()
{
    int (*Pfun)(int, int );
    Pfun = fun;
    int a= Pfun(10,12);

    cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
    return 0;

}

//Example 2: 
if you want that caller function have flexibility to control how operation need to be done by simply reusing old snippet of code in-spite of writing two separated functions.


like in below code snippet function fun is being used both for add and multiply in-spite of writing separate fun function for add and multiply.

int fun(int a,int (*operation)(int , int))
{    
    
    return a+operation(10,5);   
     
}

int add(int a, int b)
{
    return a+b;
}
int multiply(int a, int b)
{
    return a*b;
}

int main()
{
     int x = fun(10,add);
     int y = fun(12,multiply);
    //int b = Pfun(10);
      
     cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
     cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
    return 0;

}


//Example 3:

We can also create arrays of function pointers. These aren't as scary as they sound, and they're simply created much like you would with an array of "normal" pointers, by specifying a number of elements in square brackets next to the pointer name. If we wanted a function pointer array which contained pointers to both functions "add" and "multiply" in elements of index 0 and 1, we could use the following:


int add(int a, int b)
{
    return a+b;
}
int multiply(int a, int b)
{
    return a*b;
}

int main()
{
     int (*fun[2])(int , int);
     fun[0]= add;
     fun[1]= multiply;
     int x = fun[0](10,12);
     int y =fun[1](1,3);
      
     cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
     cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
    return 0;


}