Like Tree1Likes
  • 1 Post By MishaMish

simlpe inheritance question

This is a discussion on simlpe inheritance question within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to cpp, I have good knowledge in java and actionscript3. I want a base class with one ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    9

    Question simlpe inheritance question

    Hi,
    I'm new to cpp, I have good knowledge in java and actionscript3.
    I want a base class with one function:
    initConfig - it will initialize some variables.

    This function will get called from the constructor of the base class.

    I want a derived class that will override this function, with it's own implementation, but still call the base's class implementation too.

    so it should look like this:
    * An object is created from the derived class.
    * the constructor of the base class is called.
    * in the base's class constructor there's a call to initConfig.
    * the derived class's implementation of initConfig is executed and than the base's class implementation of initConfig.

    I tried many things, but when calling a function from a base class it's calling the base's class's implementation ONLY, without calling the derived implementation first...

    how do I do that?

    Thanks
    Gil
    Last edited by MishaMish; 03-29-2011 at 09:00 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,359
    That's great. It sounds exactly like you know what you want.

    *sigh*

    Now, post what you have so we can see what you've done.

    Soma

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MishaMish View Post
    I tried many things, but when calling a function from a base class it's calling the base's class's implementation ONLY, without calling the derived implementation first...
    You could call the derived method inside the derived constructor, which would mean the base method happens first. If you must have the derived method fire first, you'll have to remove "initConfig" from both constructors and call it manually, calling the base method inside the derived one.

    Code:
    #include <iostream>
    
    class One {
    	public:
    		One (int val) : x(val) { }
    		void initConfig () {
    			x += 2;
    		}
    		int x;
    };
    
    class Two : public One {
    	public:
    		Two (int val) : One(val) { }
    		void initConfig () {
    			x = x*x;
    			One::initConfig();
    		}
    };
    
    using namespace std;
    
    int main(void) {
    	Two eg(5);
    	eg.initConfig();
    
    	cout << eg.x << endl; 
    
    	return 0;
    }
    Last edited by MK27; 03-29-2011 at 10:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    ok, I've asked few more people, and the answer is:
    I'm trying to call a function from the base class's constructor, and the derived class was not created (yet). so calling this function will not trigger the derived class implementation (because it doesn't exist, yet)

    hope this helps someone.
    phantomotap likes this.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MishaMish View Post
    ok, I've asked few more people, and the answer is:
    I'm trying to call a function from the base class's constructor, and the derived class was not created (yet). so calling this function will not trigger the derived class implementation (because it doesn't exist, yet)
    Yep, this is why I said you have to remove initConfig() from the constructor and call it manually. The base constructor is called to create the base for the object of the derived class before it exists; you cannot circumvent that.

    Not all OO implementations are identical. This is sort of a limitation, but not a very serious one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    One important thing, even after construction is complete if you call initConfig from inside the base class you'll get the base class's version of initConfig. To change this you need to make initConfig virtual by doing something like:

    Code:
    class Base
    {
    public:
        virtual void initConfig() { /* base behaviour */ }
    }
    
    class Derived
    :
        public Base
    {
    public:
        virtual void initConfig() { /* derived behaviour */ }
    }
    This is an important difference with java - as I understand it, basically all methods in java behave virtually. In C++, you have to make the methods virtual to get this behaviour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Noob question about templates & inheritance
    By blacknail in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2008, 02:51 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21