C++ friendship


Published on

An quick overview of Friend (keyword) in C++ with a bit flavor of emotion and a light sketch of humanity in it.

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C++ friendship

  1. 1. 2011 Saket Kr. Pathak Software Developer 3D Graphics[C++ Friendship - Indeed whats it ...?] A quick review of fundamentals of C with an prospective view of implementation in daily dayto day life.
  2. 2. C++ Friendship - Indeed whats it...?A few days back I was thinking, what is Friendship and who is the FriendIndeed. I was really a bit puzzled about the real definition and the practicalimplementation 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 wordand it becomes "Healthy Friendship" haa haa haa :). Most of the time I had seen galsusing it ... :)I thought to look for the antonyms of "Healthy Friendship", because sometimesantonyms give a hazy sense to understand the actual word. So, i surfed hard around allbig search engine as Google, Bing and Alta-Vista for "Unhealthy Friendship" but as theresult, completely failed ... :( . Then after all when every door i knocked didnt open ... :),then I came to my dearest C++ and felt the actual implementation of word "Friend" withcomplete sense.What we call friend in our life is not the actual sense of Friend, because Friend hasaccess to all the Private things/matters or hasnt any restriction and I am dead surethere is no-one who share their all private stuff/matters with any single one throughoutlife. ohhhooo hooo hooo :) Here I am talking about singles as i dont have any experienceof 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 ... Collegetime fiends ...(***common its teen age yaar haaa :) haa :), and every oneamong us knows each other well ... ;) ), then comes ... Office Colleges ... (***askthem, if they have free time after being jealous ... ?).But after everything there is a relationship which can be said as Friendship, if and only ifthey are ideal. Thats we are going to consider as my example. Lets See .... (of-coursecopy-past of snippet will work this time) ...Friend Functions (syntax C++)//____Class as a Ladyclass Gal{ private: std::string str_Nam_mood; public: Gal(); virtual ~Gal(); void disp(); //Friend - Function to access Private MembersSaket Kr. Pathak Page 2
  3. 3. C++ Friendship - Indeed whats it...? friend void my_Guy(Gal &my_friend, std::stringstr_what_happn);};Here in our class "Gal" has a friend as function, :) named as my_Guy(,). Whenever hemeets the gal and when she became sad, he changes her mood ... ;) (***execute this ifyou want to see how ?)Gal::Gal(): //Constructorstr_Nam_mood("I not feeling good :("){}Gal::~Gal(){} //Destructorvoid 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 ... ;). Asconceptual point of view function has the access to private members of class and It cantbe said as the member function, because friend function cant 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;}Lets have main function regarding this Gal class.int main(){ Gal aGal; aGal.disp(); my_Guy(aGal, "Have some Ferrero Rocher and Lets go Agra tosee Taj!"); aGal.disp();}//****Saket Kr. Pathak Page 3
  4. 4. C++ Friendship - Indeed whats it...?Friend Class (syntax C++)Here for explaining friend-class, Yes I am going to take help from previous example withsome addition. So, there will be a forward declaration of a class who is the friend of ourprevious class as;class Guy;Then a bit change in Gal class as;//____Class as a Ladyclass Gal{ friend class Guy; //friend class as a Soulmate private: std::string str_Nam_mood; public: Gal(); virtual ~Gal(); void disp();};Gal::Gal(): //Constructorstr_Nam_mood("I not feeling good :("){}Gal::~Gal() //Destructor{}void Gal::disp(){ printf("n%sn", str_Nam_mood.c_str());}Lets see how this class takes care of his friend who has given him complete access. Hefeels all the happiness that the Gal - class can imagine ... :)//___Class as a Manclass Guy{ public: Guy(); virtual ~Guy(); void please_change_ur_mood(Gal &dear_friend, std::stringouting);};Guy::Guy() //ConstructorSaket Kr. Pathak Page 4
  5. 5. C++ Friendship - Indeed whats it...?{}Guy::~Guy() //Destructor{}void Guy::please_change_ur_mood(Gal &dear_friend, std::stringstr_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, "Lets marry again and Wewill visit to Rome, this time!"); aGal.disp(); return 0;}Now thats a friend having complete trust and in-dependency with all the prospectiveviews of respect and so, ... :) ... I LOVE IT ... :) haaa haa haaa :). Hmmm :) as perprinciples 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/himto your friends, then she/he is just stranger for them and I remember a song "Dont talkto the Strangers ... Dont talk the Strangers ... Strangers Strangers" ... :) )Friendship is not inherited. So, Inheritance stands outside here ... :)Friendship is not transitive. (Its not mandatory that, your friend will has to be a friendof your mate.)Thats why I like C++. :)Ok so I dont think, Its needed to say Its completely my Prospective view to the conceptwhich may vary from you. So dont be personal, enjoy it ... at least try ... :)Same as always take care in someones style not mine, i used say catch you again :)Saket Kr. Pathak Page 5