Accessing a base class member in derived class - c++

I have a simple class as below
class A {
protected:
int x;
};
class B : public A {
public:
int y;
void sety(int d) {
y = d;
}
int gety() {
return y;
}
};
int main() {
B obj;
obj.sety(10);
cout << obj.gety();
getch();
}
How can I set the value of the protected instance variable A::x from an instance of the derived class B without creating an instance of class A.
EDIT: Can we access the value of A::x using the object of B? Like obj.x?

B is an A, so creating an instance of B is creating an instance of A. That being said, I'm not sure what your actual question is, so here's some code that will hopefully clarify things:
class A
{
protected:
int x;
};
class B : public A
{
public:
int y;
int gety() const { return y; }
void sety(int d) { y = d; }
int getx() const { return x; }
void setx(int d) { x = d; }
};
int main()
{
B obj;
// compiles cleanly because B::sety/gety are public
obj.sety(10);
std::cout << obj.gety() << '\n';
// compiles cleanly because B::setx/getx are public, even though
// they touch A::x which is protected
obj.setx(42);
std::cout << obj.getx() << '\n';
// compiles cleanly because B::y is public
obj.y = 20;
std::cout << obj.y << '\n';
// compilation errors because A::x is protected
obj.x = 84;
std::cout << obj.x << '\n';
}
obj can access A::x just as an instance of A could, because obj is implicitly an instance of A.

A::x is protected, so not accessible from outside, neither as A().x or B().x. It is however accessible in methods of A and those directly inheriting it (because protected, not private), e.g. B. So, regardless of semantics B::sety() may access it (as plain x or as A::x in case of shadowing by a B::x or for pure verbosity).

Note that B does not have FULL access to A::x. It can only access that member through an instance of a B, not anything of type A or deriving from A.
There is a workaround you can put in:
class A
{
protected:
int x;
static int& getX( A& a )
{
return a.x;
}
static int getX( A const& a )
{
return a.x;
}
};
and now using getX, a class derived from A (like B) can get to the x member of ANY A-class.
You also know that friendship is not transitive or inherited. The same "workaround" can be made for these situations by providing access functions.
And in your case you can actually provide "public" access to the x through your B by having public functions that get to it. Of course in real programming it's protected for a reason and you don't want to give everything full access, but you can.

You can just refer to it simply as x in class B
For example:
class B : public A
{
public:
...
void setx(int d)
{
x=d;
}
};

Related

Same hierarchy, different behaviour when accessing protected member of base class

I came across this code in a past exam:
#include <iostream>
class cls1
{
protected:
int x;
public:
cls1()
{
x = 13;
}
};
class cls2 : public cls1
{
int y;
public:
cls2()
{
y = 15;
}
int f(cls2 ob)
{
return (ob.x + ob.y);
}
};
int main()
{
cls2 ob;
std::cout << ob.f(ob);
return 0;
}
This works just fine and outputs 28. The problem is, it seems to contradict with this code (found in another exam):
#include <iostream>
class B
{
protected:
int x;
public:
B(int i = 28)
{
x = i;
}
virtual B f(B ob)
{
return x + ob.x + 1;
}
void afisare()
{
std::cout << x;
}
};
class D : public B
{
public:
D(int i = -32)
: B(i)
{
}
B f(B ob)
{
return x + ob.x - 1;/// int B::x is protected within this context
}
};
int main()
{
B *p1 = new D, *p2 = new B, *p3 = new B(p1->f(*p2));
p3->afisare();
return 0;
}
It's the same type of hierarchy, but one has access to ob.x and the other one doesn't. Can someone explain to me why that is?
The difference is, in the 1st case the protected member is accessed through the derived class. In the 2nd case the protected member is accessed through the base class, which is not allowed.
For protected members,
(emphasis mine)
A protected member of a class is only accessible
2) to the members and friends (until C++17) of any derived class of that class, but only when the class of the object through which the protected member is accessed is that derived class or a derived class of that derived class:
struct Base {
protected:
int i;
private:
void g(Base& b, struct Derived& d);
};
struct Derived : Base {
void f(Base& b, Derived& d) { // member function of a derived class
++d.i; // OK: the type of d is Derived
++i; // OK: the type of the implied '*this' is Derived
// ++b.i; // error: can't access a protected member through
// Base (otherwise it would be possible to change
// other derived classes, like a hypothetical
// Derived2, base implementation)
}
};

shared pointer isn't working when casting?

casting shared pointer from B class to A class is not working the console output isn't 12 it's (it outputs A's x but i want B's x)(probably an other memory address). what's wrong with my code
#include <iostream>
#include <memory>
class A
{
public:
int x;
};
class B : public A
{
public:
B(){}
B(const B*){}
int x = 12;
};
std::shared_ptr<A> create()
{
return std::make_shared<B>(new B);
}
int main(){
std::shared_ptr<A> p;
p = create();
std::cout << p->x << std::endl;
std::cin.get();
return 0;
}
A::x and B::x are different objects. Variable access is never polymorphic in C++, so when you access p->x, you're accessing A::x. A::x was never initialized though, so the behavior of your program is undefined.
You need to either have your derived class's constructor initialize the base class's object or delegate that responsibility to the base class's constructor:
class A
{
public:
A(int x) : x{x} {}
int x;
};
class B : public A
{
public:
B() : A{12} {}
};
Live Demo
Alternatively you could wrap x in a virtual accessor method:
class A
{
public:
virtual ~A() = default;
virtual int x() const = 0;
};
class B
{
public:
int x() const override
{
return x_;
}
private:
int x_ = 12;
};
Live Demo

Can't access protected member

