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.
Just put them in protected
section:
class C {
protected:
A &a;
B &b;
...
};
NOTE: It has nothing to do with virtual
methods.
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.)
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)
^^^^^^^^^^^^
// ...
};
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);
};
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.
Just put them in protected
section:
class C {
protected:
A &a;
B &b;
...
};
NOTE: It has nothing to do with virtual
methods.
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.)
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)
^^^^^^^^^^^^
// ...
};
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);
};
0 commentaires:
Enregistrer un commentaire