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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:23:43+00:00 2026-05-29T20:23:43+00:00

I have a particular scenario below. The code below should print ‘say()’ function of

  • 0

I have a particular scenario below. The code below should print ‘say()’ function of B and C class and print ‘B says..’ and ‘C says…’ but it doesn’t .Any ideas..
I am learning polymorphism so also have commented few questions related to it on the lines of code below.

class A
{
public:
// A() {}
    virtual void say() { std::cout << "Said IT ! " << std::endl; }
    virtual ~A(); //why virtual destructor ?
};

void methodCall() // does it matters if the inherited class from A is in this method
{
    class B : public A{
    public:
        // virtual ~B(); //significance of virtual destructor in 'child' class
        virtual void say () { // does the overrided method also has to be have the keyword  'virtual'
            cout << "B Sayssss.... " << endl; 
        }
    };
    class C : public A {
    public:
        //virtual ~C();
        virtual void say () { cout << "C Says " << endl; }
    };

    list<A> listOfAs;
    list<A>::iterator it;

    # 1st scenario
    B bObj; 
    C cObj;
    A *aB = &bObj;
    A *aC = &cObj;

    # 2nd scenario
    //  A aA;
    //  B *Ba = &aA;
    //  C *Ca = &aA; // I am declaring the objects as in 1st scenario but how about 2nd   scenario, is this suppose to work too?

    listOfAs.insert(it,*aB);
    listOfAs.insert(it,*aC);

    for (it=listOfAs.begin(); it!=listOfAs.end(); it++)
    {
        cout <<  *it.say()  << endl;
    }
}

int main()
{
    methodCall();
    return 0;
}
  • 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-29T20:23:48+00:00Added an answer on May 29, 2026 at 8:23 pm

    Your problem is called slicing and you should check this question: Learning C++: polymorphism and slicing

    You should declare this list as a list of pointers to As:

    list<A*> listOfAs;
    

    and then insert these aB and aC pointers to it instead of creating copies of objects they are pointing to. The way you insert elements into list is wrong, you should rather use push_back function for inserting:

    B bObj; 
    C cObj;
    A *aB = &bObj;
    A *aC = &cObj;
    
    listOfAs.push_back(aB);
    listOfAs.push_back(aC);
    

    Then your loop could look like this:

    list<A*>::iterator it;
    for (it = listOfAs.begin(); it != listOfAs.end(); it++)
    {
        (*it)->say();
    }
    

    Output:

    B Sayssss....
    C Says
    

    Hope this helps.

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

Sidebar

Related Questions

If you have a particular line of C code in mind to examine in
Just curious about a particular scenario of NAT. Let's suppose we have 4 computers
Here is a particular scenario that I have been unclear about (in terms of
I have a C++ exe; under a particular scenario I need to stop the
I'm afraid I couldn't find anything quite like this particular scenario. I have a
Let's say I have a table that represents a super class, students . And
I have an object model that looks like this (pseudo code): class Product {
I have the following scenario for command line argument. If there is a particular
I have the following scenario where I have a page say parentPage.html, which contains
The JQuery has method effectively selects all elements where they have particular descendants. I

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.