Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Array
- {
- int *tab;
- int length;
- // bez dynamicznej alokacji i konstruktora
- // static const int tabsize = 5;
- // int tab [length];
- void check_errors (int index){
- if (index >= length || index < 0){
- cerr << endl << "Bledna wartosc. Podano indeks "<<index<<", a zakres indeksow tablicy to [0-"<<length-1<<"]"<<endl;
- exit(1);
- }
- }
- public:
- Array(int liczba){
- length = liczba;
- tab = new int[liczba];
- int i;
- for (i=0; i<liczba; i++){
- tab[i] = i*i;
- }
- }
- void set(int index, int value){
- check_errors(index);
- tab[index] = value;
- }
- int get(int index){
- check_errors(index);
- return *(tab+index);
- }
- const int * front() const{
- return tab;
- }
- const int *back() const{
- return &tab[length-1];
- }
- int size() const{
- return length;
- }
- void clean_up(){
- delete [] tab;
- }
- void fill(int value){
- for (int i=0; i< length; i++){
- tab[i] = value;
- }
- }
- void assign(const int t[], int tabSize){
- if (tabSize > length) {
- cerr << "Blad. Tablica za dluga." << endl;
- exit(1);
- }
- for (int i=0; i<tabSize; i++) {
- tab[i] = t[i];
- }
- }
- Array* clone() const
- {
- Array* tab2 = new Array;
- tab2->assign(tab, length);
- return tab2;
- }
- };
- int main()
- {
- const int tablica [] = {1,2,3,4,5};
- int ilosc;
- cout << "Podaj wielkosc tablicy: ";
- cin >> ilosc;
- Array arr(ilosc);
- // CIEKAWOSTKA: auto = daje typ taki jaki zwraca funkcja
- auto x = arr.size();
- cout << "Ilosc elementow: " << x << endl;
- arr.set(0, 5);
- arr.set(1, 151515);
- for (int i=0; i<x; i++) cout<<arr.get(i)<<" ";
- const int *p = arr.back();
- cout << endl << "Ostatni element tablicy:" << *p << endl;
- arr.assign(tablica, 5);
- for (int i=0; i<x; i++) cout<<arr.get(i)<<" ";
- auto a2 = arr.clone();
- cout << a2->get(1) << endl;
- arr.clean_up();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement