0

Lets discuss on the next code sample:

class A
{
public:
    int a;
};

class B:public A
{
public:

    int a;
};


int main()
{
    B b;
    std::cout << b.a;
    system("pause");
    return(0);
}

Why if I'm writing its like this, So its cant compiles, and giving me error.

But if I add a constructor to B class, like this:

B()
{
// An empty constructor!!!
}

then it prints on the screen a garbage value ('a' variable value.). And why I don't need any constructor if I'm writing the class like this:

 class B:public A
 {
 public:
     int a = 5;  // 5 is just one of many possibilities...
 };

In that case, 5 will be printed on the screen.

eddie
  • 1,252
  • 3
  • 15
  • 20
fdwfg wdfwdfv
  • 229
  • 1
  • 4
  • 9
  • 1
    Your original code *does* compile, unless it is really missing the necessary includes for `std::cout` and `system`. It has undefined behaviour and some other issues, but it compiles. So you have not posted the correct code, it seems. You should also use a spell checker or just pay more attention to potential typos. – Christian Hackl Jan 22 '17 at 10:51
  • @ChristianHackl Its compiled to you? and gave you a garbage value? I did everything correct! maybe its just undefined behaviour because Its not makes any sense to the compiler when I have member variable but not doing anything with initialize it? – fdwfg wdfwdfv Jan 22 '17 at 10:55
  • Yes, it compiles if I add `#include ` (and remove the `system("pause")`, which is a horrible thing to do anyway). Since you claim that the code does not compile unless you add the constructor, you are obviously not talking about the code which you have posted. I'll gladly explain the undefined behaviour (which to you appears as "garbage value"), but only if you actually post the code which gives you trouble. – Christian Hackl Jan 22 '17 at 10:58
  • @ChristianHackl I removed now the syste("pause") and its also works for me... weird...thanks!! – fdwfg wdfwdfv Jan 22 '17 at 11:21
  • @ChristianHackl For the secons question - Its like doing an inizializing list in a default constructor? Its exactly the same? – fdwfg wdfwdfv Jan 22 '17 at 11:22
  • It cannot work without `#include ` or without at least some other `#include` which happens to pull in ``. As for `system`, you'd need `#include ` to make it compile in a portable manner. But you don't want to use `system("pause")` anyway; see http://stackoverflow.com/a/36799058/3313064 – Christian Hackl Jan 22 '17 at 11:25
  • Since your question no longer makes sense (it contains code that doesn't match the problem description), you should probably ask a new question. Or just read all the material on constructors and variable initialisation available in C++ books: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Christian Hackl Jan 22 '17 at 11:26
  • @ChristianHackl Thank you, but you can answer on this : __"For the secons question - Its like doing an inizializing list in a default constructor? Its exactly the same?"__ – fdwfg wdfwdfv Jan 22 '17 at 11:41

1 Answers1

2

Constructors can be used to assign or initialize values to the data members by doing either one of this:

B()
   :a(5) // intialize a with value 5
{

}

or

B()
{
    a = 5; // assign a with value 5
}

the 2nd one initializes a with garbage value and then assign 5 to it.

data members are initialized before the constructor's body is executed.

And why I dont need any constructor if im writing the class like this:

its not that you dont need constructor, its just that it already has a value.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
Lorence Hernandez
  • 1,189
  • 12
  • 23
  • Hey. first - thanks alot :). second - you didnt answer my first question - why does the first sample doesnt also gives me a garbage value on the screen, and instead, its gives me an Error,, (Till I make an wmpty constructor like the socend code sample.) And another somthing : what do you mean by "Its just that it already has a value"? – fdwfg wdfwdfv Jan 22 '17 at 10:48
  • you shouldnt get error with the code in your first example, can you paste the error here? `what do you mean by "Its just that it already has a value"?` because the a is already allocated in the stack. (it doesnt make sense that it will be constructed again when its already constructed) – Lorence Hernandez Jan 22 '17 at 10:52
  • There is no initialisation with a "garbage value". This may or may not happen in practice on your machine, but from a C++ language point of view, reading from an uninitialised and unassigned `int` is simply undefined behaviour. – Christian Hackl Jan 22 '17 at 10:53