Advertisement
100rads

copy constructor

May 24th, 2019
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. class Integers {
  7. public:
  8.     int *niz = new int[100];
  9.     int *length = new int;
  10.     Integers()
  11.     {
  12.         *length = 0;
  13.     }
  14.     Integers(Integers &p, int len)
  15.     {
  16.         *length = 0;
  17.         for (int i = 0; i < len; i++)
  18.         {
  19.             //cout << "len: " << len << endl;
  20.             //cout << niz[*length] << endl;
  21.             niz[*length] = p.niz[i];
  22.             *length += 1;
  23.         }
  24.     }
  25.     int GetLength()
  26.     {
  27.         return *length;
  28.     }
  29.     void Add(int num)
  30.     {
  31.         niz[*length] = num;
  32.         *length += 1;
  33.     }
  34.     void Remove()
  35.     {
  36.         niz[*length] = NULL;
  37.         *length -= 1;
  38.     }
  39.     void Print()
  40.     {
  41.         for (int i = 0; i < *length; i++)
  42.         {
  43.             cout << niz[i] << " ";
  44.         }
  45.         cout << endl;
  46.     }
  47.     void PrintSorted(Integers n1)
  48.     {
  49.         sort(&n1.niz[0], &n1.niz[*length]);
  50.  
  51.         for (int i = 0; i < *length; i++)
  52.         {
  53.             cout << n1.niz[i] << " ";
  54.         }
  55.         cout << endl;
  56.     }
  57. };
  58.  
  59. int main()
  60. {
  61.     Integers s1;
  62.     s1.Add(10);
  63.     s1.Add(30);
  64.     s1.Add(25);
  65.     s1.Add(5);
  66.     //s2 = s1;
  67.     Integers s2(s1, s1.GetLength());
  68.     s2.Add(50);
  69.     s2.Add(1);
  70.  
  71.     s1.Print();
  72.     s2.Print();
  73.  
  74.     cout << "s1 sorted:";
  75.     s1.PrintSorted(s1);
  76.     cout << "s2 sorted:";
  77.     s2.PrintSorted(s2);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement