Thursday, January 19, 2012

C++ - Defining friendship in more than one class - Method Two


//Friends in multiple classes - Defining friendship in more than one class.
#include<iostream>
using namespace std;

//forward declaration of classes
class Circle;
class Square;

class Circle
{
char cColour[20];
public: Circle( char x[20])
{ strcpy(cColour,x);}
friend bool compare(Circle, Square);

};

class Square
{
char sColour[20];
public: Square( char y[20])
{ strcpy(sColour,y);}
friend bool compare(Circle, Square);
};

bool compare(Circle p, Square q)
{
int cmp;
cmp = strcmp(p.cColour,q.sColour);
if (cmp==0)
return true;
else
return false;


}


void main()
{
Circle cir("REED");
Square sqr("RED");
if (compare (cir,sqr))
cout<<"same"<<endl;
else
cout<<"diff"<<endl;

system("pause");
};

C++ - Defining friendship in more than one class - Method One


//Friends in multiple classes - Defining friendship in more than one class.
#include<iostream>
using namespace std;

//forward declaration of classes
class Circle;
class Square;

class Circle
{
char cColour[20];
public: Circle( char x[20])
{ strcpy(cColour,x);}
friend void compare(Circle, Square);
};

class Square
{
char sColour[20];
public: Square( char y[20])
{ strcpy(sColour,y);}
friend void compare(Circle, Square);
};


void compare(Circle p, Square q)
{
int cmp;
cmp = strcmp(p.cColour,q.sColour);

if (cmp == 0)
cout<<"Same Colour"<<endl;
else
cout<<"Diff Colour"<<endl;
};

void main()
{
Circle cir("REED");
Square sqr("RED");
compare (cir,sqr);
system("pause");
};