cannot access private member declared in class - c++

this is my first question in here :)
i have i little problem..
these are my classes:
class Gracz{
char znak_gracza;
public:
Gracz();
Gracz(char znak){
this->znak_gracza = znak;
};
friend void multiplayer();
};
class Osoba: public Gracz{
public:
Osoba();
Osoba(char znak){
this->znak_gracza = znak;
};
friend void multiplayer();
};
i also have a function multiplayer, where i try tu use constructor with argument:
void multiplayer(){
Osoba gracz1('O');
Osoba gracz2('X');
...
}
but it doesn't work.
errors are same for gracz1 and gracz2
error C2248: 'Gracz::znak_gracza' : cannot access private member declared in class 'Gracz'
see declaration of 'Gracz::znak_gracza'
see declaration of 'Gracz'

Derived classes cannot access private members of a parent class. You can declare them as protected (which is like private but lets derived classes access it), but in your case, since Gracz provides a way to initialize the variable, you should just let Osoba pass the argument to Gracz constructor.
Osoba(char znak)
: Gracz(znak) // initializes parent class
{}

private member access is only available to members of the class, and friends. what you're looking for it to declare char znak_gracza as protected, so classes that inherit Gracz have access to that member as well.
your class Gracz should look more like this:
class Gracz{
protected:
char znak_gracza;
public:
Gracz();
Gracz(char znak){
this->znak_gracza = znak;
};
friend void multiplayer();
};

The constructor needs to pass the parameter to the base class constructor:
class Osoba: public Gracz{
public:
//...
Osoba(char znak) :
Gracz(znak) {
}
};

The multiplayer function is a friend of the Gracz class, but the Osoba class isn't.
Remember that child classes can't automatically access parent classes private variables. If you want Osoba to access the znak_gracza variable you have to make it protected.

Related

How do I access private member variables with public member functions (derived class)?

I need to get access of the base class private member variables using public member functions from the derived class. That means, I can call it anytime in a public member function from the derived class and change it anytime.
For example:
# include <iostream>
using namespace std;
class A
{
private:
int a = 19;
public:
int getA() // Function to get A
{
return a;
}
};
class B : public A
{
public:
void getAatB() // How do I get a variable at parent class (A)?
{
cout<<getA();
}
};
int main()
{
B a;
a.getAatB();
return 0;
}
I need to get access to variable a. How do I do this without changing private to protected or public at class A?
the easy way of doing that is adding friend class B; to class A , so class B can access class A's attributes and you aren't going to need change private to protected.
If you want only the descendants of A having a access to getA() member function, there is a special keyword protected in c++. The purpose of protected, as stated here is.
Class members declared as protected can be used only by the following:
Member functions of the class that originally declared these members.
Friends of the class that originally declared these members.
Classes derived with public or protected access from the class that
originally declared these members.
Direct privately derived classes that also have private access to
protected members.

Friend function and Inheritance in cpp

Am new to cpp. Generally, friend function could access private and protected members of the class but when I tried to use friend function in derived class am observing weired behavior..
class first
{
int a;
};
class second : public first
{
public:
friend void hai ( second );
};
void hai ( second s )
{
printf("%d",s.a); // It says compilation error
}
void main()
{
second s;
hai(s);
}
If I make a to public, it works fine.
Could somebody clear me, why shouldn't I access a if its in base private scope.
Regards
Prasath S.
Make field a protected, not private.
Private fields can be accessed only from class declaring them.
Your error is here:
class first
{
int a;
};
The default for class members is private so you won't have access to it from derived classes.
Private members are only accessible from the same class. The line
class second : public first
doesn't change the private member to public, so you have the choice between making a protected/public or you include a public/protected get/set function to first, which will manage the access to a.
why shouldn't I access a if its in base private scope.
friend function can access private members of the class that it friends to.
However in this case, member a in base class is private by default and therefore not inherited to child class.
Public inheritance requires protected or members of base class to be inherited by child classes. So for your example to work, you can use protected member a
class first
{
protected:
int a;
};
first::a is private in class first
class second is derived publicly from class first, so:
all the public members of class first are public in class second and can be accessed via objects of class second.
all the protected members of class first are protected in class second and cannot be accessed via objects of class second.
all the private members of class first are inaccessible in class second and cannot be accessed via objects of class second.
Therefore, you cannot access first::a via object of class second.
For more information: Can a friend class object access base class private members on a derived class object?
Why can a derived class not access a protected member of its base class through a pointer to base?

How to access the members of a derived class from another derived class?

