Using a property from one class in another class' member?

This is my first post here, but I've been browsing the forums for the past year I've been in college. You guys are great help when I run into snags! :) Also, if this question is already in the forums I apologize, I looked, but wasn't exactly sure what I was looking for.

Anyway, on to the question. I'm in a college course right now about object oriented programming. I decided to code my own little game but ran into a bit of confusion. All of my assignments so far were working with single classes, but my game is going to have quite a few and I'm hoping I'm not digging myself too big of a whole. I was wondering if you had classes like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class A
{
private:
	string shape;
public:
	void nameShape(void);
};

class B
{
private:
	string color;
public:
	string nameColor(void);
};

string B::nameColor()
{
	color = "red";
	return color;
}


Is it possible to reference Class A's private or public properties inside the nameColor member? I know this doesn't work, but it's the only thing i can think of at the moment:

1
2
3
4
5
6
7
string B::nameColor()
{
	color = "red";
	return color;
        cout << "The color is " << color << " and the shape is ";
        << A::Shape << ".";
}


I know I could always return the Shape property to the main function and send it as an argument to nameColor, but that might get extreme if I have to send a lot of arguments at once later in the code. I'm trying to think ahead, but I'm pretty new to C++ and am not sure if there are easier ways to go about it so I don't have to send every property from one class to another class every time I switch the class I'm working with. I'm going to get some sleep and look at it again. Any insight would be greatly appreciated!
friend solves all inheritance problems. Yes, I do recommend inheritance.
http://cplusplus.com/doc/tutorial/inheritance/

Welcome to the forums.

-Albatross
Last edited on
Thank you so much! I had forgotten all about them! I read about them a few weeks ago the first time I was introduced to classes, but it was over my head at the time. Thanks again!
Topic archived. No new replies allowed.