Pages

Showing posts with label C_study_en. Show all posts
Showing posts with label C_study_en. Show all posts

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.

Easy tutorial for C / C ++ - class 6 (Dynamic memory allocation 2)

Easy tutorial for C / C ++ - class 6 (Dynamic memory allocation 2)

- The 'new' operator is used for dynamic memory allocation in the constructor, and the 'delete' operator is used to release the memory in the destructor. Let's look at a simple 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();
     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(){
    delete []p;
}

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

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

     t_Class x(n,v);
     x.prn();

     return 0;
}

- A class 't_Class' was declared. And integer variable 'n' as member variable and pointer variable 'p' to be used for dynamic memory allocation were also declared.

- Let's create a constructor with two integer arguments. The argument 'num' indicates the size of the array to be allocated dynamic memory. The argument 'ini' represents the initial values of the array to be created. We used 'for loop' to initialize each element of array by 'ini'.

- Use the output function 'prn ()' to check that the size of the array and the initial value appear correctly. And in the destructor, we use the 'delete' operator to release the allocated memory.

- Declared two integer variables 'n' and 'v' in the main function. I will use the 'cin' object to input the size and initial value of the array with the keyboard.

- The object 'x' is declared and given 'n' and 'v' as arguments. Let's check the output through the output function 'prn ()'.

results :

 Size        :

- You will be prompted to enter the size of the array. Let's enter 7 here.

 Size        : 7
 Initial values :

- You are prompted to enter the initial value of the array. Enter 1 here and press Enter.

 Size           : 7
 Initial values : 1  

 Size           : 7
 Initial values :

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

- An array of size 7 is created and the values of array elements are initialized to 1.

Friday, November 24, 2017

Easy tutorial for C / C ++ - class 5 (Dynamic memory allocation (new, delete))

Easy tutorial for C / C ++ - class 5 (Dynamic memory allocation (new, delete))

Dynamic memory allocation (new, delete) :

- Let's review the dynamic memory allocation using the 'new' operator. I have used variables and arrays in the previous examples. In general, a variable or array that you declare will allocate memory and use it in a 'stack'. The size of the memory space is determined when compiling, and the size of the memory space can not be changed during program execution. This is called static memory allocation.

- There is an area called 'heap' that can be used until the programmer explicitly releases the memory. While programmers have the advantage of being free to adjust, they also have administrative responsibilities.

- You can also allocate as much memory space as you want (even during program execution). This allocation of memory space at runtime is called dynamic memory allocation.

- Dynamic memory allocation can be allocated through the 'new' operator. The allocated space is managed by a pointer variable. And the memory space allocated by 'new' operator must be released by 'delete'. To declare an array with dynamic memory allocation is as follow:

usage)

allocation : 'pointer variable' = new 'datatype' ['number of element'];
deallocation : delete [] 'pointer variable';

- Let's look at a simple example using arrays.

Example Code

#include <iostream>
using namespace std;

int main() {

          int n ;
          cout << " Size of array = ";
          cin >> n;

          int *a;
          a = new int[n];

          for(int i=0; i < n; i++){
                   cout << "a[" << i << "] = " ;
                   cin >> a[i] ;
          }

          cout << endl;

          for(int i=0; i < n; i++){
                   cout << "a[" << i << "] = " << a[i] << endl ;
          }
          delete []a;
           return 0;
}

- First we declare integer variable 'n' to store the size of the array, and input the size of array through cin object. We declare pointer variable 'a' and use 'new' operator to point to the array allocated memory space in the heap area.

- For for loop, input the value to each element of array and output the result.

- You have used delete to release the space allocated by the new operator. Where '[]' indicates that the array to be released is an array.

results :

 Size of array =

- You are prompted to enter the size of the array. Here we enter 7, and then we assign 1,2,3,4,5,6,7 to the elements of the array. 

 Size of array = 7
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7

a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7

- You can see that the element value of the input array is output.

Easy tutorial for C / C ++ - class 4 (default values of constructor and destructor)

Easy tutorial for C / C ++ - class 4 (default values of constructor and destructor)

- Let's see how to give a constructor's default value in initialization with a constructor. When declaring a constructor within a class, you can supply a datatype and an initial value.

- Let's look at an example.

Example Code

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

class tClass{
   private :
     int Na;
     int Nb;

   public :
     tClass(int A = 1, int B = 2);
     void prn();
};

tClass::tClass(int A, int B){
    Na = A;
    Nb = B;
}

void tClass::prn(){
     cout << " Na = " << Na << ", Nb = " << Nb << endl ;
}

int main() {

     tClass x;
     cout << " x : " << endl ;
     x.prn();
     cout << endl ;

     tClass y(3);
     cout << " y : " << endl ;
     y.prn();
     cout << endl ;

     tClass z(4,5);
     cout << " z : " << endl ;
     z.prn();

     return 0;
}

- We created class 'tClass' and declared member variables 'Na, Nb'. We have created a constructor with two integer arguments. At this time, we gave 1 and 2 as initial values.

- You created object 'x' in main function. In this case, we created it without arguments. We then called the output function.

- When declaring object 'y', only one argument were given. ('tClass y (3); ') Let's call the function' prn () 'to check the result.

- Then, when declaring object 'z', two arguments were given. ('tClass z (4,5); ') Let's call the function' prn () 'to check the result.

results :

 x :
 Na = 1, Nb = 2

 y :
 Na = 3, Nb = 2

 z :
 Na = 4, Nb = 5

- For object 'x', you can see that the initial values of 1 and 2 of the constructor are output.

