Thread: confusion in understanding the inheritance concept!

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    confusion in understanding the inheritance concept!

    Code:
    #include<iostream>
    using namespace std;
    class A {
    protected:
      int i;
    public:
      void  fun(int p)	
    	{
    		i=p;	
    		cout<<"i= "<<i<<"p= "<<p<<endl;
    	}
    };
    
    
    class B: public A
       {
    	public: 
    	void function()
    		{
    			cout<<i<<endl;	
    		}
       };
    int main()
    	{
    		A obj;
    		obj.fun(123);
    		B object;
    		object.function();
    		return 0;
    	}
    The output of this code is:
    123 123
    ##GARBAGE VALUE##

    why is it happening? At the time when I passed 123 to p & then 'i' copied the value 123 into it, I thought it must have stored 123 into the memory space allocated to it, but after inheriting it 'function()' in class B is not printing the value. WHY?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    obj and object are two different instances of things.
    Simply storing 123 in obj has NO possible relationship to the data you see in object.

    > class B: public A
    This means B contains it's own copy of an A

    It does not mean, go looking through the code for other declarations of A (like on line 25) and hijack them into B.

    It would help if your example at least had default constructors for everything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    So the memory has nothing to do with that! What good would default constructor do?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by gaurav# View Post
    What good would default constructor do?
    You have no way to set A::i currently, using default constructors would give you one.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > What good would default constructor do?
    Well if you had a default contructor in A that assigned the value say 42 to i, then you'd get a more consistent answer.

    Further, if you put a cout statement in both default constructors, you would see two instances of A being created.
    One of them would be the standalone instance of A
    The other would be an inherited instance within B

    Finally, having default constructors which initialise all the members (well all the Plain-Old-Data members) is a good thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-24-2013, 10:00 PM
  2. confusion in array concept...
    By xterminator in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 11:51 AM
  3. confusion on multiple inheritance
    By Stream in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2011, 11:39 AM
  4. Trivial Trigonometry Question, Need Help Understanding Concept
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-14-2003, 05:50 PM
  5. Confusion over inheritance
    By dalek in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2003, 07:10 AM