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 8063267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:58:28+00:00 2026-06-05T10:58:28+00:00

I have a question about pure virtual functions. I am not clear about how

  • 0

I have a question about pure virtual functions. I am not clear about how it works and when we need to use pure virtual functions. This is the example that I do not understand:

file.h

class A
{
public :
            A();
            ~A();

            virtual void func1(void) = 0;
            virtual UINT32 func2(void) = 0;
            UINT32 initialize(void) = 0;
}


file.cpp

UINT32 A:initialize (void)
{
            func1();
            func2();
            return (result);
}

Can anyone explain in detail what this example actually does and what the result is?
I really appreciate your help and knowledge. Thank you very much.

  • 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-05T10:58:31+00:00Added an answer on June 5, 2026 at 10:58 am

    (Note, the declaration for initialize() should not be virtual, and the implementation of initialize() should return func2() probably. As this is an example, it doesn’t really matter what initialize() does, but it should compile correctly.)

    The primary purpose of virtual functions is to achieve polymorphism.

    Class A defines two pure virtual methods, and initialize calls them. This allows code in the program to initialize something of type A without being aware of the subclass. There may be many subclasses of A, and each may do something slightly differently within func1() and func2(). Since the code that only knows about A is sometimes initializing objects of different types through A, A can be referred to as a polymorphic type.

    class B : public A {
        void func1 () { std::cout << "B::func1" << std::endl; }
        UINT32 func2 () { return 1; }
    };
    
    class C : public A {
        void func1 () { std::cout << "C::func1" << std::endl; }
        UINT32 func2 () { return 2; }
    };
    
    void call_initialize (A *a) {
        std::cout << a->initialize()  << std::endl;
    }
    
    B b;
    C c;
    call_initialize(&b);
    call_initialize(&c);
    

    Results in the output:

    B::func1
    1
    C::func1
    2
    

    This is an example of polymorphic behavior, because the output is different depending on if A was subclassed by B or by C.

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

Sidebar

Related Questions

I have a question about how the System class works. Lets say there is
I have a question about generic collections in C#. If I need to store
Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
This question is about a general technique in SQL, that I can't quite work
Following on from this question about setting Culture, I have a similar issue with
Just be clear I am not asking about validation,just pure layout. I am trying
It may not be a pure programming question but I'm looking for information about
This question exist only because of pure curiosity. Not a homework. Find the fastest
I read different opinions about this question. Let's say I have an interface class
This question is for pure curiosity. Lets say I have lighttpd installed on my

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.