protected members are not accessible in derived classes - c++

Why is that protected members in the base class where not accessible in the derived class?
class ClassA
{
public:
int publicmemberA;
protected:
int protectedmemberA;
private:
int privatememberA;
ClassA();
};
class ClassB : public ClassA
{
};
int main ()
{
ClassB b;
b.protectedmemberA; // this says it is not accesible, violation?
//.....
}

You can access protectedmemberA inside b. You're attempting to access it from the outside. It has nothing to do with inheritance.
This happens for the same reason as the following:
class B
{
protected:
int x;
};
//...
B b;
b.x = 0; //also illegal

Because the protected members are only visible inside the scope of class B. So you have access to it here for example:
class ClassB : public ClassA
{
void foo() { std::cout << protectedMember;}
};
but an expression such as
someInstance.someMember;
requires someMember to be public.
Some related SO questions here and here.

You can only access protectedmemberA from within the scope of B (or A) - you're trying to access it from within main()

Related

Access Functions of "CLASS A" by "CLASS C" without Inheritance in C++

Here is what UML looks like CLASS UML
ClassA has an pointer to ClassB, and ClassB has a pointer to ClassC. I was wondering if I can access functions of ClassA in ClassC without inheriting ClassA.
Q: I was wondering if I can access functions of ClassA in ClassC without inheriting ClassA.
A: Yes, it is possible to access functions of ClassA in ClassC. Either by calling static member functions of classA or by providing specific instance of classA to ClassC. It may look like that classA can be accesed by following the pointers in reverse direction (ClassC to ClassB to ClassA) but that is not possible. Pointers point to values only in one direction.
This is interesting question about differences between a class and instances of class (objects). The following example shows why there is no classA::callAnyMethod():
#include <iostream>
class B;
class C;
class A
{
public:
A(B *next, int value) : next_(next), value_(value) { }
void af1() const { std::cout << value_ << "\n"; }
static void af2() { std::cout << staticValue << "\n"; }
protected:
B *next_;
int value_;
static int staticValue;
};
int A::staticValue = 3;
class B
{
public:
B(C *next) : next_(next) { }
protected:
C *next_;
};
class C
{
public:
void cf1(const A &a) { a.af1(); }
void cf2() { A::af2(); }
};
int main()
{
C c;
B b(&c);
A a1(&b, 1);
A a2(&b, 2);
// a1 is first instance of class A.
// One 'A::value_' is defined in a1 and is equal to 1.
c.cf1(a1);
// a2 is second instance of class A.
// Second 'A::value_' is defined in a2 and is equal to 2.
c.cf1(a2);
// Without specific instance of class A, we can use only static member
// 'A::staticValue' which is defined at file scope and is equal to 3.
// 'A::staticValue' is not part of objects (instances) of class A.
c.cf2();
return 0;
}

Access to specific private members from specific class

I have a class
class A
{
.....
private:
int mem1;
int mem2;
}
I have another class B which need to access only to mem1.
class B
{
....
}
How can I access private member mem1 from only from class B? I don't want to use friend. This means access to all private members.
With some rearrangement to class A (which might not necessarily be acceptable), you can achieve this:
class Base
{
friend class B;
int mem1;
};
class A : public Base
{
int mem2;
};
This exploits the fact that friendship is not transitive through inheritance.
Then, for class B,
class B
{
void foo(A& a)
{
int x = a.mem1; // allowed
int y = a.mem2; // not allowed
}
};
You can write a base class with member mem1 and friend B
class Base {
protected:
int mem1;
friend class B;
};
class A: private Base {
// ...
};

How to access private members of an abstract class from its friend class?

class A
{
private:
int a,b,c;
public:
virtual int get()=0;
friend class B;
};
class B{
//here I want to access private variables of class A that is a, b and c
};
class C:public class A
{
int get(){
//some code
}
};
How to access private members of class A in class B. I cannot create an object of class A since it is abstract. I somehow have to use an object of class C to do that but how?
class A {
friend class B;
private:
int x;
public:
A() : x(42) {}
};
class C : public A {
};
class B {
public:
int reveal_secrets(C &instance){
// access private member
return instance.x;
}
int reveal_secrets(){
// access private member of instance created inside B
C instance;
return instance.x;
}
};
void print_secrets(){
C instance;
B accessor;
std::cout << accessor.reveal_secrets(instance) << ", " << accessor.reveal_secrets() << std::endl;
}
class B will have to have an instance object to work with in the first place. That instance object is what B will look at in order to access a, b, etc .

How to assign and retrive the value in member variable?

I am put/get value in/from subclass B from object of base class A. But I am not able to assign or get the value. My code is:
class A
{
};
class B: A
{
string SID;
};
class C: A
{
string Name;
};
class D : A
{
string Name;
};
class E
{
A a;
UINT32 AccessLevel;
};
.......
main()
{
E e;
}
Using object of e am trying to get the value of subclass B.
I need to get the SID from the class B?
Thanks,
The C++11 standard 11/3 says:
Members of a class defined with the keyword class are private by default.
at 11.2/2
In the absence of an access-specifier for a base class [...] private is assumed when the class is defined with the class-key class.
and at 11.2/1:
If a class is declared to be a base
class for another class using the private access specifier, the public and protected members of the base
class are accessible as private members of the derived class.
So what does that mean? First of all:
class A {};
class B : A {};
Here A, by virtue of 11.2/2 is inherited privately. This may be okay if you want to inherit variables and you want to implement getter/setters for a variable only in a derived class, but that's usually considered bad style.
In your case however, as stated by 11/3, your members are not inherited at all because they are private members:
class A
{
public:
int a; // inherited
protected:
int b; // inherited
private:
int c; // NOT inherited
};
and especially
class A { int a; };
is equivalent to
class A { private: int a; };
So you could make your members accessable from within your derived classes by making them public or protected (see 11.2/1):
class A { public: int a; };
class B : A {}; // privately inherits a
and if you wanted to make it acessable from outside of your derived classes you will have to inherit as public as well:
class A { public: int a; };
class B : public A {}; // publicly inherits a
but that's not what you usually would do. It's considered better style to make variables private and expose only getters and setters for those:
class A
{
public:
int get_a() const { return a_; }
void set_a(int val) { a_ = val; }
private:
int a_;
};
class B : public A {}; // now publicly inherits the getters and setters
// but not a_ itself

Access protected data members of the base class from the derived class

I have a base class and the derived class. I need to access the protected member of the base class in the derived class. However, Eclipse does not allow me to access the data member as if it were a member of a derived class without caring that it was inherited. How do I do that?
class BaseClass {
protected:
static int a;
int b;
}
class DerivedClass: public BaseClass {
void SomeMethod {
a=10; // cannot resolve symbol
b=10; // cannot resolve symbol
BaseClass::a=10; //does not complain
BaseClass::b=10; //does not complain
}
}
I couldn't completely understand your question, but fixing the syntax errors, the following should work:
class BaseClass {
protected:
static int a;
int b;
}; // <-- Missing semicolon
int BaseClass::a = 0; // Define static member
class DerivedClass: public BaseClass {
void SomeMethod() { // <-- Missing ()
a=10;
b=10;
}
};// <-- Missing semicolon