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

  • Home
  • SEARCH
  • 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 6931543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:35:30+00:00 2026-05-27T11:35:30+00:00

Consider the following sample code: class Base { public: void f(); virtual void vf();

  • 0

Consider the following sample code:

class Base {
public:
    void f();
    virtual void vf();
};

class Derived : public Base {
public:
    void f();
    void vf();
};

#include <iostream>
using namespace std;

void Base::f() {
    cout << "Base f()" << endl;
}

void Base::vf() {
    cout << "Base vf()" << endl;
}

void Derived::f() {
    cout << "Derived f()" << endl;
}

void Derived::vf() {
    cout << "Derived vf()" << endl;
}

int main()
{
    Base b1;
    Derived d1;
    b1.f();
    b1.vf();
    d1.f();
    d1.vf();

    Derived d2;     // Derived object
    Base* bp = &d2; // Base pointer to Derived object
    bp->f();    // Base f()
    bp->vf();   // which vf()?

    return 0;
}

The output of the run is:

Base f()

Base vf()

Derived f()

Derived vf()

Base f()

Derived vf()

Questions:

  1. In the line Base* bp = &d2, the object type is known at compile time. Then the decision of which function to use in the case of bp->vf(); can also be made at compile time right?

  2. Since the object type is known at compile time itself, is the power of virtual functions used in this sample program?

  • 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-05-27T11:35:31+00:00Added an answer on May 27, 2026 at 11:35 am

    This program is trivial, and does indeed not demonstrate the power of virtual functions (or, more generally polymorphism) very well. Consider this change:

    // add second derived class
    class Derived2 : public Base {
    public:
        void vf() { std::cout << "Derived2 vf()" << std::endl; }
    };
    
    // in main
    int user_choice;
    std::cin >> user_choice;
    Base * ptr;
    if (user_choice == 0)
        ptr = new Derived();
    else
        ptr = new Derived2();
    ptr->vf();
    

    Here, the choice of the class depends on user-input – the compiler has no way to predict what type of object ptr will actually point to when the call ptr->vf(); is reached.

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

Sidebar

Related Questions

Consider the following sample code below: #include <iostream> using namespace std; class base {
Conside the following sample code below: #include <iostream> using namespace std; class base {
Consider the following sample code: class MyClass { public long x; public void DoWork()
consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following sample code: class SampleClass { public long SomeProperty { get; set;
I have the following code class sample { public void update(List l) { l
Please consider the following simple use case: public class Foo { public virtual int
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code: class A { public: A() {} ~A() {} }; class
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...

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.