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() {}
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
#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() {}
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?
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.
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.