Friend Function : As we know, private class members can not be accessed from outside the class. By using private class members, we can reduce the possibility of a program misusing a member's value, since the program must access the class members using an interface function. Sometime, we would like two classes to share a particular function. In such situation, C++ allows the common function to be made friendly with both classes, thereby allowing the function to have access to the private data of these classes. Such a function need not be a member of any of these classes.
To make an outside function 'friendly' to a class, we have to simply declare this function as a friend of the class. The function declaration should be preceded by the keyword friend. While the function definition does not use either the keyword friend or scope resolution operator (::). The function that are declared with the keyword friend are known as friend functions. A function can be declared friend in any number of classes.
A friend function possesses certain characteristics :
- It can be declared either in the public or private part of a class without affecting its meaning.
- Usually, it has objects as arguments.
- Its scope is not limited to the class in which it has been declared as friend. It can not be called using the object of that class. It can be invoked like a normal function without the help of any object.
- Unlike the normal member functions, it can not access the member names directly. It has to use an object name and dot membership operator with each member name.
Example,
#include <iostream.h>
class sample
{
int a;
int b;
public :
void setvalue( int al, int bl)
{
a = a1;
b = b1;
}
friend float mean (sample s); //friend declared
};
float mean(sample s) //function definition
{
return float(s.a + s.b) / 2; //accessing private members
}
void main ()
{
sample x; //create an object x
x.setvalue(25,40);
cout << "Mean Value : " << mean(x) << endl;
}
All the member functions of one class can be declared as friend functions of another class. In such a case,the class is called a friend class. This can be specified as follows:
class X;
class Z
{
......
friend class X; //all member functions of X are friends to Z
};
Constructors : These are special member functions that are used to initialize the objects, as soon as they are created. Thst is, you can create an object and simultaneously pass the value to the parameters of the object. The constructors should have exactly the same name as that of class of which they are members. The constructor function does not have a return value. They should be declared in public section. To understand how constructors are used, let's try the example of displaying the area of square by using constructors :
Example,
class Square
{
public :
int dim;
Square(int d); //Declaring a Constructor Function
{
dim = d;
}
void DisplayArea()
{
cout << "The area of the square : "<<dim*dim;
}
void main ()
{
Square S1(10), S2(15); //the constructor function will be called automatically
cout << "The dimension of the square is : " << s1.dim;
S1.DisplayArea();
cout << "The dimension of the square is :" << s1.dim;
S2.DisplayArea();
}
The output of the program will be :
The dimension of the square is 10
The area of the square : 100
The dimension of the square is 15
The area of the square : 225
Copy Constructor : It is used to declare an object and the values of these objects are initialized from other object. Note that a copy constructor takes a reference to an object of the same class. We can not pass the argument by value to a copy constructor.
Example,
#include <iostream.h>
class Square
{
int dim;
public :
Square();
{ }
Square (int I)
{
dim = I;
}
Square (Square & j) //passing the argument through reference not by value
{ dim = x.dim; }
void Display()
{
cout << dim;
cout << "\n The area is : "<<dim*dim;
}
};
main ()
{
Square Square1(10);
Square Square1(Square2); //declaring a copy constructor
Square Square3 = Square1; //declaring a copy constructor
cout << "\n length of square1 " ;
Square1.Display();
cout << "\n length of square2 " ;
Square2.Display();
cout << "\n length of square3 " ;
Square3.Display();
}
The output is :
Length of square1 : 10
The area is : 100
length of square2 : 10
The area is : 100
Length of Square3 : 10
The area is : 100
Dynamic Constructors : They are used to allocate memory to objects at the time of their creation. Normally we can allocate fixed amount of memoryby using array but in many situations, we are not sure how much memory we need until runtime. For example, space required to store the length of string entered by user.
In such case, we use 'new' operator. This operator obtains memory space from operating system and returns a pointer to its starting point. We can use 'new' operators in constructors to dynamically assign storage space when objects are created.
Keywords: class, constructor, copy constructor, dynamic, friend function




