Search This Blog

Wednesday, August 6, 2008

Friend Function

A friend function is not a class member and yet it can have access to private and protected members of a class. Friends are not called by using member selection operators such as “ . or ->” unless they are members of another class. Friend functions can be defined inside a class declaration. Friend functions declared within a class are inline just like other inline member functions. These functions act as though they were defined immediately after all class members have been seen but before the end of the class declaration. Even though friend functions are declared inside class declarations, they are not considered in the scope of the enclosing class. They are normally considered in the file scope. An entire class can be declared as a friend of another class.

To declare a function as a friend of a class, put the word “friend” before the function prototype in the class definition. For example, you want to show that ClassTwo is a friend of ClassOne, simply put the word “friend” in front of ClassTwo like:

friend class ClassTwo;

This line should be inside the definition of class ClassOne and declared explicitly. The friendship between ClassOne and ClassTwo is neither symmetric nor transitive. This means that ClassTwo is a friend of ClassOne but ClassOne cannot be assumed to be a friend of ClassTwo. Same concept applies when you have multiple classes connected by multiple friendships. Friendship can only go one way.

A partial example of friends accessing private members of a class goes something like this:

class ClassOne{
// friend declaration should appear before the declaration of public and private member function
friend void ClassTwo ( ClassOne &, int); // friend declaration
public:
Count( ) { x = 0; } // constructor
Void print( ) const { cout << x << endl;} // output
Private:
Int x; // data member
};

//ClassTwo is declared as a friend function of ClassOne
void ClassTwo( ClassOne &c, int val)
{
c.x = val;
}
int main( )
{
ClassOne counter;
counter.print ( );
ClassTwo( counter,10); // set x with a friend
Counter.print( );
Return 0;
}

The purpose of using friend function is to improve performance. Sometimes when a member function cannot be used in a certain operation, using friends would be able to solve that problem.

Tuesday, August 5, 2008

Devide Tree Menu in Two Diff Column

public void GetCategoryTree()
{

BpBL.ProjectCategory objCat = new BpBL.ProjectCategory();

// PopulateTreeViewControl(categoryList);
DataTable dtTemp= new DataTable();
DataTable dtSubCat = new DataTable();
DataTable dtDemoTable1;
DataTable dtDemoTable2;

dtTemp=objCat.SelectMainCategory();
dtDemoTable1 = dtTemp.Clone();
dtDemoTable2 = dtTemp.Clone();
int count = Convert.ToInt32(dtTemp.Rows.Count);
int i=0;

foreach(DataRow drMain in dtTemp.Rows)
{

if (i++ % 2 == 0)
{
dtDemoTable1.LoadDataRow(drMain.ItemArray, true);
}
else
{
dtDemoTable2.LoadDataRow(drMain.ItemArray, true);
}
}
dtTemp.Clear();
TreeNode parentNode = null;
//Binding First Tree View
foreach (DataRow dr in dtDemoTable1.Rows)
{
parentNode = new TreeNode(dr["CategoryName"].ToString(), dr["Id"].ToString());
dtSubCat = objCat.SelectSubCategory(dr["Id"].ToString());
foreach (DataRow drSubcat in dtSubCat.Rows)
{
// TreeNode childNode = new TreeNode(product.ProductName, product.ProductID.ToString());
TreeNode childNode = new TreeNode(drSubcat["CategoryName"].ToString(), drSubcat["Id"].ToString());

parentNode.ChildNodes.Add(childNode);
}

parentNode.Collapse();

treeViewProjCat.ShowCheckBoxes = TreeNodeTypes.All;
treeViewProjCat.Nodes.Add(parentNode);

}

//Binding 2nd Tree View

foreach (DataRow dr in dtDemoTable2.Rows)
{
parentNode = new TreeNode(dr["CategoryName"].ToString(), dr["Id"].ToString());
dtSubCat = objCat.SelectSubCategory(dr["Id"].ToString());
foreach (DataRow drSubcat in dtSubCat.Rows)
{
// TreeNode childNode = new TreeNode(product.ProductName, product.ProductID.ToString());
TreeNode childNode = new TreeNode(drSubcat["CategoryName"].ToString(), drSubcat["Id"].ToString());

parentNode.ChildNodes.Add(childNode);
}

parentNode.Collapse();

treeViewProjCat2.ShowCheckBoxes = TreeNodeTypes.All;
treeViewProjCat2.Nodes.Add(parentNode);

}
}