Two basic question about inheritance

This is a discussion on Two basic question about inheritance within the C++ Programming forums, part of the General Programming Boards category; OK, let's say I have an abstract base class BaseClass and a derived class Derived: Code: class BaseClass { public: ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Two basic question about inheritance

    OK, let's say I have an abstract base class BaseClass and a derived class Derived:

    Code:
    class BaseClass
    {
      public:
        BaseClass();
        virtual ~BaseClass();
        virtual void DoSomething() const = 0;
    };
    Is it necessary to redeclare each virtual function in the derived class, or will just the definition suffice? i.e. will the following work?
    Code:
    class Derived: public BaseClass
    {
      public:
        Derived();
        virtual ~Derived();
    };
    
    void Derived::DoSomething()
    {
      DoStuffADerivedWouldDo();
      DoSomeOtherStuff();
    }
    If I do have to redeclare it, will it make any difference whether the redeclaration is virtual or not? (i.e. is there any reason not to make it virtual, or will it end up virtual even if I don't declare it as such?)

    Also, if I do something like this:
    Code:
      BaseClass *abaseclass=new Derived;
    Do the constructors from both classes get called, or do I need to do everything in Derived's constructor that I had to do in BaseClass's?

    Thanks in advance.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Virtual member functions in a base class are virtual in derived classes, whether you put virtual or not.

    Base constructors are always called before derived constructors.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, you have to declare the functions in the derived class if you want to provide a derived class implementation. You should mark them as virtual to make the code clearer, even though they will be virtual anyway. If you aren't overriding the base class implementation, then you don't need to declare the function in the derived class.

    Your derived class constructor can choose which base class constructor to call if there is more than one option. You call the base class constructor in the intialization list. If you don't call one from the initialization list, the base class's default constructor is called.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Boksha

    Also, if I do something like this:
    Code:
      BaseClass *abaseclass=new Derived;
    Do the constructors from both classes get called, or do I need to do everything in Derived's constructor that I had to do in BaseClass's?

    Thanks in advance.
    While both constructor's will be called, only the base destructor will be called unless you make the destructor virtual.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by Darryl
    While both constructor's will be called, only the base destructor will be called unless you make the destructor virtual.
    That may be true in practice with some compilers, but is not required.

    The code;
    Code:
        Base *derived = new Derived;
        delete derived;
    actually yields undefined behaviour if the destructor of Base is not virtual.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Thanks people, that'll keep me going for a while.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C question ---- URGENT
    By x135 in forum Linux Programming
    Replies: 3
    Last Post: 03-25-2006, 11:05 PM
  2. Inheritance related question. Is this correct?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2002, 05:30 AM
  3. Visual Basic Question?
    By ob1cnobe in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 10:31 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 04:37 PM

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