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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:44:57+00:00 2026-05-27T23:44:57+00:00

Consider the sample code below: #include <iostream> using namespace std; class core { public:

  • 0

Consider the sample code below:

#include <iostream>

using namespace std;

class core
{
   public:
      core(const core& obj)
      {
         cout << "core copy ctor called\n";
      }
      core()
      {
         cout << "core default ctor called\n";
      }
};

class sample : public core
{
   public:
      sample()
      {
         cout << "sample default ctor called\n";
      }
#if 0
      sample(const sample& obj)
      {
         cout << "sample copy ctor called\n";
      }
#endif
};

int main()
{
   sample s1;
   sample s2 = s1; //Line1
   return 0;
}

Type1: Copy constructor not declared explicitly for class sample

(Type1 is shown in the code above. Then the copy constructor of class sample is implicitly generated by the compiler).
When the statement, Line1 is executed, first the copy constructor of class core is invoked, and then the copy constructor of class sample is invoked.

Type2: Copy constructor defined explicitly for class sample

When the statement, Line1 is executed, first the default constructor of class core is invoked, and then the copy constructor of class sample is invoked.

Question:

Why is this difference in behavior for copy constructor as mentioned in Type1 and Type2 ?

  • 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-27T23:44:58+00:00Added an answer on May 27, 2026 at 11:44 pm

    Because the copy constructor you explicitly define for sample doesn’t ask for core‘s copy constructor to be invoked. You’d have to write : core(obj) if you wanted to make that happen.

    Put another way, when you write an explicit copy constructor, you are taking charge of the copy construction of sample, including its core sub-object. By writing it the way you have done, you have chosen not to initialise the core sub-object using obj. Since you haven’t said how you do want it initialised, the compiler just uses core‘s default constructor instead, hence the behaviour in the second case you outline.

    By contrast, in the first case, the compiler-generated default copy constructor for sample does ask for the core sub-object to be initialised using core‘s copy constructor, hence the observed behaviour.

    • 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 {
Consider the sample program below: #include <iostream> using namespace std; class test { public:
Consider the sample code below: #include <iostream> using namespace std; class A { private:
Conside the following sample code below: #include <iostream> using namespace std; class base {
Consider this code sample: public class Human { public string Value { get; set;}
Consider the following sample code: class SampleClass { public long SomeProperty { get; set;
Consider the following sample code: class MyClass { public long x; public void DoWork()
Consider the following sample code: class Base { public: void f(); virtual void vf();
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
My questions are divided into three parts Question 1 Consider the below code, #include

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.