I have some difficulties understanding the following code. I have 3 classes: A, B and C. A has a private x of type int. B inherits A and everything from it. C inherits from B but protected. This means that both X and Y become inaccessible in class C. Also, get_x() and get_y() become protected in class C. So C should be able to acces the get_x() and get_y() functions but not x and y?
#include<iostream>
using namespace std;
class A
{
int x;
public: A(int i):x(i){}
int get_x(){ return x; }
};
class B: public A
{
int y;
public: B(int i,int j):y(i),A(j){}
int get_y(){ return y; } };
class C: protected B
{
int z;
public: C(int i,int j,int k):z(i),B(j,k){}
int get_z(){ return z; }
};
int main()
{
C c(1,2,3);
cout<<c.get_x();
cout<<c.get_y();
cout<<c.get_z();
return 0;
}
It seems like you understood 'protected inheritance' right but misunderstood 'protected members and methods'.
protected methods are inaccessible from outside of class.
Meaning
C c(1,2,3);
cout << c.get_x(); // THIS DOES NOT WORK.
where you can access get_x() is within class C.
class C: protected B
{
int z;
public: C(int i,int j,int k):z(i),B(j,k){}
int get_z(){ return z; }
int get_x_from_c() { return this->get_x(); } // THIS IS ALLOWED.
};
Here Even Object of type C class can not use the functions of class A and B. If you want access of member functions then make public access of B to C.
do this ...
class c : public B{.. then it will be able to access functions but remember not the member variables.

Access members of nested class

class A
{
class B
{
int x;
}
public:
void printX() { std::cout << ????; }
}
How can I access the x variable from the A class function? I can't make it static either...
I tried everything but it either tells me I need an object in order to access it or the compiler doesn't find the function.
it either tells me I need an object [...]
Think about that. Because that's exactly what the problem is here.
If you instantiate an A, you don't also get a B. A nested class isn't a member variable of the enclosing class. It's really just another way to change the namespace of a class.
So, you need an instance of B. Perhaps a member of A?
class A
{
class B
{
public:
int x;
} mB;
public:
void printX() { std::cout << mB.x; }
};
You don't ever declare an instance of the class B inside A. You need to do something like this:
class A
{
class B
{
public:
int x;
};
B b;
public:
void printX() { std::cout << b.x; }
};
You don't. You do need an object in order to use the x variable. You could, however make it static. The problem with your example is x is not public. Placing B inside A does not make B part of A, it only changes B's scope.
From this example it kinda looks like you're after inheritance instead. This would give you the effect you're after ( access to all B's methods and variables without making an object. )
Class B
{
protected:
int x;
}
Class A : B
{
void printX() { std::cout << x; }
}

c++ redefining types in subclasses and slicing

#include <string>
#include <iostream>
class a { public: int x;};
class b : public a {public: int x; } ;
int main()
{
b bee;
bee.x = 3;
a ay = bee;
std::cout << std::endl << ay.x << std::endl;
}
The code above compiles fine in clang 3.0 and g++ 4.5. However the output is junk (--i.e., not three). Since the compiler doesn't seem to mind, how do I get the code to behave ?
Secondly, If there is some way to make the above slice / conversion to work correctly, how bad would it be if I then did the following, provided a good reason to do it exists :
class c : public a { public: uint64_t x; };
Why I am interested in these semantics.
The reason I want to do this is this. I have a two class heirachies, where one heirarchy (the parent) aggregages objects, on the same heirarchy level, from the other(the child). I use a custom container for the aggregation. I want to typedef the container in the parent class (the typedefs have the same name), and declare the container with the same name at each level of the parent.
The class heirarchies are designed to contain less information at lower levels ( the base classes hold the least), therefore slicing makes perfect sense here.
Edit:
There you go, this should clear things up.
class A { int x; };
class B : public A {int y;};
class Ap {std::vector<A> entries;};
class Bp : Ap{std::vector<B> entries;};
The child B has more members than the child class A. However, I wan't to present a uniform interface for code that is only interested in the members of class A.
There is no way to do that if you directly set b::x. a::x and b::x are two different members, and the latter hides the former.
You can still access a::x on an object of type b with static_cast<a&>(bee).x = 3, but the fundamental problem is that the values of a::x and b::x on an object of type b are not synchronized.
If you abstract access to both x members with a "property getter/setter", then you can arrange for the setter on the derived class to also update the member of the base class. Or (maybe this is more appropriate?) you can make the member of the base class protected and use it from the derived class directly, slicing as you need just before returning from the getter.
huh ! its a bit complicated no ?
why don't you use :
class a
{
virtual void set( int value ) { x = value; }
protected :
int x;
};
class b : public a
{
virtual void setA( int value ) { a::x = value; }
or
virtual void setA( int value ) { b::x = value; }
or
virtual void setA( int value ) { a::x = value; b::x = value; }
protected:
int x;
} ;
There are two ways of constructing a software design; one way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.
C.A.R.Hoare
According to Jon's answer, Since a::x and b::x are separate variables, furthermore since b::x masks a::x, if you wanted to get the correct semantics you need to provide a copy conversion constructor. The following code does the trick.
#include <string>
#include <iostream>
class b;
class a {
public:
a();
a(const b & bee);
int x;
};
class b : public a {public: int x; } ;
a::a() {}
a::a(const b & bee)
{
x = bee.x;
}
int main()
{
b bee;
bee.x = 3;
a ay = bee;
std::cout << std::endl << ay.x << std::endl;
}
Maybe try something like this:
class A { int x; };
class B : public A {int y;};
class Ap {
public:
void Append(A *pa)
{
entries.push_back(pa);
}
A *GetA(size_t nIndex)
{
return entries.at(nIndex);
}
private:
std::vector<*A> entries;
};
class Bp : Ap
{
public:
B *GetB(size_t nIndex)
{
return dynamic_cast<B*>(GetA(nIndex));
}
};