Thread: Do I have to override functions in a base class?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Do I have to override functions in a base class?

    Hello, everyone. I'm trying to get my head around overriding functions of a base class, in C++. In this code:

    Code:
    class Base {
      public:
        Base()  {}
        virtual ~Base() {}
    
        virtual foo() {}
        virtual bar() = 0;
    };
    1. My understanding is that bar() must be overridden in any derived class that is instantiated. Correct?

    2. Is it the case that any derived class may or may not implement its own constructor and/or destructor and/or foo(), and it's completely okay? In any combination?

    Richard

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    1. Not "must" but "will". Unless you use the C++11 " = delete;" syntax in your derived class, a destructor is always generated.

    2. In addition to what I said above, a constructor is also required. It doesn't have to be the same as the base class constructor, but without at least one you can't create the object.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-11-2016, 01:15 AM
  2. Replies: 4
    Last Post: 01-24-2016, 05:02 AM
  3. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  4. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  5. Calling Base Class Functions
    By Asmodan in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2002, 02:48 AM

Tags for this Thread