Advertisement
Caminhoneiro

IntArray example

Apr 26th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. // ConsoleApplication(CPP Kaioh planet).cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. #define NDEBUG
  4. #define _CRTDBG_MAP_ALLOC
  5. #include <stdlib.h>
  6. #include <crtdbg.h>
  7. #include "pch.h"
  8. #include <iostream>
  9. #include <cassert>
  10.  
  11. class IndexIsOutOfBoundsException {};
  12.  
  13. class IntArray {
  14.  
  15. private:
  16.     int * m_ptr{ nullptr };
  17.     int m_size{ 0 };
  18.  
  19. public:
  20.    
  21.     bool IsValidIndex(int index) const {        //Bounds check
  22.         return (index >= 0) && (index < m_size);
  23.     }
  24.  
  25.     IntArray() = default; // This will simply create a IntArray with pointer set to null and size set to zero
  26.  
  27.     //Another constructor that takes the size of array as an input parameter
  28.     explicit IntArray(int size) { //use explicit with one argument constructor, unless I want automatically implicity conversions
  29.         if (size != 0) {
  30.             m_ptr = new int[size] {}; //Allocate array memory block on the heap (The brackets putted over there is to properly initialize the elements to zero. Without them you will get some garbage in the array memory block   
  31.             m_size = size;
  32.         }
  33.     }//Without the use of "explicit" the compiler would automattically use this constructor to implicity convert integer numbers
  34.     //to objects of this IntArray class, witch doesnt make sense from an array semantic perspective, and would trigger subtle bugs.
  35.    
  36.     //Ready only properties
  37.     int Size() const {
  38.         return m_size;
  39.     }
  40.  
  41.     bool IsEmpty() const {
  42.         return (m_size == 0);
  43.     }
  44.  
  45.     int &operator[](int index)  {       //Overloaded operator //And if it is without the & he will point to an int and not to the element address causing an error
  46.         if (!IsValidIndex(index)) {
  47.             throw IndexIsOutOfBoundsException{};
  48.         }
  49.         return m_ptr[index];        //Return the ptr index as an int type
  50.     }
  51. /*
  52.     int operator[](int index) const {      
  53.         if (!IsValidIndex(index)) {
  54.             throw IndexIsOutOfBoundsException{};
  55.         }
  56.  
  57.         return m_ptr[index];
  58.     }*/
  59.  
  60.     ~IntArray() {
  61.         delete[]m_ptr;
  62.     }
  63. };
  64.  
  65. std::ostream& operator<<(std::ostream& os, IntArray& a) {
  66.     os << "[";
  67.     for (int i = 0; i < a.Size(); i++) {
  68.         os << a[i] << ' ';
  69.     }
  70.     os << "]";
  71.  
  72.     return os;
  73. };
  74.    
  75. int main()
  76. {
  77.     using std::cout;
  78.  
  79.     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  80.  
  81.     cout << "Creating an empty array.\n";
  82.     IntArray a{};
  83.     cout << "a.Size() is " << a.Size() << '\n';
  84.     assert(a.IsEmpty());
  85.  
  86.     cout << "-------------------------------------------\n";
  87.  
  88.     cout << "Creating an array that contains elements\n";
  89.     IntArray b{10};
  90.     cout << "b.Size() is " << b.Size() << '\n';
  91.     assert(!b.IsEmpty());
  92.  
  93.     cout << "-------------------------------------------\n";
  94.  
  95.     cout << "Creating an array and getting their value\n";
  96.     IntArray c{3};
  97.     c[1] = 666;
  98.     cout << "c[1] is " << c[1] << '\n';
  99.    
  100.     cout << "-------------------------------------------\n";
  101.  
  102.     cout << "Creating an array for bound checks\n";
  103.     IntArray d{ 3 };
  104.     d[1];
  105.     cout << "d[1] is " << d[1] << '\n';
  106.  
  107.     cout << "-------------------------------------------\n";
  108.  
  109.     IntArray e{ 15 };
  110.     for (int i = 0; i < e.Size(); i++) {
  111.         e[i] = (i + 1) * 10;
  112.     }
  113.  
  114.     cout << "Array elements: " << e << "\n";
  115.  
  116.  
  117.  
  118.     //_CrtDumpMemoryLeaks();
  119.     //An array of elements of type char can be declared like this
  120.     //char v[6] = {'XXX', 'CCC', 'VVX', 'F', 'X', 'XVX'};
  121.     //char *p = &v[3]; // p points to vā€™s four th element
  122.     //char x = *p;
  123.  
  124.  
  125.     ////1 Allocate big chunk of memory using new[]
  126.     //int *p = new int[1000000]{};
  127.  
  128.     ////2 Use the memory
  129.     //std::cout << "Before delete!\n " << &p[500] <<"\n";
  130.  
  131.     ////3 Must release it!
  132.     //p = nullptr;
  133.     ////delete[]p;
  134.     //std::cout << "After delete!\n " << &p[500] << "\n";
  135.  
  136.    
  137.  
  138.  
  139. };
  140.  
  141.  
  142. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  143. // Debug program: F5 or Debug > Start Debugging menu
  144.  
  145. // Tips for Getting Started:
  146. //   1. Use the Solution Explorer window to add/manage files
  147. //   2. Use the Team Explorer window to connect to source control
  148. //   3. Use the Output window to see build output and other messages
  149. //   4. Use the Error List window to view errors
  150. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  151. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement