Friend Class Issues

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Locked
VsPokemon
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Wed Oct 24, 2012 12:40 am

Friend Class Issues

Post by VsPokemon »

Thanks to everyone reading this, and please help! I'm trying to get it so that the value of an inherited variable in a friend class will change, but it only seems to stay the same. Below I have posted the code. I expected to get the output '1''2', but instead it's like I never added 1 to x and instead got the output '1''1'.

Code: Select all

#include <iostream>
using namespace std;

class Me {
private: int X;
public: 
	Me::Me();
	void add();
	friend class You;
};

class You {
public:
	Me m;
	void show();
};

Me::Me(){
X = 1;
}

void You::show(){
cout << "The number is " << m.X << endl;
}

void Me::add(){
X+=1;
}

int main(){
	Me m;
	You y;
	y.show();
	m.add();
	y.show();
system ("pause");

}
Last edited by MarauderIIC on Sun Oct 28, 2012 5:33 pm, edited 2 times in total.
Reason: I put in the code tags for you. Use them yourself in the future =)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Friend Class Issues

Post by MarauderIIC »

I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Locked