Thread: How do I override constructor in inheritance?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    40

    How do I override constructor in inheritance?

    I'm trying to use inheritance now to make things much more organised. I have a superclass of type item and an inheriting class of type special_item.

    The book I'm reading an example from has something like this
    Code:
    class object
    {
      object(void){...}
    };
    class object2: public object
    {
      object2(void){...}  //overrides default constructor
      object2(parameters){...}  //overloads a constructor that was not present
    };
    So in my test, I did something similar, except that I left the parameters for the second constructor (that is, the one it says is overloading the constructor) to be void. The compiler doesn't like that, as it looks just like the one immediately preceeding it.

    So what's the deal? If you want to override the constructor with a new one do you have to have different parameters? What if I want it to be void in both? Should I just give it a parameter that defaults to NULL and ignore it?

    Here is my code, if it matters.
    Code:
    class item
    {
      public:
      string name, desc;
      unsigned short int id;
      friend bool operator==(const item& lhs, const item& rhs){return (lhs.id==rhs.id);}
      item(void)
      {
        id = item_id++; name = "test"; desc = "a test object";
      }
    };
    class special_item: public item
    {
      public:
      special_item(void){}
      special_item(void)
      {
        id = item_id++; name = "special"; desc = "special, one of a kind object";
      }
    };
    (edit)
    I realise that, in this specific instance, it doesn't make a difference whether the parent class' constructor is called; but this is just a test program for learning, and I'd like to figure it out anyway.
    Last edited by Loduwijk; 03-22-2006 at 09:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Inheritance
    By mrafcho001 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2006, 03:49 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM