I'm learning C++ and am a fair beginner at it. I'm having a little trouble understanding why there's a logical or a runtime error. Can anyone explain what I did wrong?
I already know a solution by removing my functions in the classes A and B and simply defining it in the constructor but if that's the correct way why can't I initialize it in some other function like I have done?
#include <stdlib.h>
#include<iostream>
using namespace std;
class A
{
protected:
int x;
public:
void Init()
{
x = 10;
}
};
class B
{
protected:
int x;
public:
void Init()
{
x = 20;
}
};
class C: public A, public B
{
public:
void Test()
{
cout << "A's x is " << A::x;
cout << "\nB's x is " << B::x;
}
};
int main()
{
C c;
c.Test();
return 0;
}
I was Expecting A normal result that i would usually get in java with "this" function.
i.e.
A's x is 10
B's x is 20
But what I'm getting is
A's x is 8
B's x is 50