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:

Is that all the codes that you have? Did you ever set any value to your variable 'commonVar1' before calling GetcommonVar1() method? If not, I suppose the result would be unexpected and the -1 is only a coincidence.

If I'm not wrong, the child's property 'commonVar1' takes precedence over the parent's. Anyway as a side note, I don't see any reason why you should declare a variable in the child class having the same name as the parent's.

Did you ever set any value to your variable 'commonVar1' before calling GetcommonVar1() method?

there is no setter method. I intentionally did it like this to see a garbage value.

I don't see any reason why you should declare a variable in the child class having the same name as the parent's.

yes I know. This is not a professionally written code. I was learning inheritance and was trying out different combinations.

If it's a coincidence then i'll have to check with other compiler. But i feel this is not a coincidence.

-1 is still garbage

Did you ever set any value to your variable 'commonVar1' before calling GetcommonVar1() method?

there is no setter method. I intentionally did it like this to see a garbage value.

I don't see any reason why you should declare a variable in the child class having the same name as the parent's.

yes I know. This is not a professionally written code. I was learning inheritance and was trying out different combinations.

If it's a coincidence then i'll have to check with other compiler. But i feel this is not a coincidence.

If you don’t initialize your variables, you will get garbage...
garbage in this case is any value that int is capable of representing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.