- For object 'y', only one initial value (3) is given. At this time, 3 is assigned to the member variable 'Na' and the member variable 'Nb' is assigned the initial value '2' of the constructor.

- In case of object 'z', initial values 4 and 5 at the time of object creation are assigned to member variables Na and Nb and output.


Destructor :

- A destructor (as the opposite of a constructor) is a special member function that is called when an object is destroyed and cleans up the object.

- The destructor is indicated by a tilde (~) before the class name. You can not specify a parameter and the data type of the return value.

ex)
Declaration of constructor : tClass ();
Declaration of destructor : ~ tClass ();

Definition of constructor : tClass :: tClass () {}
Definition of destructor : tClass :: ~ tClass () {}

- The C ++ compiler automatically calls the default destructor even if you do not explicitly describe the destructor. If you need to do something special when destroying an object, you should create a destructor and do something special.

ex) If you have been allocated memory space by dynamic memory allocation in the constructor, you need to deallocate memory allocated by the destructor.

Easy tutorial for C / C ++ - class 3 (Constructor)

Easy tutorial for C / C ++ - class 3 (Constructor)

Constructor :

- Initialization of an object usually means initialization of member variables. Commonly member variables are set to 'private', you need to change the access specifier to 'public' or define the initialization function separately to initialize it arbitrarily.

- A special function provided to compensate for this is the Constructor. A constructor is a special member function for initializing an object. It has the same name as the class name and does not specify the data type of the return value. You can also overload with different arguments.

- If the programmer does not define a constructor, the C ++ compiler automatically creates a default constructor. It is equivalent to a class name, and has no role as an argumentless constructor.

- Let's look at an example.

Example Code

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

class tClass{
   private :
     int Na;
     int Nb;

   public :
     tClass();
     tClass(int, int);
     void prn();
};

tClass::tClass(){
    Na = 10;
    Nb = 20;
}

tClass::tClass(int A, int B){
    Na = A;
    Nb = B;
}

void tClass::prn(){
     cout << " Na = " << Na << ", Nb = " << Nb << endl ;
}

int main() {

     tClass x;
     cout << " Class x " << endl ;
     x.prn();
     cout << endl ;

     tClass y(50,60);

     cout << " Class y " << endl ;
     y.prn();
     return 0;
}

- We created the class 'tClass' and declared the member variables 'Na, Nb'. We created a constructor 'tClass ()' with no arguments and a constructor 'tClass (int, int)' with two integer arguments.

- Constructor 'tClass ()' is a constructor that initializes the member variables Na and Nb to 10 and 20, respectively. The constructor 'tClass (int, int)' allows the user to set the initial values of the member variables Na and Nb when creating the object.

- First we declare the object 'x' in the main function and print the value of the member variables. Let 's declare object' y 'and give parentheses' ()' to input values 50 and 60. In this case, a constructor with two integer arguments is called and the given value is assigned to each member variable. Let's call the function 'prn ()' to check the result.

results :

 Class x
 Na = 10, Nb = 20

 Class y
 Na = 50, Nb = 60

- For object 'x', a constructor with no arguments is called and the result is 10 and 20 for Na and Nb, respectively. If you call the output function 'prn ()', you will see that 10 and 20 are output.

- When declaring object 'y', we gave two arguments 50 and 60. At that time, a constructor with two arguments is called, and 50 and 60 are assigned to each member variable. When you call the output function 'prn ()', you will see that 50 and 60 are output.

Easy tutorial for C / C ++ - class 2 (class)

Easy tutorial for C / C ++ - class 2 (class)

- Let's create a simple class.

Example Code

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

class tClass{
   private :
     int Na;
     int Nb;

   public :
     void init();
     void input(int,int);
     void prn();
};

void tClass::init(){
    Na = 10;
    Nb = 20;
}

void tClass::input(int A, int B){
    Na = A;
    Nb = B;
}

void tClass::prn(){
     cout << " Na = " << Na << ", Nb = " << Nb << endl ;
}

int main() {

     tClass x;

     x.init();
     x.prn();

     x.input(30,40);
     x.prn();

     return 0;
}

- We declared a class named 'tClass'. You have created the member variables (Na and Nb) and specified the access specifier as private.

* If you do not specify an access specifier, the default value 'private' is applied. 

- If you write 'private' (or public or protected), 'private' (or public or protected) will be applied to all subsequent members until another specifier appears.

- The members specified as 'private' are only available within the member functions of the class. Typically, member variables of classes are set to 'private'.

- Member functions (init (), input (), prn ()) are set to 'public'. If it is set to 'public', it can be used in member functions in the class, and it can be used in the area where the object is declared. In general, you should set member functions to 'public'.

- We defined the member function 'init ()' of class 'tClass'. Since the member function is a member of the class, the scope operator (: :) is used to indicate membership. And the initial value is assigned to the member variable 'Na, Nb'.

- The member function 'input ()' is a function that allows you to assign a value to a member variable. The member function 'prn ()' of the 'tClass' class is a function that prints the value of the member variable.

- We defined object (class variable) 'x' in main function. We then used the member reference operator ('.') to access the member function and print the result. The result is as follows.

results :

 Na = 10, Nb = 20
 Na = 30, Nb = 40

- The first result is the initial value assigned to the member variable 'Na, Nb'. The second shows the result of changing the value assigned to the member variable 'Na, Nb' using the input method.

- If an attempt is made to change the value of a member variable directly in the main function (x.Na = 0;), an error occurs. This is because the member variable is set to 'private'. If you want to access member variables directly (without using member functions) in the main function, you can specify the access specifier as 'public'.