Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:55:01+00:00 2026-06-14T20:55:01+00:00

Possible Duplicate: pure virtual function with implementation I have a base class which has

  • 0

Possible Duplicate:
pure virtual function with implementation

I have a base class which has a function called time().
My idea is to have a pure virtual function which all the classes that inherits from it must implement.

However I also want to add the default behavior; Whoever calls time() function needs to print “hello”.

Is that possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:55:03+00:00Added an answer on June 14, 2026 at 8:55 pm

    The easiest way to achieve something like this would be to have something like the following:

      class base {
           virtual void time_impl()=0;
      public:
           void time() {
                 //Default behaviour
                 std::cout << "Hello\n";
                 //Call overridden behaviour
                 time_impl();
           }
      };
    
      class child : public base {
           void time_impl() override {
                //Functionality
           }
      };
    

    This way, when any child’s time function is called, it calls the base classes time function, which does the default behaviour, and then make the virtual call to the overridden behaviour.

    EDIT:

    As Als says in the comments, this is called the Template method pattern which you can read about here:

    http://en.wikipedia.org/wiki/Template_method_pattern

    Althought for C++ that is a rather confusing name (as templates are something else, and “methods” don’t mean anything in C++ (rather member function).

    EDIT:

    Someone commented about the use of override, this is a C++11 feature, if you have it USE IT, it will save many headaches in the long run:

    http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

    And also, the _impl function should be private, UNLESS you want the user to be to bypass the default behaviour, but I would argue that’s a poor design in most use-cases.

    Working test case

    http://ideone.com/eZBpyX

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: pure virtual function and abstract class I have a class and I
Possible Duplicate: Why pure virtual function is initialized by 0? In C++, what does
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicate: Pure virtual destructor in C++ I have two classes: the abstract Game
Possible Duplicate: Pure virtual destructor in C++ class A{ public: virtual ~A()=0; }; class
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Why shall I use the “using” keyword to access my base class
Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
Possible Duplicate: Highlight keywords in a paragraph I have been struggling with the idea
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.