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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:01:15+00:00 2026-06-08T17:01:15+00:00

I have a problem in virtual function: Here is some code as an example:

  • 0

I have a problem in virtual function:
Here is some code as an example:

class A
   {
      public : virtual  void print(void)
           {
              cout<< "A::print()"<<endl;
           }
    };
 class B : public A
    {
      public : virtual void print(void)
           {
               cout<<"B::print()"<<endl;
           }
    };
 class C : public A
    {
      public : void print(void)
            {
               cout<<"C::print()"<<endl;
            }
     };
  int main(void)
     {
         A a,*pa,*pb,*pc;
         B b;
         C c;
         pa=&a;
         pb=&b;
         pc=&c;

         pa->print();
         pb->print();
         pc->print();

         a=b;
         a.print();
         return 0;
       }

the result:
A::print()
B::print()
C::print()
A::print()

I know it’s a Polymorphism ,and know have a table called virtual-function-table,but I don’t know how it is to achieve,And

   a=b;
   a.print();

the result is: A::print() not B::print(),why it hasn’t polymorphism.
thank you!

  • 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-08T17:01:20+00:00Added an answer on June 8, 2026 at 5:01 pm
    a=b;
    a.print();
    

    It will print A::print() because a=b causes object-slicing, which means a gets only the a-subobject of b. Read this :

    • What is object slicing? .

    Note that runtime-polymorphism can be achieved only through pointer and reference types. In the above code, a is neither pointer, nor reference type:

    A * ptr = &b; //syntax : * on LHS, & on RHS
    A & ref =  b; //syntax : & on LHS, that is it!
    
    ptr->print(); //will call B::print() (which you've already seen)
    ref.print();  //will call B::print() (which you've not seen yet)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class: class CBase { public: virtual void SomeChecks() {} CBase()
I hope I got the relevant code in here. I have some problem when
I have two classes: class Object { public: Object(); virtual void update(); virtual void
I have defined some models like this (Entity Framework Code-First): public class A {
i have a script which is for virtual keyboard, i am facing some problem
I have a class template where some methods are defined as virtual to give
I have a strange problem in my rich client application. Here is some background:
I have some code where I really want to call a virtual method from
Here is some sample code to illustrate the problem I am having. #include <iostream>
I have two classes like this (simplified and renamed): class Base { public: virtual

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.