VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-declare-a-copy-constructor-in-a-derived-class-in-cpp/

⇱ How to Declare a Copy Constructor in a Derived Class? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Declare a Copy Constructor in a Derived Class?

Last Updated : 23 Jul, 2025

In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class.

Declaring a Copy Constructor in a Derived Class

We can declare a copy constructor in a derived class by declaring a function with the same name as the derived class, which takes a reference to an object of the derived class as a parameter. Inside the initializer list, call the base class’s copy constructor to ensure that when an object of the derived class is copied, both the base part and the derived part of the object are copied correctly to avoid repeated logic.

Syntax to Create a Copy Constructor in a Derived Class in C++

DerivedClass(const DerivedClass& otherObj): BaseClass(otherObj)
{
// Derived class copy constructor body
}

Here,

  • DerivedClass is the name of the Derived class.
  • BaseClass is the name of the base class from which DerivedClass is inherited.
  • otherObj is the reference to the object whose copy we want to make.

C++ Program to Declare a Copy Constructor in a Derived Class

The following program illustrates how we can declare a copy constructor in a derived class in C++.


Output
Original Object: Name: John Id: 100 Id2: 200
Base class copy constructor called.
Derived class copy constructor called.
Copied Object: Name: John Id: 100 Id2: 200

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment