C++ Programming & Tutorials for Beginners

Friend Class

Home  »  C++ Programming  »  Friend Class


     In case of friend function, a fucntion is made friend to a class in order to access its private data members. But in Friend classes, an entire a class is made a friend to another class. By creating the friend class, all the member functions of the friend class can access the private member data of another class. In other words, we can say that the member functions of a class can all be made friends at the same time when you make the entire class a friend the general syntax of a friend class is given below:

clas first
{
  private:
    _______________
    _______________
  public:
    _______________
    _______________
  friend class_second;
  //friend class declaration class_second
  //Actual definition of friend class_second
  {
    _______________
    _______________
  }
};

In above given syntax second is a friend class of class first. From the class second, all the members of the class first can be accessed. The following example illustrates the use of friend class.








This Pointer

     A Pointer variable is generally used to hold the memory address of the variables. Pointer can also be used to access the data of another variables indirectly. The this pointer also work as a simple pointer variable to access the address of the class. The this pointer may be used to return data items to the called. The this pointer is used in unary operator overloading for the postfix operation for returning the address of data items of overloaded function. The keyword this is a local variable available in the body of any no static member function. The this pointer does not need to be declared and is rarely referred to explicity in a function definition. The use of this pointer is quite obsolete. The following example gives the use of this pointer.













Previous
Next Post »