Advertisement
Guest User

ckonstr

a guest
Jan 22nd, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Konstr.h"
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7.  
  8.  
  9. using namespace std;
  10.  
  11.     CKonstr::CKonstr()
  12.     {
  13.         InitializeAndZeroFillArr();
  14.         s_name = DEFAULT_NAME;
  15.         cout << "bezp: " + s_name << endl;
  16.     }
  17.  
  18.  
  19.     CKonstr::CKonstr(string s_name)
  20.     {
  21.         this->s_name = s_name;
  22.         cout << "parametr: " + s_name << endl;
  23.     }
  24.  
  25.  
  26.     CKonstr::CKonstr(const CKonstr& source)
  27.     {
  28.         s_name = source.s_name + "_copy";
  29.         InitializeAndZeroFillArr(source.i_size);
  30.         CopyArr(source.arr, arr, i_size);
  31.     }
  32.  
  33.  
  34.     CKonstr::~CKonstr()
  35.     {
  36.         delete arr;
  37.         cout << "removing: " + s_name << endl;
  38.     }
  39.  
  40.  
  41.  
  42.  
  43. //Zmianę długości tablicy
  44.     void CKonstr::ChangeArrSize(int i_size)
  45.     {
  46.         int* tempArr = new int[i_size];
  47.         for (int i = 0; i < i_size; i++)
  48.         {
  49.             if (i < i_size) tempArr[i] = arr[i];
  50.             else tempArr[i] = 0;
  51.         }
  52.         this->i_size = i_size;
  53.         //arr* = &tempArr; ?????????????????????
  54.     }
  55.  
  56.  
  57. //Przypisanie określonej komórce tablicy wartości typu int
  58.     bool CKonstr::SetValueByIndex(int i_index, int i_value) //
  59.     {
  60.         if (i_index < i_size)
  61.         {
  62.             arr[i_index] = i_value;
  63.             return true;
  64.         }
  65.         else return false;
  66.     }
  67.  
  68.  
  69. //Odczyt wartości określonej komórki
  70.     int CKonstr::GetValueByIndex(int i_index)
  71.     {
  72.         return arr[i_index];
  73.     }
  74.  
  75.  
  76. //Utworzenie klonu obiektu(innego obiektu posiadającego te same wartości w tablicy)
  77.     CKonstr* CKonstr::Clone()
  78.     {
  79.         return new CKonstr(*this);
  80.     }
  81.  
  82.  
  83. //Przypisanie obiektowi A, wartości i stanu tabeli w obiekcie B (po wykonaniu takiej operacji w obiekcie A tabela ma posiadać tą samą długość i te same wartości, co tablica w obiekcie B)
  84.     void CKonstr::CopyArr(int* source, int* dest, int i_size)
  85.     {
  86.         for (int i = 0; i < i_size; i++)
  87.         {
  88.             dest[i] = source[i];
  89.         }
  90.     }
  91.  
  92. //Zwrócenie informacji o obiekcie do zmiennej typu string w formacie: (<nazwa obiektu> len: <liczba pozycji> values : <wszystkie wartości z tablicy oddzielone przecinkami>)
  93.     string CKonstr::ToString()
  94.     {
  95.         string res = s_name + " len: " + to_string(i_size) + " values: ";
  96.         for (int i = 0; i < i_size-1; i++)
  97.         {
  98.             res += to_string(arr[i]) + ", ";
  99.         }
  100.         res += to_string(arr[i_size - 1]) + ".";
  101.         return res;
  102.     }
  103.  
  104.  
  105.     void CKonstr::SetName(string s_name)
  106.     {
  107.         this->s_name = s_name;
  108.     }
  109.  
  110.     void CKonstr::InitializeAndZeroFillArr()
  111.     {
  112.         arr = new int[DEFAULT_SIZE];
  113.         i_size = DEFAULT_SIZE;
  114.         for (int i = 0; i < i_size; i++)
  115.         {
  116.             arr[i] = 0;
  117.         }
  118.     }
  119.  
  120.     void CKonstr::InitializeAndZeroFillArr(int i_size)
  121.     {
  122.         arr = new int[i_size];
  123.         this->i_size = i_size;
  124.         for (int i = 0; i < i_size; i++)
  125.         {
  126.             arr[i] = 0;
  127.         }
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement