The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method. Here is a simple constructor class example below,
1 2 3 4 5 6 7 8 9 10 |
class myclass { public: myclass() { std::cout << "myclass is constructed!\n"; }; }; |
There are different constructor types in classes and the Copy Constructor is one of these. Copy Constructors not only used in classes but also used with struct and union data types Do you want to learn what is copy constructor or what kind of methods we have that we can declare and use copy constructors? In this post, we will try to explain how to use Copy Constructor with examples.
The Copy Constructor in classes (i.e class_name) is a non-template constructor whose first parameter is class_name&, const class_name&, volatile class_name&, or const volatile class_name& . It can be used with no other parameters or with the rest of the parameters all have default values.
The Copy Constructor is a constructor type for classes that class_name must name the current class, or it should be a qualified class name when it is declared at namespace scope or in a friend declaration.
Typical Declaration of a Copy Constructor
Syntax to declare a Typical Copy Constructor,
1 2 3 |
class_name (const class_name &) |
Syntax to use Copy Constructor, this copy source_class to new_class as below,
1 2 3 |
class_name new_class(source_class); |
An example with a class;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> class myclass { public: int param; myclass() // Default Constructor { }; myclass(const myclass& a) // Copy Constructor { }; }; int main() { myclass class1; class1.param=100; std::cout << class1.param << '\n' ; // Copy Class1 to Class2 by using Copy Constructor myclass class2(class1); std::cout << class2.param << '\n' ; getchar(); return 0; } |
and the output will be,
1 2 3 4 |
100 4358220 |
As you see, this copy constructor copies only class properties and methods but not values of it. To copy values of parameters (attributes) of classes we should add each parameter into copy constructor and set them as below,
1 2 3 4 5 6 |
myclass(const myclass& a) // Copy Constructor { param = a.param; // we need to copy properties as in here }; |
Thus, by adding this line full Copy Constructor example can be like this,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> class myclass { public: int param; myclass() // Default Constructor { }; myclass(const myclass& a) // Copy Constructor { param = a.param; // we need to copy values of each properties as in here }; }; int main() { myclass class1; class1.param=100; std::cout << class1.param << '\n' ; // Copy Class1 to Class2 by using Copy Constructor myclass class2(class1); std::cout << class2.param << '\n' ; getchar(); return 0; } |
Now, this copy constructor copies not only class properties and methods but also copies values defined by user. Result will be as follows,
1 2 3 4 |
100 100 |
Both methods can be used, if you don’t need to copy to each values of properties of the source class to the destination class, and you will set each parameters with new values use the first example, it will be faster. If you need to copy some or all parameter values then you can use the last example here.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition