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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:53:09+00:00 2026-06-11T09:53:09+00:00

I have the following code [This is an interview question]: #include <iostream> #include <vector>

  • 0

I have the following code [This is an interview question]:

#include <iostream>
#include <vector>

using namespace std;

class A{
public:
    A(){
        cout << endl << "base default";
    }
    A(const A& a){
        cout << endl << "base copy ctor";
    }
    A(int) { 
        cout << endl << "base promotion ctor";
    }
};

class B : public A{
public:
    B(){
         cout << endl << "derived default";
    }
    B(const B& b){
         cout << endl << "derived copy ctor";
    }
    B(int) {
         cout << endl << "derived promotion ctor";
    }
};

int main(){

    vector<A> cont;
    cont.push_back(A(1));
    cont.push_back(B(1));
    cont.push_back(A(2));

        return 0;
    }

The output is :

base promotion ctor
base copy ctor
base default
derived promotion ctor
base copy ctor
base copy ctor
base promotion ctor
base copy ctor
base copy ctor
base copy ctor

I am having trouble understanding this output, specifically why base default is called once and the last 3 copy ctor. Can someone please explain this output ?

Thanks.

  • 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-11T09:53:11+00:00Added an answer on June 11, 2026 at 9:53 am

    The base default constructor is called once from the line

    cont.push_back(B(1));  
    

    All your B constructors call the default A constructor. The last two copy constructors are because of a vector re-allocation. For example, if you added

    cont.reserve(3);
    

    before the push_backs, they’d go away.

    The one before that is a copy of the temporary A(2) in your final push_back.

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

Sidebar

Related Questions

I have the following C# code fragment: using System; class count { public static
I am using Jersey v10 and have written the following code.Is this the right
I have the following code: Intent serviceIntent = new Intent(this, ServiceClass.class); startService(serviceIntent); I have
I have the following code using RapahelJs running in IE. This code cause a
I have the following code: public class HitController : MonoBehaviour, ICentroidHitListener The way I
I have following code like this in 'artists_helpers' class. def round_to(x) (self * 10**x).round.to_f
I have faced the following interview question. Consider this function declaration: void quiz(int i)
I have the following code (in this case for two images - but in
I have the following code: Bear in mind that while this code works on
I have this following code, here CssClass is of the String type. <asp:Panel ID=Panel1

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.