Open In App

How to Declare a Copy Constructor in a Derived Class?

Last Updated : 21 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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 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++.

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;
}

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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads