0

I have 4 classes.

class A, class B, class C, class D

Class C includes class A and class B and reference them:

The Header File:

class C
{
private:
    A &a;
    B &b;
    int x;
    int y;
    int energy;
public:

    C(A &a, B &b);

          void print(void);

    virtual void printAt(void);

CPP File includes:

void C::printAt(void)
{
    // move cursor to the current x, y coordinates
    b.gotoXY(x,y);
}

In class D, I make class C a friend class by (class D : public class C...)

Then I have a void printAt(void).

This all works, but how do I access the b class attributes (b.gotoXY..) from class D?

Hopefully this makes Sence.

Mykola
  • 3,343
  • 6
  • 23
  • 39
  • 2
    You don't make it _"friend"_, you inherit from it. – Alexander Shukaev Apr 12 '13 at 15:20
  • 2
    Go read up on [inheritance](http://stackoverflow.com/a/860353/57318) and [public/protected/private access](http://stackoverflow.com/a/614844/57318). We can answer this for you, but I fear that you won't understand why. – Xymostech Apr 12 '13 at 15:25

4 Answers4

0

Just put them in protected section:

class C {
protected:
  A &a;
  B &b;

  ...
};

NOTE: It has nothing to do with virtual methods.

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
0

The reason you cannot access them from D is because they are private, which means they are only accessible from within D itself. In order to be able to access them only from D or its subclasses, you need to use the protected access modifier instead:

class C
{
private:
    int x;
    int y;
    int energy;

protected:
    A &a;
    B &b;

public:
    C(A &a, B &b);

    void print(void);

    virtual void printAt(void);
    /// ...
};

Now, a bit of terminology:

When you type class C : public D you are not making it a friend, you are inheriting from it. This means C will be a base class of D. A friend is another, related concept.

A friend of some class is another class which has access to its private properties. So, if you instead had made D a friend of C, you would have had access to a and b without having to make them protected. This would be accomplished as such:

class C
{
    // Some code...
    friend D;
    //Lots of code ...
}

Please note that, for this to work, you need to declare D before C.

Now, which of these options should you use?

Ask yourself this question: is a D logically a more specific type of C? If so, it should use inheritance. If not, it may be better to make D have a member of type C and use the friend keyword. In either case, use friend sparingly, and only if there is necessarily a very tight relationship between the two classes (perhaps if D is a factory for type C and C has a private constructor.)

Agentlien
  • 4,996
  • 1
  • 16
  • 27
0

You should make intended members protected or make their classes friend to your class.

In addition, I feel you will have a problem when you instantiating an object from C because of uninitialized references.

class C
{
private:
    A &a;
    B &b;

    // ...

public:
    C(A &a, B &b) : a(a), b(b)
                  ^^^^^^^^^^^^
    // ...
};  
masoud
  • 55,379
  • 16
  • 141
  • 208
0

when you want other class in inherit access to your attributes .dont private them
so you can choose protected or public. for more detail you can go http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
for solve problem try below code

class C
{
protected://or public
    A &a;
    B &b;
    int x;
    int y;
    int energy;
public:
    C(A &a, B &b);
    void print(void);
    virtual void printAt(void); 

and in class D

class D:public C
{
public:
    void printAt(void);
};
Mahdi-bagvand
  • 1,396
  • 19
  • 40