-6
#include<iostream>
using namespace std;
class A
{
    private:
    int a,b;

    public:
    void setdata(int x,int y){
         a=x;b=y;
    }

    void show_data(){
         cout<<a<<b;
    }
};

class B: public A{
};

main(){
   B b1;
   b1.setdata(3,4);
   b1.show_data();
}

How does setdata work even if we don't create an object of class A (how did the variables a and b get memory)? And how was it possible to access the private variables of A using an object b1 of class B? I am surprised to see my program working properly.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
ravikiran
  • 33
  • 6
  • 4
    You might want to grab a [good book](https://stackoverflow.com/q/388242/1782465) to get a firmer understanding of C++ basics. This would give you a more thorough overview than individual SO questions can. – Angew is no longer proud of SO May 31 '18 at 10:12
  • Possible duplicate of [C++, What does the colon after a constructor mean?](https://stackoverflow.com/questions/2785612/c-what-does-the-colon-after-a-constructor-mean) – DDS May 31 '18 at 10:16
  • 1
    @DDS What? That one is about mem-initializer-lists, this one is about misunderstanding basics of inheritance. – Angew is no longer proud of SO May 31 '18 at 10:17
  • Try removing the `public A` in the `class B: public A{` line – Jose May 31 '18 at 10:18
  • 1
    Class `B` inherits from `A`. So part of a `B` is an `A`. When a `B` is constructed, so are its parts. – Peter May 31 '18 at 10:22
  • Is this a variant of the [*"private members are not inherited"*](https://stackoverflow.com/questions/2676443/inheriting-private-members-in-c) misunderstanding? The thing is that everything in `A` is also a part of `B`. – Bo Persson May 31 '18 at 12:05

2 Answers2

3

How does setdata work even if we don't create an object of class A (how did the variables a and b get memory)

But the code does create an object of class A, right here:

B b1;

Since B is derived from A, each object of type B contains an object (the base class subobject) of type A.

How was it possible to access the private variables of A using an object b1 of class B

Yes, the object is of type B, but the function actually doing the access (setdata) is a member of class A, and thus has a member's access rights to all of class A.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • If so then why can't I access the a and b of A using show_data() with b1(getting garbage values). #include#include class A { int a, b; public: void setdata(int x, int y) { a = x; b = y; } void showdata() { std::cout << a << b; } }; class B : public A { }; int main() { A a1; B b1; a1.setdata(5, 4); a1.showdata(); b1.showdata(); } – ravikiran May 31 '18 at 17:29
  • @ravikiran Because you never called `setdata` on `b1` in the code you posted in the comment? – Angew is no longer proud of SO May 31 '18 at 18:54
  • 1
    @ravikiran When you declare a class (`class A {...}`), you are defining a type of object. When you create an object (`A a1`), you are creating an instance of that type. e.g: Let's rename your class `A` to `Animal` and your class `B` to `Dog`. So you have `class Animal {...}` and `class Dog: public Animal {...}`. Now when you create `Animal tucker` and `Dog buddy`, then `buddy` can do all the things `tucker` can do, because it's also an `Animal`, but `tucker` and `buddy` are different individuals, so when you `setdata` on one, you can't `show_data` on the other. – tomas Jun 04 '18 at 12:21
0

That's how inheritance is supposed to work: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/inher.htm

In short, you've created an object of type B and you've declared that class B inherits from class A. That means that an object of type B is also of type A, just more specific. The same way a float is a number, while an integer is also a number. A Dog is an Animal, while a Cat is also an Animal.

When you define

class B: public A { };

that means that class B should inherit all public methods from class A. setdatais a public method, as is show_data()

tomas
  • 963
  • 6
  • 19
  • 1
    I'm not sure the int/float/number relationship is a useful analogy here, since _number_ is not a base class. – Useless May 31 '18 at 10:28
  • Sure, I was not speaking about any specific language in that example, just trying to explain the idea. imo, the int/float/number analogy is just as useful as the cat/dog/animal one. – tomas Jun 04 '18 at 12:14