-3
#include <bits/stdc++.h>
using namespace std;

class Parent {
    public:
    int id_public;
};

class Child1 : public Parent {
    public:
    void setId(int id) {
        id_public = id;
        cout<<id_public<<endl;
    }
};

class Child2 : public Parent {
    public:
    void setId2(int id) {
        cout<<id_public<<endl;
        id_public += id;
        cout<<id_public<<endl;
    }
};

int main() {
    Child1 obj1;
    Child2 obj2;

    obj1.setId(81);
    obj2.setId2(5);

    return 0;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    Don’t `#include` anything from `bits/`. – Davis Herring Oct 28 '18 at 19:47
  • 2
    Recommend moving the question from the title to the question. In addition, `` is not a standard header and should not be used ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)). `using namespace std;` should be avoided ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). When you combine the two the odds of you encountering problems rises dramatically. – user4581301 Oct 28 '18 at 19:48
  • Why do you think obj2,id_public should be 86? Why do you think the two instances (obj1.id_public and obj2.id_public) should occupy the same memory? – 2785528 Oct 28 '18 at 20:01
  • @2785528: "id_public" is the feature of the parent class. So, when child1 and child2 are inheriting the "id_public" feature, I'm thinking both the child class and the parent class should have the same copy of that feature with same memory location.....where did my thinking go wrong? – Joydwip Mohajon Oct 29 '18 at 20:50
  • In C++, each child instance has it's own parent instance. So the two parents are distinct, and that explains why they occupy different memory. – 2785528 Oct 29 '18 at 22:31
  • @2785528: Is it just a C++ feature or an OPP feature! I mean, does it differ in case of Java? – Joydwip Mohajon Oct 30 '18 at 19:25
  • @JoydwipMohajon - I do not know Java. – 2785528 Oct 31 '18 at 03:33
  • @2785528: Thanks BTW – Joydwip Mohajon Nov 02 '18 at 17:17

1 Answers1

3

It’s nothing to do with two classes: you just have two objects, each with its own member variables. A variable has to be static to be shared (which is rarely needed).

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Also, in `obj2`, the member `id_public` has not been initialized. – Pete Becker Oct 28 '18 at 21:00
  • @PeteBecker: Well, yes—they’re separate variables *and* there’s undefined behavior. – Davis Herring Oct 28 '18 at 21:40
  • @DavisHerring: "id_public" is the feature of the parent class. So, when child1 and child2 are inheriting the "id_public" feature, I'm thinking both the child class and the parent class should have the same copy of that feature with same memory location.....where did my thinking go wrong? – Joydwip Mohajon Oct 29 '18 at 20:46
  • @JoydwipMohajon: The *classes* do share just one member variable, but two objects even of *one* class share none of their (non-`static`, again) member variables. Being of different, related classes won’t make them share *more*. – Davis Herring Oct 30 '18 at 03:29