C++ inheritance private members - c++

I have a question about inheritance:
For example I have a base class A with private members: x, y
I have getter functions getx and gety,
I want to use the getter functions from A to class B that inherits from A,
But my problem is that x and y are private members of A
I cant access the private of A from B.
So I need to create two x,y for class B but that way I wont be able to use the get functions of class A.
Any help? I dont know how I can access the private members of A without creating new ones. Maybe with the constructor? But I am not sure how.

Private members are always private. They can't be accessed from outside classes at any cost. You can try protected instead, consider the following:
#include <iostream>
// Base class
class A {
protected: // declaration for protected vars which are clearly accessible to B
int x;
int y;
public:
void setX(int w) { x = w; }
void setY(int h) { y = h; }
};
// Derived class
class B: public A { // derives A
public:
int multiply() {
return (x * y); // accesses x and y from class A
}
};
The x and y are accessed from Class A to derived Class B.
That's it.

C++ provides the Protected mechanism which you might want to use. Take a look here.
You could make members x and y of Class A protected so that Class B which inherits from A can access them.
class A{
protected:
int x;
int y;
};
class B : public A{
public:
void somefn(int someval){
x = someval; //A::x is being set here.
}
};

Related

C++ Exclude classes inherited through base class in derived class

In C++, is it possible to exclude a class inherited in the base class, from a subclass?
For example, if you have three classes like so:
class A
{
public:
int x;
};
class B : public A
{
public:
int y;
};
class C : public B
{
public:
int z;
};
Is it possible for class B to contain the variables x and y, while class C only contains y and z?
I think the closest thing to what you want to achieve is private inheritance.
struct A
{
int x;
};
struct B : private A
{
int y;
};
struct C : B
{
int z;
};
int main()
{
C c;
c.x = 5; // error. you're not allowed to access members from A
c.y = 3; // ok
c.z = 4; // ok
}
So the data of A is still going to be there but you just can't access it from outside of B.
If you want the data to not exist in C at all then the answer is No. Inheritance is literally having the content of your parents in the beginning of your struct.
struct A
{
int x;
};
struct B : A
{
int y;
};
in memory an instance of B will look like this:
struct B
{
int x;
int y;
}
Yes, it's possible to meet your variable inheritance constraints. Use friend classes. With the friendship semantics exposed by the keyword friend inside a class declaration, you can make a derived class to have access to the private members of its parent class. The link contains a full tutorial of the friendship semantics.
Your code would look like:
class A
{
private:
int x;
friend class B;
};
class B : public A
{
public:
int y;
};
class C : public B
{
public:
int z;
};
With this, class B has access to the private x of class A, but only from inside the definition of class B. Your variable inheritance constraint is met, class B contains (and can use) x and y, while class C contains y and z.
Another way could be by private inheritance, like this:
class A
{
private:
int x;
};
class B : private A
{
public:
int y;
};
class C : public B
{
public:
int z;
};
But this is much more stricter than the friend approach. Choose which one suits your need.

Inheritance from the base of a base class

I'm learning c++ here.
I'm learning more about inheriting and classes but I'm having the following issue:
class A
{
public:
std::vector<int> vect1;
};
class B : A
{
public:
int x, y;
};
class C : B
{
c()
{
x=10;
y=30;
vect1.pushback(44);
}
};
why cant i access vect1 from the class C? how can i achieve this?
i also tried:
class C : B, A {...}
but then it said vect1 is ambiguous (or something like that). im using visual studios.
thank you.
why cant i access vect1 from the class C?
Because vect1 has private access. Sub classes can not access private members of a base class. They can only access protected or public members.
Furthermore, A is a private base of B, so sub classes of B have no access to it. Only protected or public bases are accessible to grand children.
The default access specifier of classes defined with the class keyword is private (The default is public for classes defined with the keyword struct). To declare a base with another access specifier, you can put the access specifier keyword right before each base in the declaration:
class B : protected A
{
// ...
};
To declare members with another access specifier:
class A
{
protected:
std::vector<int> vect1;
};
To get access from derived to base class you should do both:
public inheritance
protected declaration
like the following:
#include <vector>
class A
{
protected:
std::vector<int> vect1;
};
class B : public A
{
protected:
int x, y;
};
class C : public B
{
public:
C(){
x=10;
y=30;
vect1.push_back(44);
}
};

