Pages

Friday, December 8, 2017

Easy tutorial for C / C ++ - class 7 (Copy constructor)

Easy tutorial for C / C ++ - class 7 (Copy constructor)

Copy constructor :

- A constructor that is created when an object of the same class already created at the time of object creation is given as an initial value. Copy the member variable values of the class to the generated object.

- The basic form of the copy constructor is as follows.
'Class name' (const 'class name' & 'object name');

- It treats it as a reference variable by giving an existing object as an argument. And because you should not change the original object, use the 'const' keyword.

- If you allocate memory dynamically within a class, you must create a custom copy constructor to allocate dynamic memory. Let's create a custom copy constructor with the following example.

Example Code

#include <iostream>
#include <iomanip>
using namespace std;

class t_Class{
   private :
     int n;
     int *p;

   public :
     t_Class(int, int);
     t_Class(const t_Class &ttt);
     ~t_Class();
     void prn();
};

t_Class::t_Class(int num, int ini){
    p = new int[num];
    n = num ;
    for(int i = 0; i < n; i++){
         p[i] = ini;
         }
}

void t_Class::prn(){
    cout << " Size : " << n << endl ;
    cout << " Initial values : " << endl;
    cout << endl;
    for(int i = 0; i < n; i++){
         cout << " p[" << i << "] = " << p[i] << endl;
         }
}

t_Class::t_Class(const t_Class &ttt){
     n = ttt.n ;
     p = new int[n] ;

     for(int i = 0; i < n; i++){
          p[i] = ttt.p[i] ;
          }
}

t_Class::~t_Class(){
    delete []p;
}

int main() {
     int n, v;
     cout << " Size : " ;
     cin >> n ;

     cout << " Initial values : " ;
     cin >> v ;
     cout << endl;

     t_Class x(n,v);
     cout << " Results of \'x\' " << endl ;
     x.prn();
     cout << endl;

     t_Class y(x);
     cout << " Results of \'y\' " << endl ;
     y.prn();

     return 0;
}

t_Class(const t_Class &ttt); : The part that declares a custom copy constructor.

n = ttt.n ; : In the definition part of copy constructor, the value of the member variable 'n' of the reference variable ('ttt') is assigned to the variable 'n' of the copy constructor object.

p = new int[n] ; : Using the 'new' operator, the pointer variable 'p' of the copy constructor object pointed to the array to which the heap space was allocated.

p[i] = ttt.p[i] ; : The for loop is used to assign the initial value of the array of reference variables ('ttt') to each element of the array of the copy constructor object.

- We will declare 'n' and 'v' in the main function and use the 'cin' object to input the size and initial value of the array. And we declared object 'x' and gave 'n' and 'v' as arguments. Let's check the output function.

- Let's create an object 'y' using the copy constructor, print it, and compare it.

results :

 Size :

- You are asked to enter the size of the array. We'll enter 5 here.

 Size : 5
 Initial values :

- You are prompted to enter the initial value of the array. We will assign 2 here.

 Size : 5
 Initial values : 2

 Results of 'x'
 Size : 5
 Initial values :

 p[0] = 2
 p[1] = 2
 p[2] = 2
 p[3] = 2
 p[4] = 2

 Results of 'y'
 Size : 5
 Initial values :

 p[0] = 2
 p[1] = 2
 p[2] = 2
 p[3] = 2
 p[4] = 2

- Notice that the result of object 'x' is array size 5, and that the initial value of each element in the array is 2. You can see that the object 'y' defined by the copy constructor also gives the same result.

No comments:

Post a Comment