I have a parent class and I have 2 publicly derived classes from that parent class. eg.
class Parent
| |
| |
| |
class derived1 class derived2.
Question:
I would like to access the private members of one derived class from another derived class. How do I do this?
The way I have it now is as follows:
Passing the cDerived1 object as a parameter to the ctor of cDerived2. If I do it this way, then I have to declare cDerived2 as a friend of cDerived1 and also include cDerived1.h inside cDerived2.h
#include cParent.h
#include cDerived1.h
#include cDerived2.h
void main (){
// Instantiate a cDerived1 object
Derived1 dev1();
// Instantiate a cDerived2 object. The cDerived2 object will need access to the
// private members of cDerived1. So pass dev1 by reference to dev2 ctor.
Derived2 dev2(dev1);
}
Is this the right way to do it or am I doing something very blatantly wrong ??
Thanks.
In response to Paul's comment:
I already have the shared code in the parent class as shown below.
cParent.h
class cparent{
public:
// ctor
// dtor
protected:
int* pArr;
};
cDerived1.h
// derived1's header
#include "cParent.h"
class cDerived1 : public cParent{
public:
//
};
cDerived2.h
// derived2's header
#include "cParent.h"
class cDerived2 : public cParent{
public:
// I want access to derived1's pArr member over here....How do I do this ?
This is what the keyword friend is used for. It can be used for either functions or classes...in this case you would apply it to the class. The only way you can access protected members is through inheritance...i.e. a sub-class, or a friend class. I'm pretty sure the only way to access private members outside of the class is to use a friend function or class. Here is a relevant link from cplusplus.com
Here is their example (their second) of declaring a friend class.
class CSquare {
private:
int side;
public:
void set_side (int a)
{side=a;}
friend class CRectangle; // here is the friend declaration
};
So in your example you would use:
friend class Derived1; // place in Derived2
friend class Derived2; // place in Derived1
Derived 1 would have access to Derived 2 private members, and Derived 2 would have access to Derived 1 private members.
I'm assuming you don't want to simply use a getter function.
Once it is declared a friend you can use:
Obj.theVar
instead of using a getter function like this:
Obj.getVar()
Since pArr is in the base class, you can achieve this without needing the definition of cDerived1. Pass the object of cDerived1 as pointer of the cparent class and everything would be alright. Here is some sample code...
// cDerived2.h
// derived2's header
#include "cParent.h"
class cDerived2 : public cParent{
public:
// I want access to derived1's pArr member over here....How do I do this ?
void AccessPArr(cparent* pParent)
{
// access over here
}
int main()
{
cDerived1 der1;
cDerived2 der2;
der2.AccessPArr(&der1);
}

C++ classes (public, private, and protected)

How can classes in C++ be declared public, private, or protected?
In C++ there is no notion of an entire class having an access specifier the way that there is in Java or C#. If a piece of code has visibility of a class, it can reference the name of that class and manipulate it. That said, there are a few restrictions on this. Just because you can reference a class doesn't mean you can instantiate it, for example, since the constructor might be marked private. Similarly, if the class is a nested class declared in another class's private or protected section, then the class won't be accessible outside from that class and its friends.
By nesting one class inside another:
class A
{
public:
class B {};
protected:
class C {};
private:
class D {};
};
You can implement "private classes" by simply not publishing their interface to clients.
I know of no way to create "protected classes".
It depends if you mean members or inheritance. You can't have a 'private class', as such.
class Foo
{
public:
Foo() {} //public ctr
protected:
void Baz() //protected function
private:
void Bar() {} //private function
}
Or inheritance:
class Foo : public Bar
class Foo : protected Bar
class Foo : private Bar

How to access a data class's private member variable from another derived class whose parent class is a friend class of the data class?

I have three classes:
A data holder class CDataHolder, which uses a Pimpl pattern
class CDataHolder
{
public:
// ...
private:
friend class CBase;
struct PImpl;
PImpl* iPimpl;
};
A base class CBase, which need to access the iPImpl member in CDataHolder, so it is a friend class of CDataHolder
class CBase:
{
protected:
CDataHolder::Pimpl* getDataHolderPimpl();
};
A derived class CDerived from CBase, which need to access the same iPimpl member also. Here occurs a problem.
The derived class cannot use the iPimpl member although its parent class is a friend class. like this:
class CDerived : public CBase
{
public:
void doSth() {
CDataHolder::Pimpl *pImpl = getDataHolderPimpl(); // this line raises an error:
// "illegal access from CDataHolder to protected/private member CDataHolder::PImpl"
}
};
There are plenty of derived classes, so it's not a good way for each derived class to put a "friend class CDerivedXXX" line in CDataHolder class.
How to overcome this issue? Is there a better way to do this? Thanks in advance.
Since you have declared struct PImpl in the private part of CDataHolder class, only friends of CDataHolder can access the same. Why don't you put a forward declaration struct PImpl in the public section or even better before the CDataHolder class?
Friend is (rightfully) very limited and can't be inherited. I hate to beg the question, but maybe either A) you need public access to PImpl or some aspect of it, or B) you need the DataHolder class to do something with PImpl for you.