Hi friends,
I was trying out some inheritance related stuff and found something. I just want to know the reason why.
I have a ParentClass and a ChildClass. Both have an integer named commonVar1. Also both have a getter method of the same name. Here goes the code.
class ParentClassOT
{
public:
ParentClassOT();
~ParentClassOT();
int commonVar1;
int GetcommonVar1() { return commonVar1; }
};
class ChildClassOT : public ParentClassOT
{
public:
ChildClassOT();
~ChildClassOT();
int commonVar1;
int GetcommonVar1() { return commonVar1; }
};
In my main method I'm doing something like this
ChildClassOT obj1;
cout<<obj1.GetcommonVar1();
Now I'll give out the results of different combinations I have tried
1. Code as it is : outputs garbage
2. Parent getter (line 7) commented : outputs garbage
3. Child member variable (line 6) commented : outputs garbage
4. Child getter (line 7) commented : outputs -1
5. Child getter and member variable commented : outputs garbage
I'm okay with results 1,2,3 and 5. But I don't get why 4th one gives a -1.
Could somebody please help... :confused: