Returning Objects

  • A function cannot only receive objects as arguments but also can return them.
  • Following example illustrates how an object can be created (within a function) and returned to another function.

  • Example

    Friend function Example

    Example

    Friend class example

    Example

    • This is a good way out given by C++ to avoid restrictions on private variables.
    • But this should be used with caution though.
    • If all the functions and classes are declared as friends, then the concept of encapsulation and data security will be totally lost.

    Disadvantage of Friend Class/Functions:
    • The aim of using object oriented programming is to protect the data members from accidental or deliberate modifications.
    • The data is to be accessed only through the member functions of that class.
    • By declaring a friend, the private variables can be accessed by the friend which defeats the purpose of OOP.


    Friend Functions << Previous

    Next >> Operator Overloading

    Our aim is to provide information to the knowledge seekers.


    comments powered by Disqus






    Footer1