Celestial Coding

Archive => Archives => C++ => Topic started by: Celestialkey on January 14, 2009, 06:01:01 am

Title: [31] Friend's and how they can touch your privates
Post by: Celestialkey on January 14, 2009, 06:01:01 am
PrintMonster.h
Code: [Select]
/*
The friend function can be consider a friend with benefits.
It in itself is seperate from the class members, but it
, just as friend with benefits, is able to touch and manipulate
the classes private parts.
*/

class cMonster
{
public:
cMonster();

void PrintData();

friend void GetData(cMonster MD); // Friend!

private:
int X;
int Y;
};


PrintMonster.cpp
Code: [Select]
#include "PrintMonster.h"
#include <iostream>
using namespace std;

cMonster::cMonster()
{
X = 20;
Y = 30;
}

void cMonster::PrintData()
{
cout << "x = " << X << ", y = " << Y << "\n";
}



void GetData(cMonster MD) // We accept the private parts with a friend function
{
cout << "Current Val X = "<< MD.X << endl; // Read the private data.
cout << "Current Val Y = "<< MD.Y << endl; // Read the private data.
}

main.cpp
Code: [Select]
#include <iostream>
#include "PrintMonster.h"

using namespace std;

int main()
{
cMonster Goblin;

GetData(Goblin);
cout << endl << endl;

Goblin.PrintData();
getchar();
return 0;
}
Title: Re: [31] Friend's and how they can touch your privates
Post by: IonDuel on January 14, 2009, 09:22:17 pm
Finally!  Lol.  Ive been waiting for this one!  Ive never needed friend classes, but i'm sure its good to know.
Title: Re: [31] Friend's and how they can touch your privates
Post by: Celestialkey on January 15, 2009, 01:57:56 am
I never really use them either.
Title: Re: [31] Friend's and how they can touch your privates
Post by: wersD on January 17, 2009, 09:03:21 am
um...interesting title...
Title: Re: [31] Friend's and how they can touch your privates
Post by: Celestialkey on January 17, 2009, 12:37:09 pm
It is how I learned by my teacher lol. It is how Kiera learned as well.
Title: Re: [31] Friend's and how they can touch your privates
Post by: IonDuel on January 17, 2009, 01:04:31 pm
It is how I learned by my teacher lol. It is how Kiera learned as well.
If youve heard of Falco (Gyrovorbis on youTube) he talks about them like friends with privileges lol.  Like, you can touch her junk, and she can touch yours lol.  Im going to post some of Falcos "Adventures In Game Development" movies so you guys know their unlimited power lol.