Friend Function in C++= Can one friend function access private member

Friend Function in C++= Can one friend function access private member

Page 1 of 1

3 Replies - 4635 Views - Last Post: 11 August 2007 - 11:40 AM Rate Topic: -----

#1 ketu0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-August 07

Friend Function in C++= Can one friend function access private member

Post icon  Posted 07 August 2007 - 10:43 PM

Friend Function in C++=

Can one friend function access private members of two different classes if it is a friend of those two classes ?
#include<iostream.h>
#include<math.h>

class Rec {
	double xco;
	double yco;
public:
	Rec(double xco1 =0, double yco1 =0)
	{
		xco =xco1;
		yco=yco1;
	}
	void display() { cout<<"\nxco = "<<xco<<"\tyco = "<<yco;}
	void operator= (Rec objRec)
	{
		xco = objRec.xco;
		yco = objRec.yco;
	}
	friend Rec ConvRec2Polar(Polar);
};

class Polar {
	double radius;
	double angle;
public:
	Polar(double radius1 =0, double angel1 =0)
	{
		radius = radius1;
		angle= angel1;
	}
	void display() { cout<<"\nradius = "<<radius<<"\tangle = "<<angle;}
	friend Rec ConvRec2Polar(Polar);
};

Rec ConvPolar2Rec(Polar objPolar)
{
	Rec objRec;
	objRec.xco = (sin(objPolar.angle) )* objPolar.radius;
	objRec.yco = (cos(objPolar.angle) )* objPolar.radius;
	return objRec;	
}

void main()
{
	
	Polar  objPolar(10,60);
	Rec objRec;
	objRec = ConvPolar2Rec(objPolar);	
}




errors=>
--------------------Configuration: Assignment4 - Win32 Debug--------------------
Compiling...
Polar2RecFriend.cpp
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(19) : error C2061: syntax error : identifier 'Polar'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(38) : error C2248: 'xco' : cannot access private member declared in class 'Rec'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(5) : see declaration of 'xco'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(38) : error C2248: 'angle' : cannot access private member declared in class 'Polar'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(24) : see declaration of 'angle'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(38) : error C2248: 'radius' : cannot access private member declared in class 'Polar'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(23) : see declaration of 'radius'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(39) : error C2248: 'yco' : cannot access private member declared in class 'Rec'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(6) : see declaration of 'yco'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(39) : error C2248: 'angle' : cannot access private member declared in class 'Polar'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(24) : see declaration of 'angle'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(39) : error C2248: 'radius' : cannot access private member declared in class 'Polar'
D:\KETAKI\Assignment4\Polar2RecFriend.cpp(23) : see declaration of 'radius'
Error executing cl.exe.

Assignment4.exe - 7 error(s), 0 warning(s)

Is This A Good Question/Topic? 0
  • +

Replies To: Friend Function in C++= Can one friend function access private member

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Friend Function in C++= Can one friend function access private member

Posted 08 August 2007 - 12:47 AM

Quote

Can one friend function access private members of two different classes if it is a friend of those two classes ?

Yes, they can.

But you have forgotten to mention the ConvPolar2Rec() function as a friend of the Rec Class.
Was This Post Helpful? 0
  • +
  • -

#3 mukeshid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-August 07

Re: Friend Function in C++= Can one friend function access private member

Posted 09 August 2007 - 11:21 AM

I don't think that's the problem in this case here.

I think you need to give a prototype of class Polar before using it in class Rec.

as you can see, when first friend function is declared, it is using Polar, and compiler at that time, don't know, what Polar is...
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Friend Function in C++= Can one friend function access private member

Posted 11 August 2007 - 11:40 AM

Yes, thanks for pointing that out. I missed that.
For this situation, adding a class Polar; before the Rec Class Declaration would do the trick.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1