How to Declare a Copy Constructor in a Derived Class? Last Updated : 21 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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 ClassWe 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 ClassThe following program illustrates how we can declare a copy constructor in a derived class in C++. C++ // C++ Program to Declare a copy constrcutor in a derived // class #include <iostream> using namespace std; // Base class class Base { protected: string name; int id; public: // constructor to initialize data members Base(string name, int id) { this->name = name; this->id = id; } // Copy constructor of base class Base(const Base& other) { this->name = other.name; this->id = other.id; cout << "Base class copy constructor called." << endl; } }; // Derived class class Derived : public Base { protected: // Data member of the derived class int id2; public: // Constructor of derived class to initialize data // members Derived(string name, int id, int id2) : Base(name, id) { this->id2 = id2; } // Copy constructor of derived class Derived(const Derived& obj) : Base(obj) { // call the copy constructor of base class // explicitly copy other data members in the derived // class this->id2 = obj.id2; cout << "Derived class copy constructor called." << endl; } // Function to print details of the derived class void print() { cout << "Name: " << this->name << " Id: " << this->id << " Id2: " << this->id2 << endl; } }; int main() { cout << "Original Object: "; Derived obj1("John", 100, 200); obj1.print(); Derived obj2 = obj1; // Copying obj1 to obj2 cout << "Copied Object: "; obj2.print(); return 0; } OutputOriginal 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 More infoAdvertise with us Next Article How to Declare a Copy Constructor in a Derived Class? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-class cpp-constructor CPP Examples +1 More Practice Tags : CPP Similar Reads How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w 3 min read How to Create a Class with Constructors and Destructors in C++? In C++, a class is a user-defined data type encapsulating data members and member functions. The constructors and destructors are special member functions of a class used for initializing objects and cleaning up resources respectively. In this article, we will discuss how to create a class with cons 2 min read How to Define the Default Constructor in C++? In C++, a constructor that takes no parameters is called a default constructor. A default constructor gets automatically invoked when an object of a class is created without any arguments. It is mainly used to initialize legal initial values to the member variables or perform other setup tasks. In t 2 min read How to Create a Derived Class from a Base Class in C++? In C++, one of the fundamental concepts of Object Oriented Programming (OOPS) is inheritance, allowing users to create new classes based on existing classes. The class that inherits another class is called derived class and the class that is inherited is called base class. In this article, we will l 2 min read How to Define a Move Constructor in C++? In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy 2 min read Like