Passing data between classes

Suppose I have two separate classes,
Class A
1
2
3
4
5
class A
{
public:
int length;
};

Class B
1
2
3
4
5
6
class B
{
public:
int width;
int area() {return length * width;}
};

Is there a method of passing the data between the two classes, so that B uses "length" from A?
Both classes are separate, not inherently linked or anything. Basically, I'm trying to use a member function in a, and calling it from b.
Cheers!
Last edited on
Well,
1
2
3
4
5
6
class B
{
public:
int width;
int area() {return length * width;}
};


Should be :
1
2
3
4
5
6
class B : public A
{
public:
int width;
int area() {return length * width;}
};

Here you misunderstand the function of a class declaration. A class is just a template for making objects of them. So if you want to use the data members of a class, then you must instantiate them. This means, you must make an Object of the class type, and then you could use the members of this object. But at your intention is a way by making a member 'static'. That means it belongs to the class and is shared by all objects, so that needn't instantiate a object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class A
{
public:
    static int length;  // shared by all A objects
};

class B
{
public:
    int width;   // individually by each B object
    int area( return A::length * width );
};

#include <iostream>

int main()
{
    A::length = 4;  //  You have access to 'length' without creating an object of 'A'.

    B b_object;     // You must make an object of B for using its members
    b_object.width = 7;

    std::cout << b_object.area();  //  Here you need also an object of B for calling 'area()'.
 }  
Last edited on
@nuderobmonkey - I don't believe the OP said anything about sharing length across all instances of A, hence there should be no need to make length static. IMO, making length static would be rather unusual.

@SakurasouBusters - the OP said "not inherently linked", so that would rule out using inheritance, although I agree with you that may be what the OP is looking for.

@OP
I'm trying to use a member function in a, and calling it from b.

You didn't show any member functions in A. Did you mean member variable?

If you don't want to use inheritance (it's not clear why you don't), then you can simply create an instance of A inside B.
1
2
3
4
5
6
7
8
9
class B
{
public:
  A  a;  // instance of A inside B
  int width;

  int area() 
  { return a.length * width; }
};






Last edited on
AbstractionAnon, thanks; that seems to be the solution. The examples in my OP didn't reflect the complexity of my actual code; I was simply trying to illustrate the problem.

As it turns out, just placing a member object inside would do the trick. I was aware of that method, but was looking at the problem the wrong way.

Here's the thing; I have a class that, when instantiated, creates an object which is responsible for all random-number generation in the program:
1
2
3
4
5
6
7
8
9
10
class Random
{
public:
	Random()
	{
		setRand();
	}
	void setRand();
	int getRand(int max);
};

Then, there is another class, which is a "player" object, which contains all of the player-attributes, as well as functions related to the player. This keeps everything in a nice, portable bundle that I can port elsewhere, if needed.
What I was hoping for, was a way to get the player-class to 'talk' to the 'random' class, to simplify the end-code.
I'll go with your suggestion, as there doesn't seem to be an alternative.
Cheers!
Topic archived. No new replies allowed.