1. 2011
Saket Kr. Pathak
Software Developer
3D Graphics
[C++ Friendship - Indeed what's it ...?]
A quick review of fundamentals of C with an prospective view of implementation in daily day
to day life.
2. C++ Friendship - Indeed what's it...?
A few days back I was thinking, what is Friendship and who is the Friend
Indeed. I was really a bit puzzled about the real definition and the practical
implementation of Friendship in our Daily life.
Trust me :) this is quite dangerous word for me and if I will be bit specific then, I never-
ever understand the meaning and sense, when a prefix added with this dangerous word
and it becomes "Healthy Friendship" haa haa haa :). Most of the time I had seen gals
using it ... :)
I thought to look for the antonyms of "Healthy Friendship", because sometimes
antonyms give a hazy sense to understand the actual word. So, i surfed hard around all
big search engine as Google, Bing and Alta-Vista for "Unhealthy Friendship" but as the
result, completely failed ... :( . Then after all when every door i knocked didn't open ... :),
then I came to my dearest C++ and felt the actual implementation of word "Friend" with
complete sense.
What we call friend in our life is not the actual sense of Friend, because Friend has
access to all the Private things/matters or hasn't any restriction and I am dead sure
there is no-one who share their all private stuff/matters with any single one throughout
life. ohhhooo hooo hooo :) Here I am talking about singles as i don't have any experience
of doubles or about further steps of life.
Before commenting here or blaming me wrong, think a minute and then decide. What ...
Parents ...(***knows everything just till your school days), then What ... College
time fiends ...(***common it's teen age yaar haaa :) haa :), and every one
among us knows each other well ... ;) ), then comes ... Office Colleges ... (***ask
them, if they have free time after being jealous ... ?).
But after everything there is a relationship which can be said as Friendship, if and only if
they are ideal. That's we are going to consider as my example. Let's See .... (of-course
copy-past of snippet will work this time) ...
Friend Functions (syntax C++)
//____Class as a Lady
class Gal
{
private:
std::string str_Nam_mood;
public:
Gal();
virtual ~Gal();
void disp();
//Friend - Function to access Private Members
Saket Kr. Pathak Page 2
3. C++ Friendship - Indeed what's it...?
friend void my_Guy(Gal &my_friend, std::string
str_what_happn);
};
Here in our class "Gal" has a friend as function, :) named as my_Guy(,). Whenever he
meets the gal and when she became sad, he changes her mood ... ;) (***execute this if
you want to see how ?)
Gal::Gal(): //Constructor
str_Nam_mood("I not feeling good :("){}
Gal::~Gal(){} //Destructor
void Gal::disp() //Member Function definition
{
printf("n%sn", str_Nam_mood.c_str());
}
Here our dear friend my_Guy(,) function changing the mood of his Gal ... ;). As
conceptual point of view function has the access to private members of class and It can't
be said as the member function, because friend function can't be accessed by scope-
resolution, dot or arrow operators with class.
void my_Guy(Gal &my_friend, std::string str_what_happn)
//Friend Function Definition
{
my_friend.str_Nam_mood = str_what_happn;
}
Let's have main function regarding this Gal class.
int main()
{
Gal aGal;
aGal.disp();
my_Guy(aGal, "Have some Ferrero Rocher and Let's go Agra to
see Taj!");
aGal.disp();
}
//****
Saket Kr. Pathak Page 3
4. C++ Friendship - Indeed what's it...?
Friend Class (syntax C++)
Here for explaining friend-class, Yes I am going to take help from previous example with
some addition. So, there will be a forward declaration of a class who is the friend of our
previous class as;
class Guy;
Then a bit change in Gal class as;
//____Class as a Lady
class Gal
{
friend class Guy; //friend class as a Soulmate
private:
std::string str_Nam_mood;
public:
Gal();
virtual ~Gal();
void disp();
};
Gal::Gal(): //Constructor
str_Nam_mood("I not feeling good :(")
{}
Gal::~Gal() //Destructor
{}
void Gal::disp()
{
printf("n%sn", str_Nam_mood.c_str());
}
Let's see how this class takes care of his friend who has given him complete access. He
feels all the happiness that the Gal - class can imagine ... :)
//___Class as a Man
class Guy
{
public:
Guy();
virtual ~Guy();
void please_change_ur_mood(Gal &dear_friend, std::string
outing);
};
Guy::Guy() //Constructor
Saket Kr. Pathak Page 4
5. C++ Friendship - Indeed what's it...?
{}
Guy::~Guy() //Destructor
{}
void Guy::please_change_ur_mood(Gal &dear_friend, std::string
str_what_happn)
{
dear_friend.str_Nam_mood = str_what_happn;
}
Now main() playing as the role of life and have a definition as,
int main()
{
Gal aGal;
aGal.disp();
Guy aGuy;
aGuy.please_change_ur_mood(aGal, "Let's marry again and We
will visit to Rome, this time!");
aGal.disp();
return 0;
}
Now that's a friend having complete trust and in-dependency with all the prospective
views of respect and so, ... :) ... I LOVE IT ... :) haaa haa haaa :). Hmmm :) as per
principle's of the concept, we need to notice a few points as;
Friendship is not mutual unless explicitly specified. (i.e. If you don’t introduce her/him
to your friends, then she/he is just stranger for them and I remember a song "Don't talk
to the Strangers ... Don't talk the Strangers ... Strangers Strangers" ... :) )
Friendship is not inherited. So, Inheritance stands outside here ... :)
Friendship is not transitive. (It's not mandatory that, your friend will has to be a friend
of your mate.)
That's why I like C++. :)
Ok so I don't think, It's needed to say It's completely my Prospective view to the concept
which may vary from you. So don't be personal, enjoy it ... at least try ... :)
Same as always take care in someone's style not mine, i used say catch you again :)
Saket Kr. Pathak Page 5