Passing/updating base class variable value by sub-class

Variable id_protected value (base class) is not revised by subclass member function newID(int multiply) by newobj

Why * int finalID; // cannot use static ??*

#include <Arduino.h>
// *************************************************************************
// ******* important  protected: is very different from private:  **********
// ****************************************************************************

//=========== protected access modifier===============

// base class
class Parent {

  // protected data members
protected:
  int id_protected;

public:
  int finalID;  // cannot use static ??

public:
  void printParent() {
    Serial.println("id_protected in Parent is:");
    Serial.println(id_protected);
    Serial.println("id_protected in Parent via finalID is:");
    Serial.println(finalID);
  }
};

class Child : Parent {

protected:

  String ChildName;  // protected

public:

  int setId(int id) {

    // Child class is able to access the inherited
    // protected data members of base class

    id_protected = id;

    finalID = id_protected;  // ***compile erroe if static was declared earlier ???

    return id_protected;
  }

  void displayId() {
    Serial.println("id_protected is:");
    Serial.println(id_protected);
  }
};


class Child2 : Parent {
public:
  int newID(int multiply) {

    Serial.println("id_protected from Child2 A is:");
    Serial.println(id_protected);
    id_protected = id_protected * multiply;  // sub class accessable to protected parant member but value wrong ????
    Serial.println("id_protected from Child2  B is:");
    Serial.println(id_protected);
    return id_protected;
  }
};

// main function
void setup() {

  Serial.begin(9600);
  delay(500);

  Parent myParent;

  Child obj1;

  // member function of the derived class can
  // access the protected data members of the base class
  myParent.printParent();  //  somehow the value id_protected set earlier is not passed from child to parent ?

  obj1.setId(81);
  myParent.printParent();
  obj1.displayId();

  Child2 newObj;
  newObj.newID(2);

  myParent.printParent();


}

void loop() {}

//==============================================================
results output >>>

id_protected in Parent is:
-14590
id_protected in Parent via finalID is:
-14846
id_protected in Parent is:
-14590
id_protected in Parent via finalID is:
-14846
id_protected is:
81
id_protected from Child2 A is:
0
id_protected from Child2  B is:
0
id_protected in Parent is:
-14590
id_protected in Parent via finalID is:
-14846

First problem is you never initialize id_protected and finalID

What are you trying to do exactly ?

compiles:

#include <Arduino.h>
// *************************************************************************
// ******* important  protected: is very different from private:  **********
// ****************************************************************************

//=========== protected access modifier===============

// base class
class Parent {

  // protected data members
protected:
  int id_protected;

public:
  static int finalID;  // cannot use static ??

public:
  void printParent() {
    Serial.println("id_protected in Parent is:");
    Serial.println(id_protected);
    Serial.println("id_protected in Parent via finalID is:");
    Serial.println(finalID);
  }
};
int Parent::finalID = 0;

class Child : Parent {

protected:

  String ChildName;  // protected

public:

  int setId(int id) {

    // Child class is able to access the inherited
    // protected data members of base class

    id_protected = id;

    Parent::finalID = id_protected;  // ***compile erroe if static was declared earlier ???

    return id_protected;
  }

  void displayId() {
    Serial.println("id_protected is:");
    Serial.println(Parent::id_protected);
  }
};


class Child2 : Parent {
public:
  int newID(int multiply) {

    Serial.println("id_protected from Child2 A is:");
    Serial.println(id_protected);
    id_protected = id_protected * multiply;  // sub class accessable to protected parant member but value wrong ????
    Serial.println("id_protected from Child2  B is:");
    Serial.println(id_protected);
    return id_protected;
  }
};

// main function
void setup() {

  Serial.begin(9600);
  delay(500);

  Parent myParent;

  Child obj1;

  // member function of the derived class can
  // access the protected data members of the base class
  myParent.printParent();  //  somehow the value id_protected set earlier is not passed from child to parent ?

  obj1.setId(81);
  myParent.printParent();
  obj1.displayId();

  Child2 newObj;
  newObj.newID(2);

  myParent.printParent();


}

void loop() {}

What do you want to achieve?

Thanks for taking this up.
I am trying to learn how to use protected modifier and see how sub-class can update base class value that is id_protected is for.
finalID was created try to circumvent the failed passing thinking static variable could work.
Please advise me how/where to assign?

so you don't need a static variable?
Have you already read about what static variables are doing in a class?

obj1.setId(81);
myParent.printParent();

Do you expect myParent.printParent() to print 81 ?

obj1 and myParent are not related in any way. Maybe you want obj1.printParent() instead ?

i have to admit not yet. but in functions . thanks i will do

I am expecting after newObj.newID(2); the id_protected be = 81 * 2 =122 . instead I got 0

But myParent, obj1 and newObj have no relationship, and they all have their own id_protected variable

I just could not understand this. I had tried another program earlier of similar and I got the correct result. Please advise me.

#include <Arduino.h>

class Box {
protected:
  double width;
};

class SmallBox : Box {  // SmallBox is the derived class. child
public:
  void setSmallWidth(double wid);
  double getSmallWidth(void);
  double set2xSmallWidth(int multiply);
  void printSmallBoxInfo();
};

// Member functions of child class
double SmallBox::getSmallWidth(void) {
  return width;  // tested still work without return width ?? 
}

void SmallBox::setSmallWidth(double wid) {
  width = wid;
}
  double SmallBox::set2xSmallWidth(int multiply) {
  width = width* multiply;
  Serial.print("small Width New= ");
  Serial.println(width);
  return width;  // tested still work without return width ??
}

void SmallBox::printSmallBoxInfo() {

  Serial.print("small Width = ");
  Serial.println(width);
}

// Main function for the program
void setup() {
  Serial.begin(9600);

  Box myBox;

  SmallBox mysmallbox;

  mysmallbox.setSmallWidth(5.0);
  mysmallbox.printSmallBoxInfo();

  // set box width using member function
  mysmallbox.setSmallWidth(25.0);
  mysmallbox.printSmallBoxInfo();

  mysmallbox.set2xSmallWidth(2);
  mysmallbox.printSmallBoxInfo();
}

void loop() {
}

How to make them related / the same referenced variable ?

Just read Static variable in Class. Now I remembered I had read . That was why I use static so it can be used by objects.

Try inline static int id_protected = 0; but I still don't understand what you are trying to do

Many thanks.
This is my self learning coding of " private" " static" and Class OOP. a bit advance coding and I just cannot figure the problem of passing, even though I tried another similar program and works. No practical application.
I fond out from digging the web further (geeksforgeeks) - as you have mentioned earlier:
each object created have own id_protected and no relationship. So as you said " inline static int id_protected = 0; " at the class definition will fixed the problem.

interesting - the web page demonstrated the same mistake I made.

The objective was to learn how "protected" , sub-class creation and passing value back from sub-class and read base class value.
Thanks again

that is a wrong assumption. a protected or a public member can be used by objects of a derived class.
a static variable in a class is a variable which exists (only once) on class level. If the derived members have access to that variable (granted by the modifiers public or protected) they access all the same variable.

static (in class context) is not a keyword to grant/deny access from a derived class.

better explanations can be found here:

https://www.learncpp.com/cpp-tutorial/static-member-variables/
https://en.cppreference.com/w/cpp/language/static

Many thanks for the explanations and link.
I was learning from very simple web resources .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.