Assign static variable to non-static variable

Consider the Base class where A class and BaseB class are derived. From BaseB is derived C class. All clases inherit non-static variable “y” but in the case of BaseB and C class “y” have the same value.
I resolved this situation with the following code:
class Base {
protected:
int y;
virtual void registerValue()
{
y = 5;
}
};
class A : public Base {
};
class BaseB : public Base {
protected:
static int x;
virtual void registerValue()
{
// Process x ...
y = x;
}
};
class C : public BaseB {
};
int BaseB::x = 3;
int main() {}
It works but is it right to assign static variable to non-static variable for this case?
It's fine to do from a language legality perspective, but it's a little odd.
Presumably you can't make Base::y static since that would interfere with the behaviour of class A?
You just need to be aware of the fact that instances of BaseB will all share the same x, but could have different values of y. Is that the intended behaviour?
Personally I'd consider making void registerValue() a pure virtual function in the base class, and expect all derived classes to implement that method including all necessary storage for its implementation. Perhaps that necessitates a base class function virtual int getRegistedValue() = 0 too?

Can a private base class member variable be modified in a derived class?

Is there a way to set a private member variable of a base class to a value in the constructor of a derived class?
I understand that's what getter and setter methods are for and what making the variable protected or public is for, but assuming you can't modify the base class, is there any alternate way to set it?
No. It's private. That is the whole point of private.
From the clarifications in the comments - the base class does give you a way to do it via its constructor, so you can use that.
// Assuming MyBaseclass has a 1 int constructor to set the
// private member, then something like this works.
//
MySubclass(int x) : MyBaseclass(x) {}
I understand that's what getter and setter methods are for and what making the variable protected or public is for, but assuming you can't modify the base class, is there any alternate way to set it?
Sure. The alternative way instead of using setter functions in the derived class constructor body, is to use an appropriate constructor from your derived class to call an initializing constructor of the base class (as you state it exists in your comment):
class Base {
public:
Base(int x, int y) : x_(x), y_(y) {}
private:
int x_;
int y_;
};
class Derived : public Base {
public:
Derived() : Base(15,42) {}
// ^^^^^^^^^^^^^
}:
See more details about the member initializer list.
Yes, we can do so by calling a getter function from the base class. Here is an example:
#include<iostream>
using namespace std;
class A{
int x;
public:
void setx(int x1) {
x = x1;
}
int getx() {
return x;
}
};
class B: public A {
};
int main() {
B b;
b.setx(1000);
cout << b.getx();
return 0;
}

Base class functions that use derived class variables

In c++, Is there a standard way to create a function in a base class that can use the variables of derived classes?
class Foo{
private:
int x;
public:
Foo(){
x = 2;
}
void print(){
std::cout << x << std::endl;
}
};
class Derived : public Foo{
private:
int x;
public:
Derived(){
x = 4;
}
};
void main()
{
Derived a;
a.print();
}
This code prints the variable of the base class ( 2 ). Is there a way to make a function used by many derived classes to use the class's private variables without passing them as parameters?
Edit: My intentions are, to avoid writing the same code for each derived class.
For example, I want a get_var() in all, but this function should return the variable of that own class. I know I can make virtual and override, but I was looking for a way that I don't need to write again and again.
No, it is not possible for the base class to access anything in the derived class directly. The only way for a derived class to share anything with its base class is by overriding virtual member functions of the base class.
In your case, however, this is not necessary: the derived class can set variable x in the base class once you make it protected, and drop its own declaration as unnecessary:
class Foo{
protected: // Make x visible to derived classes
int x;
public:
Foo(){
x = 2;
}
void print(){
std::cout << x << std::endl;
}
};
class Derived : public Foo{
public:
Derived(){
x = 4;
}
};