1
#include<iostream>

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();
}

I just want to print the values of the a and b members using the b1 object of class B, as it can access the member functions of class A since class B has public inheritance of class A. But, I am getting garbage values when I try to print the values of a and b using b1.

Can someone explain why this is happening and how to fix it?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
ravikiran
  • 33
  • 6
  • 2
    You don't call `setdata` on the `b1` instance. And please read up on constructors. –  May 30 '18 at 18:49
  • It seems like you assume `b1` should print the values provided to `a1`, but `b1` has nothing to do with `a1`. What if you have another `A`? How would `b1` know which `A`'s value to use? I'm not sure I understand your mental model for how inheritance works. – François Andrieux May 30 '18 at 18:49

1 Answers1

3

a1 and b1 are completely separate object instances in memory. They have their own copies of the a and b members in memory. They have nothing to do with each other at all. Whatever you do to a1 does not affect b1 at all, and vice versa.

You are initializing the members of a1 only, you are not initializing the members of b1 at all. That is why you are seeing garbage when you try to print out the members of b1.

Before calling b1.showdata(), you need to call b1.setdata() to initialize b1's members, eg:

int main() {
    A  a1;
    B  b1;
    a1.setdata(5, 4);
    a1.showdata();
    b1.setdata(1, 2); // <-- add this!
    b1.showdata();
}

You should also give class A a default constructor that initializes the members to default values, in case setdata() is not called after construction (such as what happened in your case), eg:

class A {
    int a, b;
public:
    A() : a(0), b(0) {} // <-- add this!
    void setdata(int x, int y) { a = x; b = y; }
    void showdata() { std::cout << a << b; }
};

Alternatively, you might consider giving class A and class B a constructor that takes values as input, eg:

class A {
    int a, b;
public:
    A() : a(0), b(0) {}
    A(int x, int y) : a(x), b(y) {} // <-- add this!
    void setdata(int x, int y) { a = x; b = y; }
    void showdata() { std::cout << a << b; }
};

class B : public A {
public:
    B() : A() {}
    B(int x, int y) : A(x, y) {}
};

/* or simpler:

class B : public A {
public:
    using A::A; // <-- inherit all constructors
};
*/

int main() {
    A  a1(5, 4);
    B  b1(1, 2);
    a1.showdata();
    b1.showdata();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It's ok. But my question is, as B inherits the member functions of A, I am trying to print the values of a and b of a1 using b1 through showdata () as b1 can use it. But it is not working, why?. – ravikiran May 30 '18 at 19:07
  • Read the 1st paragraph of my answer, I already addressed that question. Just because `B` derives from `A` does not mean that object `b1` can access object `a1`'s data. You clearly have a fundamental misunderstanding of how objects actually work. You need to get yourself a [good C++ book](https://stackoverflow.com/questions/388242/) that covers this in detail. – Remy Lebeau May 30 '18 at 19:23
  • What I am trying to do is to print the variables of a1 using b1 but not the variables of b1 as b1 can access the showdata() function because of inheritance. – ravikiran May 31 '18 at 04:13
  • @Remy Lebeau If B cannot access the values of a then how was I able to set values to a and b of class A using setdata() with the help of an obj of class B that is b1 and was able to print them using showdata() usng the same b1. – ravikiran May 31 '18 at 04:26
  • @ravikiran You clearly do not understand what inheritance is or how objects work. Again, stop what you are doing and go get some [good C++ books](https://stackoverflow.com/questions/388242/) to study. `B` simply inherits the same *functionality* as `A`. What `A` can do, `B` can also do. That DOES NOT mean that `b1` has access to `a1`'s data members. `b1` has its own data members. `a1` and `b1` DO NOT share the same memory, so what you are asking for is simply not possible. – Remy Lebeau May 31 '18 at 06:01