0

In the following code I would expect to get an output of 5, but instead I get arbitrary trash. Why is x not being set to 5? Wouldn't the constructor be invoked when I declare the derived class object and set the value of x to 5?

#include <iostream>


using namespace std;

class Base{

public:
    int x;
    Base(){
    }
    Base(int arg)
    {
        x = arg;
    }
};

class Derived: public Base{
public:
    Derived():Base(5){
    }

};

int main() {


   Base obj2;
Derived obj1;
   cout << obj2.x;

    return 0;
}
Sarah Sepanski
  • 189
  • 2
  • 10
  • Why did this post get downvoted? I need to know so I can write better questions! – Sarah Sepanski Jul 20 '17 at 18:01
  • if you hover your cursor to the downvote near question you can see the reason it's "Doesn't show any research effort". – Saubhagya Srivastava Jul 20 '17 at 18:08
  • @SaubhagyaSrivastava Thanks. I definitely did put effort into researching this before I asked. What can I do next time to demonstrate that I have put in that effort? – Sarah Sepanski Jul 20 '17 at 18:12
  • @SarahSepanski What kind of research did you do? Did you, for example, put `cout` statements in constructors to see which ones are invoked, with what arguments (I realize that asking to step through the code with a debugger, to better understand code flow, might be out of question for a beginner)? Or, alternatively, you could read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), to understand of how this work, and why. – Algirdas Preidžius Jul 20 '17 at 18:18
  • 1
    Re your meta question... if you're asking extremely basic questions in the C++ tag, you're going to get murdered. Maybe even literally. These guys are crazy, and I'd be too if I coded c++. You DEFINITELY need to learn how to debug before asking any questions. Asking others to debug for you is considered rude and a waste of other people's time (i.e., you would have figured this out if you had debugged your code). If your current toolset doesn't support debugging, you need to find something that does. Visual Studio is free, try it out. –  Jul 20 '17 at 18:29
  • Possible duplicate of [What are the rules for calling the superclass constructor?](https://stackoverflow.com/questions/120876/what-are-the-rules-for-calling-the-superclass-constructor) – Mykola Yashchenko Jul 20 '17 at 19:05

2 Answers2

1

In your example, you are not passing any values to the obj2 constructor, so your x member remains not initialized.

Remember that obj1 and obj2 do not share any members, they are separate, disconnected instances.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • ah, thank you. that really clears things up. i appreciate it. any idea why this post got downvoted? I need to know so I can write better questions – Sarah Sepanski Jul 20 '17 at 18:04
  • I have never figured out the thinking of these downvoters. I wish they would comment as to why they downvoted. – Thomas Matthews Jul 20 '17 at 20:04
0

Your Base default constructor does not initialize x and it has no in-class initializer, so Base obj2; constructs an instance with an uninitialized x and reading from uninitialized variables is Undefined Behaviour.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70